0

I have created a form with submit buttons on it.

I have entered the data in the text box and then clicked on submit button.But the data is not getting saved in the table.Also,it is not showing any error message. It is not working at all.

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 & "')" 

1 Answer 1

1

try this:

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 

The debug.print messages will print to the immediate window (Ctrl+g) to view from VBA editor, you can delete them if you want to once you have confirmed it's working.

Sign up to request clarification or add additional context in comments.

7 Comments

it's giving an error "index or primary key cannot contain a null value"
Okay, this means that we are either not inserting into all of the columns in tblemployee (and at least one of the missing columns is required) or that we have named one or more of the columns incorrectly in the query. Can you compare the SQL query in the debug window to the column names in the table.
Chris you are correct but now it's showing an error "record is deleted".
Can you expand on which of my suggestions was correct and what you have done to fix it please.
I have added another column to tblemployee and modified the query to Sqltext = "INSERT INTO tblemployee(EmployeeID,firstname,lastname,Address,city) " & _ "VALUES('" & Me.EmployeeID & "','" & Me.txtfirstname & "','" & Me.txtlastname & "','" & _ Me.txtaddress & "','" & Me.txtcity & "');"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.