Syntax Error in Query Expression

jcruzAME

Registered User.
Local time
Today, 09:25
Joined
Oct 5, 2011
Messages
135
Here's the code I have:

Dim FullName As String
Dim Initials As String
Dim UserName As String
Dim Email As String
Dim ContactFileAs As String
Dim SplitName() As String
Dim InsertAdvisor As String
Dim FirstName As String
Dim LastName As String
Dim db As DAO.Database
Set db = CurrentDb()


FullName = txtFullName.Value
SplitName() = Split(FullName)
FirstName = SplitName(0)
LastName = SplitName(1)

ContactFileAs = SplitName(1) & ", " & SplitName(0)

InsertAdvisor = "INSERT INTO dbo_tblContact(FullName, ContactFileAs, FirstName, LastName, IsEmployee) VALUES(" & FullName & ", " & ContactFileAs & ", " & FirstName & ", " & LastName & ", -1)"

db.Execute (InsertAdvisor)


The text it's pulling from the txtFullName will be something like "John Smith". When it gives me this error, it will say Syntax error in query expression 'John Smith'.

Can anyone help me out here?
 
Just wondering why you're not using a bound form?

Plus your Split() will fail for names like:

Michael Peter Fox
 
Text data must be enclosed by single quotes, so your code should look like this


InsertAdvisor = "INSERT INTO dbo_tblContact(FullName, ContactFileAs, FirstName, LastName, IsEmployee) VALUES('" & FullName & "', '" & ContactFileAs & "', '" & FirstName & "', '" & LastName & "', -1)"
 
Geez, how slow am I? :p
 
I haven't had breakfast yet!! :D
 
You're welcome. Please let us know if the code runs as expected.
 
Thanks again everyone. That worked. I did what pbaldy pointed out.
 

Users who are viewing this thread

Back
Top Bottom