使用按钮VBA创建数据输入表单

时间:2018-03-28 15:10:51

标签: sql database access-vba access

我正在尝试创建一个基本的数据输入表单,然而,它变得比我预期的更麻烦。我已经创建了表单,现在我只是想将数据插入到数据库(TEST)中。我收到“Object Required”错误。有什么建议?所有txt框都经过验证是正确的。这一切都是通过Access VBA

完成的
Private Sub Command28_Click()



Dim sSQL As String

Set sSQL = "INSERT INTO TEST (Carrier_Ent_ID, Row_Insert_TS, Row_Update_TS, 
            Row_Insert_User_ID, Row_Update_User_ID, Carrier_Ent_Name, Active) 
VALUES (" & Me.txtENTID & "," & Me.txtDate & "," & Me.txtDate & "," & 
            Me.cmboUserID & "," & Me.cmboUserID & "," & Me.txtENTNAME & "," 
          & Me.Txtactive & "); "


DoCmd.RunSQL.sSQL

End Sub

1 个答案:

答案 0 :(得分:0)

从我所看到的,代码中存在一些错误。

在设置对象的引用时,只使用SET - sSQL是文本字符串 DoCmd.RunSQL之后RunSQL不应该只有文字字符串 如果Me.txtENTNAME是一个文本字符串,它应该在它之前和之后有一个撇号。

Private Sub Command28_Click()

    Dim sSQL As String

    'No Set & ' before and after Me.txtENTNAME (assuming it's text).
    sSQL = "INSERT INTO TEST (Carrier_Ent_ID, Row_Insert_TS, Row_Update_TS, " & _
           "Row_Insert_User_ID, Row_Update_User_ID, Carrier_Ent_Name, Active) " & _
           "VALUES (" & Me.txtENTID & "," & Me.txtDate & "," & Me.txtDate & "," & _
           Me.cmboUserID & "," & Me.cmboUserID & ",'" & Me.txtENTNAME & "'," & Me.txtActive & "); "

    'No full stop after RunSQL.
    DoCmd.RunSQL sSQL

End Sub