我在MS Access 2002中有一个表单,在命令按钮后面有以下代码片段。
'Requery subform to make sure total is calculated
Me.fsubUpdateShipments.Requery
DoEvents
'Confirm quantities have been entered
If Form_fsubUpdateShipments.txtTotalShipmentQty.Value <= 0 Then
MsgBox "Cannot create shipment, no quantities have been entered", vbCritical
Exit Sub
End If
几个月以来一直运作正常,但今天有一位同事来找我并解释说即使他们输入了数量,也会显示错误信息。
在进行了一些挖掘后,我确定在相关文本框完成计算其值.Value <= 0
之前,正在评估=Sum([QtyToShip])
表达式。这似乎只发生在子表单具有非平凡数量的记录(大约10或更多)时,并且显然是一个严重的问题。
在包含If
语句的行上添加断点允许代码正确运行但我显然无法永久使用此方法。
有什么方法可以强制代码暂停,直到子窗体控件完成重新计算它们的值?
答案 0 :(得分:4)
我会完全跳过使用用户界面并查看表格。如果在主窗体命令按钮上运行,则子窗体失去焦点并保存其值。重新查询是不必要的。只需在DSum的标准(第三个参数)中重新创建父子关系:
If DSum("QtyToShip", "ShipmentDetails", "ShipmentID = " & Me!ShipmentID) <= 0 Then
MsgBox "Cannot create shipment, no quantities have been entered", vbCritical
Exit Sub
End If
答案 1 :(得分:1)
有两个选项,都已经被其他人识别,但由于他们的建议不完整或不整齐(慢),我正在发布此帖子。
:一种。您可以强制等待延迟IF测试:
把它放在一个模块中:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
...
Public Sub GoSleep(millisecondDelay as long)
Sleep (millisecondDelay)
End Sub
以你的形式:
'Requery subform to make sure total is calculated
Me.fsubUpdateShipments.Requery
DoEvents
GoSleep 1000 '1 second delay. Or 2000, whatever is required really
'Confirm quantities have been entered
If Form_fsubUpdateShipments.txtTotalShipmentQty.Value <= 0 Then
MsgBox "Cannot create shipment, no quantities have been entered", vbCritical
Exit Sub
End If
<强> B中。正如另一个答案所说,你可以自己重新计算,并根据这个结果进行测试。
然而,不是使用DSum作为pteranodon建议,我会建议比DSum更合适的东西,因为DSum(DCount等)非常慢。
我通常使用ADO进行数据操作,尽管我的代码可以很容易地适应DAO。要使用此代码,如果您还没有这个代码,则需要引用Microsoft activeX 2.8:
在一个模块中:
Public Function GetDBValue(qry as string) as variant
dim rst as new adodb.recordset
rst.open qry, currentproject.connection, adOpenKeyset, adLockReadOnly
if rst.eof then
GetValue = null
else
GetValue = rst.fields(0)
end if
end function
public Function IsNullSQL(basevalue as variant, replacementvalue as variant) as variant
isNullSQL = iif(isnull(basevalue), replacementvalue, basevalue)
end function
在您的表单中:
''Requery subform to make sure total is calculated
'Me.fsubUpdateShipments.Requery
'DoEvents
'Confirm quantities have been entered
If IsNullSQL(GetValue("SELECT Sum(QtyToShip) FROM tbl WHERE ..."), -1) < 0 Then
MsgBox "Cannot create shipment, no quantities have been entered.", vbCritical, "No Quantities to Ship"
Exit Sub
End If
'If Form_fsubUpdateShipments.txtTotalShipmentQty.Value <= 0 Then
' MsgBox "Cannot create shipment, no quantities have been entered", vbCritical
' Exit Sub
'End If