Solved SQL Append Syntax Error (1 Viewer)

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
I've been banging my head on my keyboard for some time of this piece of code

Code:
 QryAppend = "INSERT INTO TBL_Grape2 [( Grape )] " & _
             "values ( '" & GrapeSendStr & "' ) ;"

When it tries to run, I'm getting a "Syntax error in INSERT INTO statement". I'm sure it's something very simple but have yet to figure out exactly what
 

Minty

AWF VIP
Local time
Today, 19:30
Joined
Jul 26, 2013
Messages
10,368
Try
Code:
QryAppend = "INSERT INTO TBL_Grape2 ( Grape ) " & _
             "values ( '" & GrapeSendStr & "' ) ;"
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
Bingo :) thanks @Minty
 

Minty

AWF VIP
Local time
Today, 19:30
Joined
Jul 26, 2013
Messages
10,368
You are welcome - If your field name had spaces or was a reserved word you would need the square brackets around the field name;

QryAppend = "INSERT INTO TBL_Grape2 ( [Grape Name] ) " & _
"values ( '" & GrapeSendStr & "' ) ;"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:30
Joined
May 7, 2009
Messages
19,229
without using Any Delimeter, you can get away with:
Code:
 QryAppend = "INSERT INTO TBL_Grape2 ( Grape ) " & _
             "values p1;"
With Currentdb.CreateQueryDefs("", QryAppend)
    .Parameters("p1") = GrapeSendStr
    .Execute
End With
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
@Minty I'm very concious of avoiding the pitfalls of using spaces and reserved words in filed names :)

The query is part of a much larger procedure to normalise a table that until now I was happy being de-normalised. It has to copy grape names into a new table and separate fields that contain more than one grape name (separators being either "," or "&") and not create duplicates. The procedure has been running for a while now, Can't recall how many records there were (several hundred from memory). So hopefully I got all the logic right, and it produces the result I'm after :unsure:
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
Would have worked had I reversed the order of the parenthesis and square brackets :mad:
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
It's been running for over 30 minutes now, and whilst it is manipulating copies of the data in the live tables, I am a little nervous about what I'll see when it finally finishes :eek:
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
Clearly a deeper flaw in my logic. Back to the drawing board :sick:
 

Minty

AWF VIP
Local time
Today, 19:30
Joined
Jul 26, 2013
Messages
10,368
Can you post up some sample starting data, and what you want it to look like - we may be able to help.
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
Thanks @Minty it's currently a logic problem, that I will, continue to, tussle with.

Should I have no joy, I will certainly avail myself of your kind offer :)
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
OK the code seems to be working now but one small hitch, it crashes when it encounters a string that contains an apostrophe
for example Montepulciano d'Abruzzo.

Given that there is only one record that contains an apostrophe, the very simple (and obvious) solution is to edit the data prior to running the procedure and insert once it's finished.

However, it's piqued my curiosity as to how you could handle this with code :unsure:
 

Minty

AWF VIP
Local time
Today, 19:30
Joined
Jul 26, 2013
Messages
10,368
Arnel's code get's around that by passing in the parameter without the need for escaping it.
The other route is to use triple quotes;

QryAppend = "INSERT INTO TBL_Grape2 ( [Grape Name] ) values ( """ & GrapeSendStr & """ ) ;"

or literal double quotes

QryAppend = "INSERT INTO TBL_Grape2 ( [Grape Name] ) values ( " & chr(34) & GrapeSendStr & chr(34) & " ) ;"
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
@Minty (y) I'll post the full code once it's running to my satisfaction
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:30
Joined
May 7, 2009
Messages
19,229
Code:
QryAppend = "INSERT INTO TBL_Grape2 ( Grape ) " & _
             "values ( '" & Replace$(GrapeSendStr, "'", "''") & "' ) ;"
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
Ok the code seems to be doing it job of looping through the records, separating the discrete items and inserting them into a new table. That part is working just fine. Thanks peeps :love:

However, I'm using Dcount() prior to running the append query to test if the next value, to be added to the new table already exists there. This is however failing to detect duplicates, and I believe that this is because the new data is not being properly committed to the table until the whole procedure is completed. I reached this conclusion by inserting a MsgBox to report the item and count, and it always reports 0 (even if I have seen the same item reported numerous time). I know the Dcount() is structured correctly as it will report the correct count once the procedure has finished.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:30
Joined
May 7, 2009
Messages
19,229
you need to index on a field(s) with No duplicate so you won't need for dcount().
 

LanaR

Member
Local time
Tomorrow, 04:30
Joined
May 20, 2021
Messages
113
No that just causes a key violation error the first time the append tries to enter a duplicate. The duplicate needs to be detected before the query tires to append it
 

Users who are viewing this thread

Top Bottom