SQL SYNTAX ERROR

D

DS

I'm trying to insert all records that meet a certain criteria and I'm
getting an error.
Whats wrong with my syntax?
Thanks
DS

Dim NewItemSQL
NewItemSQL = "INSERT * INTO tblCheckDetailsTMP " & _
"SELECT * FROM tblCheckDetailsTMP " & _
"WHERE tblCheckDetailsTMP.CDCheckID=Forms!frmFXTransferSelect!TxtCheckID " &
_
"AND tblCheckDetailsTMP.CDGroupID = Forms!frmFXTransferItem!TxtGroupID;"
DoCmd.RunSQL (NewItemSQL)
 
A

Allen Browne

Try concatenating the values from the text boxes into the string, like this:

Dim NewItemSQL As String

If IsNull(Me.TxtCheckID) Or IsNull(Me.TxtGroupID) Then
MsgBox "Fill in both"
Else
NewItemSQL = "INSERT * INTO tblCheckDetailsTMP " & _
"SELECT * FROM tblCheckDetailsTMP " & _
"WHERE ((tblCheckDetailsTMP.CDCheckID = " & _
Me.TxtCheckID & _
") AND (tblCheckDetailsTMP.CDGroupID = " & _
Me.TxtGroupID & "));"

dbEngine(0)(0).Execute NewItemSQL, dbFailOnError
End If

Notes:
====
1. Testing for Null prevents a mal-formed string.

2. If CDCheckID or CDGroupID is a Text field (not a Number field), you need
extra quotes. See:
Quotation marks within quotes
at:
http://allenbrowne.com/casu-17.html

3. Using Execute instead of RunSQL gives more info if something goes wrong.
See:
Action queries: suppressing dialogs, while knowing results
at:
http://allenbrowne.com/ser-60.html

4. If it still fails, add the line:
Debug.Print NewItemSQL
just before the Execute. You can then copy that from the Immediate Window
(Ctrl+G), paste into SQL View in a query, and see what's not right.
 
D

Dirk Goldgar

DS said:
I'm trying to insert all records that meet a certain criteria and I'm
getting an error.
Whats wrong with my syntax?
Thanks
DS

Dim NewItemSQL
NewItemSQL = "INSERT * INTO tblCheckDetailsTMP " & _
"SELECT * FROM tblCheckDetailsTMP " & _
"WHERE tblCheckDetailsTMP.CDCheckID=Forms!frmFXTransferSelect!TxtCheckID "
& _
"AND tblCheckDetailsTMP.CDGroupID = Forms!frmFXTransferItem!TxtGroupID;"
DoCmd.RunSQL (NewItemSQL)


Why are you inserting into and selecting from the same table?
 
D

DS

Because I need to create new records using the same info, is that bad or
incorrect to do?
Thanks
DS
 
D

Dirk Goldgar

DS said:
Because I need to create new records using the same info, is that bad or
incorrect to do?


No, it's fine if that's your intention. I just didn't know if it was a
mistake or not. Note that, as it stands, there's no visible way to tell the
difference between the records you duplicated and their originals. If
that's not an issue, and there's no unique index on the table that would be
violated by the duplication, then there's nothing wrong with what you're
doing.
 
D

DS

Your're right I've decided to make a holding table and adjust from there,
then place the records.
Thanks
DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Select From 1
SELECT INSERT 1
Is This Right? 3
OR or AND 2
UNION Error 9
SQL Other Database 2
SQL ORDER BY 1
Function Returns 0 3

Top