Add record form

damdoumaa

Registered User.
Local time
Today, 12:28
Joined
Apr 23, 2004
Messages
16
I have a contact database and I'm trying to create a regular add form so that the user can add new contacts. On the form, I have unbound text boxes for all the fields of the "contacts" table. I also have a buttom that the user has to push after he/she enters data so that the data could be added to the database.

Here is the code that I have on the "Onclick" event of the buttom:

Dim strSQL As String

strSQL = "INSERT INTO Contacts (LastName, FirstName) VALUES (" & Me.txtLname.Value & "," & Me.txtFname.Value & ");"

DoCmd.OpenQuery "strSQL", , acEdit

The error that I'm getting is the following:
Run-time error '7874
Microsoft Acces can't find the object strSQL

I have tested the SQL statement by displaying it in a textbox inside the form and it is correct.
Could anybody please help me with this?????
 
By using
Docmd.openquery ""strSQL" - you are implying that strSQL exists as a query object. However, it is only a string.

Thus you need to use

DoCmd.RunSQL "strSQL"

Also, why not use bound text boxes to remove the need for coding?

hth
 
Thank you for the quick response.

I tried DoCmd.RunSQL "strSQL" and now i have a different error message that says:

Invalid SQL statement; expected 'DELETE', ' INSERT','PROCEDURE','SELECT', or 'UPDATE'


I tried the same sql statment inside a query and it worked!! do you know what the problem is?


Dim strSQL As String

strSQL = "INSERT INTO Contacts (LastName, FirstName) VALUES (" & Me.txtLname.Value & "," & Me.txtFname.Value & ");"

DoCmd.RunSQL "strSQL"



I'm not using bound text boxes because I want to control what the user enters, give the user messages if they enter something is wrong!! and I don't want the user to have direct access to the database.


Thanks a bunch
 

Users who are viewing this thread

Back
Top Bottom