Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT INTO Statement Help 1

Status
Not open for further replies.

3239

Technical User
May 14, 2003
64
I am trying to learn how to program using VBA and SQL. I created a Sub routine that I want to use to insert records from an unbound form into a table. When I call the procedure from the Click event on the button of my form, I get an error message "Run-time error 2498 the expression you entered is the wrong data type for one of the arguments"


Here is my code:

Public Sub AddRec()
Dim frm As Form_frmEmployees

Set frm = Forms!frmEmployees

DoCmd.SetWarnings False

DoCmd.RunSQL "INSERT INTO Clients(First,Last)" & _
"SELECT" & frm.First & " As Expr1,'", frm.Last & " As Expr2;"

DoCmd.SetWarnings True

End Sub


Can anyone tell me whats wrong with my code?

thanks tony
 
It is usually beneficial to build a string to hold the SQL command. That way you can set a break point to view the SQL statement the way the command interpreter will see it. In this case the finished command should look something like this:

INSERT INTO Clients(First,Last) VALUES('Joe','Green');

To achieve that end you could use code something like this:
Code:
sCommand = "INSERT INTO Clients(First,Last)" & _
" VALUES('" & frm.First & "','" & frm.Last & "');"

DoCmd.RunSQL sCommand
 
You missed a few spaces and single-quotes...

[tt]
DoCmd.RunSQL "INSERT INTO Clients(First,Last)" & _
" SELECT '" & frm.First & "' As Expr1,'", frm.Last & "' As Expr2;"
[/tt]

*cLFlaVA
----------------------------
When will I be able to see what other members can see about myself? It's been like, a freakin' month already!
 
Hi Tony,

One thing you can do is to open Access and insert your SQL statement in the SQL view and attempt to run it. Access will usually highlight where the problem is.

Just a thought.

Thanks, Rib

Bartender:Hey aren't you that rope I threw out an hour ago?

Rope:No, I'm a frayed knot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top