可能是在尝试将数据插入 Access 数据库表格时出了问题。 在确保表格和字段名称正确无误的前提下,确保插入的值与字段数据类型匹配,例如:插入字符串值时,字段数据类型应为文本。另外,如果正在使用表单,还需要确保表单已打开或表单控件已正确标识。以下是示例代码:
Private Sub cmdAdd_Click()
On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("MyTable", dbOpenDynaset)
rs.AddNew
rs!FirstName = Me.txtFirstName.Value
rs!LastName = Me.txtLastName.Value
rs!Age = Me.txtAge.Value
rs!Address = Me.txtAddress.Value
rs.Update
Set rs = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error " & err.Number & ": " & err.Description, vbExclamation
Set rs = Nothing
Set db = Nothing
End Sub