我创建了一个非常简单的表单,其中包含两个文本框和一个“更新位置”按钮,其输入值应更新为数据库中的两个链接表。我最终试图把它放在一个独立的应用程序中,但是现在我只想让表单本身工作。我是FoxPro的新手,所以我不知道是否有可能只是一个表单更新我的表,或者是否有其他问题在工作。
这是我的“更新位置”按钮的代码(OldLoc是第一个文本框,NewLoc是第二个):
SET DATABASE TO LOCATIONS
CLOSE ALL DATABASES
OPEN DATABASE locations\locations EXCLUSIVE
IF this.parent.OldLoc.Value == "" then
MESSAGEBOX('Please fill in the old location.', 48, 'Missing Location Information')
this.parent.OldLoc.SetFocus
ELSE
INDEX ON table1.loc TAG loc
SET ORDER TO loc
SEEK this.parent.OldLoc.Value IN table1
IF FOUND()
IF this.parent.NewLoc.Value == "" then
MESSAGEBOX('Please fill in the new location.', 48,
'Missing Location Information') this.parent.NewLoc.SetFocus
UPDATE table1 SET loc = this.parent.NewLoc.Value ;
WHERE loc = this.parent.OldLoc.value
UPDATE table2 SET loc = this.parent.NewLoc.Value ;
WHERE loc = this.parent.OldLoc.value
ENDIF
ENDIF
ENDIF
您对任何输入表示赞赏!
答案 0 :(得分:2)
你在点击事件中做了多余的工作......关闭数据库,然后重新打开,然后打开独占。您尝试更新的表应该已经有了您计划加入或搜索的键列的索引。完成后,您不需要反复显式创建索引标记...一旦创建了索引“TAG”,您就不需要再做了..
所以,那就是说......
打开表单,然后打开“INIT”事件。在那里,您可以在表单正在使用时显式打开表格以供使用...
IF NOT DBUSED( "Locations" )
*/ You only need exclusive if you will be modifying the database.
*/ The indexes should already exist before the form ever starts
Open Database Locations\Locations SHARED
ENDIF
现在,您的更新按钮的“点击”事件...预先确认有值,不要担心尝试寻找或更新,除非填写完... ...
IF ALLTRIM( This.Parent.OldLoc.Value ) == "";
OR ALLTRIM( This.Parent.NewLoc.Value ) == ""
*/ Simple one message about BOTH being required
messagebox( "Please fill in both Old and New Locations.", 48, ;
"Missing Location Information" )
ENDIF
*/ Then, update... if no entries match, no records will be updated.
*/ VFP sometimes chokes with multiple "." values such as form values
*/ as SQL commands are expecting "alias.field" format...
lcOldValue = This.Parent.OldLoc.Value
lcNewValue = This.Parent.NewLoc.Value
Update Locations!Table1;
set Loc = lcNewLoc;
where Loc = lcOldLoc
*/ Were there any updates?
llAnyUpdates = _Tally > 0
*/ Now, the OTHER table too...
Update Locations!Table1;
set Loc = lcNewLoc;
where Loc = lcOldLoc
*/ Were there any updates on THIS cycle...
llAnyUpdates = llAnyUpdates OR _Tally > 0
if llAnyUpdates
messagebox( "Locations have been updated.", 0, "Update complete" )
else
messagebox( "No such location found to be updated.", 0, "No Records Updated" )
endif
答案 1 :(得分:0)
您可能还想查看表单的数据环境。 您可以选择在表单加载时打开表单的表格,这样可以在表单加载事件或表单init事件中明确打开它们
我对“更新位置”按钮的点击事件的简单提议是: -
LOCAL lcOldLoc,lcNewLoc
WITH THISFORM
lcOldLoc=.oldloc.VALUE
lcNewLoc=newloc.VALUE
DO CASE
CASE EMPTY(lcOldLoc)
MESSAGEBOX("PLEASE ENTER OLD LOCATION",16,"ERROR")
CASE EMPTY(lcNewLoc)
MESSAGEBOX("NEW LOCATION CAN'T BE EMPTY",16,"ERROR")
OTHERWISE
UPDATE table1 SET loc=lcNewLoc WHERE loc=lcOldLoc
DO CASE
CASE _TALLY=0
MESSAGEBOX("OLD LOCATION NOT FOUND",16,"ERROR")
OTHERWISE
UPDATE tabel2 SET loc=lcNewLoc WHERE loc=lcOldLoc
ENDCASE
ENDCASE
ENDWITH
这应该是唯一需要的代码。显然,可以对上述内容进行许多改进,但实质上可以完成这项工作。
如果您想从头开始构建上述表单,请发送电子邮件至support@foxsoft.co.uk,我将为您提供现场演示。