proc changeData {op win mode} {

global base table  DB Qtable

upvar #0 EZEcols cols
upvar #0 EZEcolatts colatts
upvar #0 EZEshows shows
upvar #0 EZEkeys keys
upvar #0 EZEnrec nrec
upvar #0 EZEcci cci
upvar #0 EZEforf forf
upvar #0 EZEvform vf
upvar #0 EZEhform hf
upvar #0 EZEfwhere fwhere
 
global dbpipe1 sybmsg server ayscheck areyousure


# By request of Uwe Seufert, thanks Uwe:  ARE YOU SURE
# if user sets ayscheck, then check on upd and del

	if {$ayscheck && (($op == "del") || ($op == "upd"))} {

	if {!$areyousure} {

	eval mkDialog .confirm_Chg  \{ -text \"Do You Really Want To?\" \}   \"\{Yes, I'm Sure\}   \{set areyousure 1\; changeData $op $win \{$mode\}\; set areyousure 0 \}\"  \"\{Not Sure/Cancel\} \{set areyousure 0\}\"
	return

	}

	}

	if {$win == $vf} {
		set emode "V"
		set marea $win.msg
	} else {
		set emode "H"
		set marea $win.data.msg
		set entrypre .$hf.data.pic.win
	}

	set keyed [llength $keys]


	case $op in {
	{upd} {
		set sql1 "update $Qtable set "
		set sql2 " where "
	}
	{del} {
		set sql1 "delete from $Qtable"
		set sql2 " where "
	}
	{ins} {
		set sql1 "insert into $Qtable values("
		set sql2 ""
	}
	}

	set firsts 1
	set firstm 1
	set blank 1

	set selex 0
	foreach c $cols {

		if {$c == "oid"} {
			set keym " oid = [$hf.data.pic.win.oid.e get]"
			continue
		}

	set cs $shows($c)
	if {$cs} {

		set cn [keylget colatts($c) name]
		set nulls [keylget colatts($c) nulls]
		set type [keylget colatts($c) type]

		if {$nulls == 0} {
			set nonulls 1
		} else {
			set nonulls 0
		}

		if {$emode == "V"} {
		set match [stringFix2 [$vf.data.can.fr.old.$c get] out]
#
#		WHY ON EARTH did I do this?
#		set match [lindex $old [expr {[llength $old] - 1}]]
#
		} else {
		set match [stringFix2 [$hf.data.pic.win.$c.d get $cci] out]
		}

		if {$emode == "V"} {
		set new [stringFix2 [$vf.data.can.fr.new.$c get] out]
                $vf.data.can.fr.new.$c delete 0 end
                $vf.data.can.fr.new.$c insert 0 $new
		} else {
		set new [stringFix2 [$hf.data.pic.win.$c.e get] out]
                $hf.data.pic.win.$c.e delete 0 end
                $hf.data.pic.win.$c.e insert 0 $new
		}

		set q ""
		if {[getType $type] == "S"} {
			if {[string toupper $new] != "NULL"} {
			set q "'"
			} else {
			set q ""
			}
		} 
		
		if {(!$keyed) || ([lsearch $keys $c] >= 0)} {
		if {$firstm} {
			if {$match == "NULL"} {
			set nextm [format "%s is NULL" $cn]
			} else {
			set nextm [format "%s = %s%s%s" $cn $q $match $q]
			}
			set firstm 0
		} else {
			if {$match == "NULL"} {
			set nextm [format " and %s is NULL" $cn]
			} else {
			set nextm [format " and %s = %s%s%s" $cn $q $match $q]
			}
		}
		} else {
			set nextm ""
		}

		case $op in {

		{upd} {
		if {($new != $match) && ($new != "")} {
			if {$firsts} {
				set nexts [format "%s = %s%s%s" $cn $q $new $q]
				set firsts 0
			} else {
				set nexts [format ", %s = %s%s%s" $cn $q $new $q]
			}
			set blank 0
		} else {
			set nexts ""
		}
		}

		{ins} {
		if {[lsearch $forf $c] < 0} {
#	Why prohibit them here from dup record?  let the dbase rules do that
#			if {$new != $match} {set blank 0}
			if {$new != ""} {
				set blank 0
				set nexts [format "%s%s%s," $q $new $q]
			} else {
				set nexts "null"
			}
		} else {
			set nexts "null"
		}
		if {$nexts == "null"} {
			if {$nonulls} {
				if {$q == "'"} {
					set nexts "' ',"
				} else {
					set nexts "0,"
				}
			} else {
				set nexts "null,"
			}
		}
		} 

		{del} {
#	If in H form then we delete the Selected Record -- gotta select one
#	If in V form we just delete the Current Record
		if {($new != "") || ($emode == "V")} {set blank 0}
		set nexts ""
		}
		}

		append sql1 $nexts
		if {$nextm != ""} {
			append sql2 $nextm
			set selex 1
		}

	}
	}
	if {!$selex} {
		append sql2 $keym
	}

	if {$op == "ins"} {
		set sql1 [string trimright $sql1 ,]
		append sql1 ")"
	}

	
	if {$blank} {
		$marea configure -text "Sorry, you can't delete, update or insert without selecting a record and/or entering at least one new field value!"
		$win.ctrl.change flash
		return
	}

	case $op in {

	{upd del} {
	if {[string first all $mode] >= 0} {
	set sqlcmd [format "%s%s" $sql1 $fwhere]
	} else {
	set sqlcmd [format "%s%s" $sql1 $sql2]
	}
	}

	{ins} {
	set sqlcmd $sql1
	}

	}

	if {[string first show $mode ] >= 0} {

	set title "SQL(NOT)"
	mkAlert "$title" "\"$sqlcmd\"" "OK" l showsql

	} else {


	set msg ""
	set sqt $table
        set res [queryDB $sqlcmd] 


	${DB}chkMsg

        if {[lindex $res 0] == "ERROR"} {
#    		$marea configure -text "Error: line $sybmsg(line): $sybmsg(msgno) : $sybmsg(msgtext)"
    		return
  	} else {
		if {$msg == ""} {
    		$marea configure -text "SQL data modification completed."
		}
#	Find the desired selection again and paint the current page.
#	In theory, if you deleted a record out from under yourself,
#	you should see it disappear whether in Vert or Horiz mode.
#	I don't think I'm going to protect you from that.
		findData {}
	}

	}

}

