使用Access 2010,我有Purchase_Orders
的表单,其中status
会根据子表单中的Items
是否已发送而发生更改,并且会受到影响截至日期。
Private Sub Form_AfterUpdate()
Dim rs As Recordset
Dim db As Database
Dim var_Delivered As String
var_Delivered = "SELECT Count(*) AS d_Count" & _
" FROM Items" & _
" WHERE PO_ID =" & Me.PO_ID.Value & _
" AND Supplier_Dnote_ID IS Null" & _
" AND Delivered_Without_Dnote =0;"
Set db = CurrentDb
Set rs = db.OpenRecordset(var_Delivered, dbOpenDynaset)
'MsgBox rs!d_Count
If rs!d_Count > 0 Then
If Me.Supply_date < Date Then
Me.Status = "Overdue"
Else
Me.Status = "Submitted"
End If
Else
Me.Status = "Delivered"
End If
db.Close
Set db = Nothing
Set rs = Nothing
End Sub
这将运行Purchase_Orders
的after_update。我有一个save_close
按钮,它使用以下代码并且不会返回错误:
If Me.Dirty = True Then
DoCmd.Close acForm, "Purchase_Orders", acSaveYes
Else
DoCmd.Close acForm, "Purchase_Orders", acSaveNo
End If
但是,我还有一个Save
按钮,不会关闭表单。这是我得到运行时错误2759:您尝试在对象上调用的方法失败。 Debug突出显示saverecord行。
Private Sub SaveOnlyBtn_Click()
If Me.Dirty = True Then
docmd.RunCommand acCmdSaveRecord
End If
End Sub
如果我将状态代码注释掉并使用保存按钮,则记录保存正常,没有任何错误。为什么我会收到此错误?我完全难过,在线搜索错误也没有帮助我。
答案 0 :(得分:1)
所以当我把代码放在&#34; on dirty&#34;上时,我发现错误没有发生。事件,然后让我意识到,我不需要在表单更新后必须运行代码,只有在特定字段更改时。因此,我将代码更改为公共代码,并在supply date
,delivered_without_dnote
或supplier_Invoice_ID
更改时调用它。
公共代码是:
Public Sub delivered_status()
On Error GoTo errTrap1
If Forms!Purchase_Orders_Ex.Form!Status = "Cancelled" Then
Exit Sub
Else
DoCmd.RunCommand acCmdSaveRecord
Dim rs As Recordset
Dim db As Database
Dim var_Delivered As String
var_Delivered = "SELECT Count(*) AS d_Count" & _
" FROM Items" & _
" WHERE PO_ID =" & Forms!Purchase_Orders_Ex.Form!PO_ID.Value & _
" AND Supplier_Dnote_ID IS Null" & _
" AND Delivered_Without_Dnote =0;"
Set db = CurrentDb
Set rs = db.OpenRecordset(var_Delivered, dbOpenDynaset)
'MsgBox "Outstanding Items: " & rs!d_Count
If rs!d_Count > 0 Then
If Forms!Purchase_Orders_Ex.Form!Supply_date < Date Then
Forms!Purchase_Orders_Ex.Form!Status = "Overdue"
Else
Forms!Purchase_Orders_Ex.Form!Status = "Submitted"
End If
Else
Forms!Purchase_Orders_Ex.Form!Status = "Delivered"
End If
rs.Close
Set db = Nothing
Set rs = Nothing
End If
errTrap1:
Select Case Err.Number
Case 3314 'form not complete and other required fields are empty
Exit Sub
Case Else
If Err.Number > 0 Then
MsgBox Err.Number & ": " & Err.Description
End If
End Select
End Sub
现在,当我使用save_close
或Save_Only
时,我没有收到错误2759.我不完全理解我的原始方法的哪个部分导致错误但是这种方法不再发生。
答案 1 :(得分:0)
我刚刚遇到此问题,并将代码移出Form_AfterUpdate也为我修复了它。
有趣的是,有问题的代码在本地运行良好,但在部署到客户端时无法正常工作。我尝试只导入修改后的表单而不是替换整个访问应用程序,但我仍然遇到了同样的问题。我还将后端数据库从服务器复制回我的开发机器,但仍然没有在本地解决问题。最重要的是,我做了无尽的紧凑/修复和反编译/编译。
我最后的结论是,这是来自Access黑盒的另一个奇怪的问题,而不是特定代码的问题。