我一直在努力让这段代码工作几天。如果您能提供任何解决方案,我将非常感激。
Private Sub Command0_Click()
If IsNull(NewSupBox.Value) Or IsNull(NewNumberBox.Value) Then
MsgBox ("All fields must be filled")
GoTo ErrorExit
End If
If Not IsNull(DLookup("SupplierNumber", "SupGenInfo ", "SupGenInfo.SupplierNumber =" & NewSupBox)) = Then
MsgBox ("This supplier number already exists. You can edit the current record on the Edit supplier page.")
GoTo ErrorExit
End If
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("select * from SupGenInfo")
rec.AddNew
rec("SupplierNumber") = Me.NewSupBox.Value
rec("SupplierName") = Me.NewNameBox.Value
rec.Update
Set rec = Nothing
Set db = Nothing
MsgBox "Records added successfully."
ErrorExit:
End Sub
编辑:忘记提及我没有收到任何错误消息。该命令将不会向我的表添加新记录。
Edit2:上面的代码将输出msg"记录已成功添加"当我删除以下代码块。
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("SupGenInfo")
rec.AddNew
rec("SupplierNumber") = Me.NewSupBox
rec("SupplierName") = Me.NewNameBox
rec.Update
Set rec = Nothing
Set db = Nothing
当包含此代码时,我的命令单击变得无响应。
答案 0 :(得分:0)
我相信,您正在使用select * ...
语句阅读表格(用于显示目的),然后您将新记录添加到该列表而不是实际数据库。当你打开OpenRecordset
时,只提供表名,而不是整个SQL查询shebang ......
我创建了一个新表,因此请编辑此代码以匹配您的参数/值,否则测试工作正常:
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Table1")
rec.AddNew
rec("Field1") = 1234
rec("Field2") = "blah2"
rec("Field3") = "blah3"
rec.Update
Set rec = Nothing
Set db = Nothing
希望这有帮助。