Solved Help with syntax error (1 Viewer)

duke217

Registered User.
Local time
Today, 21:37
Joined
Jan 23, 2018
Messages
17
Hi all,

I am getting a syntax error when I try to execute this piece of code:
Code:
DoCmd.RunSQL "INSERT INTO tblLogins " _
            & "(lkpUser, lstAppName, lstLogType, lkpWorkstation, chrPubIP, chrLocIP, chrLogInLocation, chrLoginRegion, chrLoginCountry, chrTimeZone, chrTimeStampUTC, chrSSID) VALUES )" _
            & "(" & chrUserLogin & ", 'In', " & chrWorkstation & ", " & chrPublicIP & ", " & chrLocalIP & ", " & chrCurrentGeoLoc & ", " & chrGeoAera & ", " & chrCountry & ", " & lngUtcOffset & ", " & Now() & ", " & chrServiceSet & ");"

Can anybody help? It probably has to do with separators or parens, but my eyes are tired, I just can't figure it out

Thanks a lot
 

Minty

AWF VIP
Local time
Today, 20:37
Joined
Jul 26, 2013
Messages
10,368
Think you have to add some # around your date field;

SQL:
dim strSQL as string

strSQL = "INSERT INTO tblLogins "
strSQL = strsql & " (lkpUser, lstAppName, lstLogType, lkpWorkstation, chrPubIP, chrLocIP, chrLogInLocation, chrLoginRegion, chrLoginCountry, chrTimeZone, chrTimeStampUTC, chrSSID) "
strSQL = strsql & "    VALUES ("  & chrUserLogin & ", 'In', " & chrWorkstation & ", " & chrPublicIP & ", " & chrLocalIP & ", " & chrCurrentGeoLoc & ", " & chrGeoAera & ", " & chrCountry & ", " & lngUtcOffset & ", #" & Now() & "#, " & chrServiceSet & "); "
debug.Print strSQL

DoCmd.RunSQL strSQL
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:37
Joined
Jul 9, 2003
Messages
16,272
Can anybody help? It probably has to do with separators or parens, but my eyes are tired, I just can't figure it out

You are not the only one! This is a recurring issue on AWF. I call it "The Notorious Insert Into SQL Statement" and I have blogged about it on my website here.


Minty has hit the nail on the head and given you a workable solution. However I strongly recommend you have a look at my blog where I offer a much simpler way of avoiding this syntax problem.

My method keeps the insert into SQL statement as a simple string which is an identical, repeatable sequence, which makes very easy to edit and manipulate if you have to add any extra Fields.

Then the delimiters for dates and text are added to a variable which is then placed within the insert into SQL string.

By dividing and conquering, by simplifying the process into logical easily understandable steps, you will always get your insert into SQL statement correct every time.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 20:37
Joined
Sep 21, 2011
Messages
14,237
Plus you should put all that into a string variable, then you can Debug.Print it and see where the error is?

Didi you notice Minty did exactly that?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:37
Joined
Feb 19, 2013
Messages
16,609
suspect you have not provided delimiters for text. for example perhaps this

& ", " & chrCountry & ", " &

should be

& ", '" & chrCountry & "', " &

if that's the case you wont get a syntax error, but you will get a runtime error
 

onur_can

Active member
Local time
Today, 12:37
Joined
Oct 4, 2015
Messages
180
Hi all,

I am getting a syntax error when I try to execute this piece of code:
Code:
DoCmd.RunSQL "INSERT INTO tblLogins " _
            & "(lkpUser, lstAppName, lstLogType, lkpWorkstation, chrPubIP, chrLocIP, chrLogInLocation, chrLoginRegion, chrLoginCountry, chrTimeZone, chrTimeStampUTC, chrSSID) VALUES )" _
            & "(" & chrUserLogin & ", 'In', " & chrWorkstation & ", " & chrPublicIP & ", " & chrLocalIP & ", " & chrCurrentGeoLoc & ", " & chrGeoAera & ", " & chrCountry & ", " & lngUtcOffset & ", " & Now() & ", " & chrServiceSet & ");"

Can anybody help? It probably has to do with separators or parens, but my eyes are tired, I just can't figure it out

Thanks a lot
VALUES )
Do you think the red brackets are correct?
Minty seems to have fixed this issue.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:37
Joined
May 21, 2018
Messages
8,525
Everything you need to make this super simple. Never have to worry about delimiters again. Look at the functions to make parameter queries and insert queries. Pass in the controls and get a formatted insert query or simply use the paramater query.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:37
Joined
May 21, 2018
Messages
8,525
To demonstrate how easy this module makes things. Here is a complex insert test case. In real life you would likely use controls and not variables
Code:
Public Sub TestInsertQryValues()
  Dim flds As String
  Dim Vals As String
  Dim strSqlInsert As String
  Dim lngInStock
   'demoing a number literal and number in a variable
  lngInStock = 2500
  flds = "'FirstName', 'LastName', 'OrderID', 'OrderDate', 'OrderQuantity', 'InStock'"
  Vals = CreateInsertQueryValues(CSql("Mike"), CSql("O'Brien"), Null, CSql(Date, sdt_date), 100, lngInStock)
  Debug.Print Vals
  strSqlInsert = CreateInsertQuery("MyTable", flds, Vals, "LastName = " & CSql("jones"))
  Debug.Print strSqlInsert
End Sub

Result

INSERT INTO MyTable('FirstName', 'LastName', 'OrderID', 'OrderDate', 'OrderQuantity', 'InStock') VALUES ('Mike', 'O''Brien', NULL, #12/18/2020#, 100, 2500) WHERE LastName = 'jones'

using my module
Code:
  Dim flds As String
  Dim Vals As String
  Dim strSqlInsert As String
 
 flds = "'lkpUser', 'lstAppName', 'lstLogType', 'lkpWorkstation', 'chrPubIP', 'chrLocIP', 'chrLogInLocation', 'chrLoginRegion', 'chrLoginCountry', 'chrTimeZone', 'chrTimeStampUTC', 'chrSSID'"
 
 vals = CreateInsertQueryValues(CSql(chrUserLogin), CSql('In'), CSql(chrWorkstation ),CSql(chrPublicIP),... CSql(chrServiceSet))
 
  strSqlInsert = CreateInsertQuery("tblLogins", flds, Vals)
  currentdb.execute strSqlInsert
 
Last edited:

Users who are viewing this thread

Top Bottom