0

我创建了一个带有提交按钮的表单。

我已经在文本框中输入了数据,然后单击了提交按钮。但是数据没有保存在表中。此外,它没有显示任何错误消息。它根本不起作用。

Private Sub CmdAddNew_Click()
'add data to table
CurrentDb.Execute "INSERT INTO tblemployee(firstname,lastname,Address,city)" & _
" VALUES('" & Me.txtfirstname & "','" & Me.txtlastname & "','" & Me.txtaddress & "','" &   Me.txtcity & "')"
4

1 回答 1

1

尝试这个:

Private Sub CmdAddNew_Click()

    Dim dbs As DAO.Database, Sqltext As String, iCount As Integer
    Set dbs = CurrentDb
    Sqltext = "INSERT INTO tblemployee(firstname,lastname,Address,city) " & _
                 "VALUES('" & Me.txtfirstname & "','" & Me.txtlastname & _
                        "','" & Me.txtaddress & "','" & Me.txtcity & "');"
    Debug.Print "SQL statement generated with variables:" & vbCrLf & Sqltext
    dbs.Execute Sqltext, dbFailOnError
        iCount = dbs.RecordsAffected
    Debug.Print "..." & iCount & " row(s) inserted"

End Sub

debug.print 消息将打印到即时窗口 (Ctrl+g) 以从 VBA 编辑器中查看,一旦您确认它正在工作,您可以删除它们。

于 2013-10-08T06:11:54.160 回答