Insert into syntax error (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 03:00
Joined
Oct 14, 2019
Messages
463
2 days I've stared at this:
Code:
Private Sub GroceryListTable()
Dim db As DAO.Database
Dim rst As Recordset
Dim frm As Form
Dim strSQL As String
Set db = CurrentDb
Set frm = Me!frmCalendarWeek.Form!fsubGrocery.Form
Set rst = frm.RecordsetClone
strSQL = "INSERT INTO tempGroceryList (PrefixII, Location, GroceryName) " & _
        "VALUES (" & rst![PrefixII] & "," & rst![Location] & "," & rst![GroceryName] & ")"

CurrentDb.Execute "DELETE FROM tempGroceryList"
 
 rst.MoveFirst
        While Not rst.EOF
            CurrentDb.Execute strSQL, dbFailOnError
rst.MoveNext
Wend
Debug returns:
INSERT INTO tempGroceryList (PrefixII, Location, GroceryName) VALUES (F-1,Meat,hamburger)
The error is: too few parameters; expected 3
Or:
INSERT INTO tempGroceryList (PrefixII, Location, GroceryName) VALUES (F-1,Meat,flank steak)
syntax error. Missing operator in flank steak
Can you tell me what I'm missing?
 

June7

AWF VIP
Local time
Today, 02:00
Joined
Mar 9, 2014
Messages
5,472
Need apostrophe delimiters for text field parameter.
Code:
strSQL = "INSERT INTO tempGroceryList (PrefixII, Location, GroceryName) " & _
        "VALUES ('" & rst![PrefixII] & "','" & rst![Location] & "','" & rst![GroceryName] & "')"
Date/time field parameter needs # as delimiter.
 

ClaraBarton

Registered User.
Local time
Today, 03:00
Joined
Oct 14, 2019
Messages
463
Thank you so much... Now I'm getting a table full of the same record... It doesn't movenext
 

Minty

AWF VIP
Local time
Today, 11:00
Joined
Jul 26, 2013
Messages
10,371
Your strSQL needs to be inside your recordset loop for that to work unless you are only returning one record.

You aren't changing it when you move through the recordset.
 

ClaraBarton

Registered User.
Local time
Today, 03:00
Joined
Oct 14, 2019
Messages
463
Code:
rst.MoveFirst
 strSQL = "INSERT INTO tempGroceryList (PrefixII, Location, GroceryName) " & _
        "VALUES ('" & rst![PrefixII] & "','" & rst![Location] & "','" & rst![GroceryName] & "')"
        While Not rst.EOF
            CurrentDb.Execute strSQL, dbFailOnError
rst.MoveNext
Wend

???? How do I do that?
 

ClaraBarton

Registered User.
Local time
Today, 03:00
Joined
Oct 14, 2019
Messages
463
Code:
rst.MoveFirst
        While Not rst.EOF
         strSQL = "INSERT INTO tempGroceryList (PrefixII, Location, GroceryName) " & _
        "VALUES ('" & rst![PrefixII] & "','" & rst![Location] & "','" & rst![GroceryName] & "')"
    CurrentDb.Execute strSQL, dbFailOnError
rst.MoveNext
Wend
IT WORKS!!! Thank you so much!
 

Josef P.

Well-known member
Local time
Today, 12:00
Joined
Feb 2, 2023
Messages
826
You could also think about replacing the loop with a single SQL statement.
Code:
strSQL = "INSERT INTO tempGroceryList (PrefixII, Location, GroceryName) " & _
        "select PrefixII, Location, GroceryName from <Source Of Me!frmCalendarWeek.Form!fsubGrocery.Form>"
 

Users who are viewing this thread

Top Bottom