 |
| Access VBA Discuss using VBA for Access programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 11th, 2007, 07:43 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Jez
Dont pull your hair out, you need it!
Is it a Syntax Error in the VB Editor?!?! I have copied the code into VBA and it works fine here??
Do you mean its an SQL Syntax Error?
Rob
|
|

July 11th, 2007, 08:41 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Rob,
The error message is
RunTime Error -2147217900
Syntax error in INSERT INTO statement.
Not sure what the problem is as when I compile it in vba everything is fine.
Jez
|
|

July 11th, 2007, 08:54 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Jez,
It will do, Code in VBA and Commands via SQL are two completely diffrent things.
You need to double check the following:
â¢The Constant specifying the Database path is correct, and you have access to the file.
â¢All the fields in both the INSERT INTO header, and the SELECT body of the SQL are spelt correctly.
â¢Another thing worth trying is to add a breakpoint to the CurrentProject.Execute line (click the line
and press F9) and then Debug.Print sQRY in the Immediate Window (press CTRL+G if it is not open)
and then paste the code into a new query. Try to preview the code and it may give you a better indication
where its going wrong.
As always, if anything is unclear then please ask.
Regards,
Rob
|
|

July 11th, 2007, 09:29 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Rob,
I have always wondered how to do that debug.print Thanks!!
I put the break point in at the execute line and watched what the SQL printed. It prints it correct.
I then had a go at building the table in my front end DB and importing it there using this code below. used same SQL and it works perfectly. I cant understand how it doesnt work when sending it to the back end DB.
Private Sub cmdImport_Click()
DoCmd.SetWarnings False
strTempTable = "tblCSATAddressTEMP"
strFilePath = Me.txtFilePath
'Check that BusinessType has been selected- quit if not
If IsNull(Me.cboBusinessType) Or Me.cboBusinessType = "" Then
MsgBox "You must select a Business Type from the dropdown list", vbExclamation, cApplicationName
Exit Sub
End If
If VBA.Len(strFilePath) <> 0 Then
'Import the spreadsheet
DoCmd.TransferSpreadsheet acLink, , strTempTable, strFilePath, True
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = CurrentProject.Connection
Set rs = New ADODB.Recordset
'set rs to temporary table
rs.CursorLocation = adUseClient
rs.Open strTempTable, cnn
'insert new addresses into tblAddress
Dim sQRY As String
sQRY = _
"INSERT INTO tblCSATAddressA ( JobNumber, Address, ProjectID, Project, JobDate, TeamCode, Engineer, Contract, BusinessType ) " & _
"SELECT tblCSATAddressTEMP.[No], tblCSATAddressTEMP.Description, tblCSATAddressTEMP.[Bill-to Customer No], " & _
"tblCSATAddressTEMP.[Scheme Code], tblCSATAddressTEMP.[Planned Start Date], " & _
"tblCSATAddressTEMP.[Team Code], tblFamilyTree.Engineer, tblFamilyTree.Contract, '" & Me.cboBusinessType & "' AS BusinessType " & _
"FROM tblCSATAddressTEMP LEFT JOIN tblFamilyTree ON tblCSATAddressTEMP.[Team Code] = tblFamilyTree.TeamCode"
DoCmd.RunSQL sQRY
End If
DoCmd.DeleteObject acTable, strTempTable
MsgBox Me.cboBusinessType & " Data has been imported", vbExclamation, "eSHSL CSAT & Complaints Database"
Exit Sub
End Sub
I am totally confused!!
Jez
|
|

July 11th, 2007, 09:41 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Jez,
Why are you opening an ADO Recordset up?? You dont need to use it, and I don't think so, but
that may be interfering with the INSERT INTO statement. Remove the code:
Code:
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = CurrentProject.Connection
Set rs = New ADODB.Recordset
'set rs to temporary table
rs.CursorLocation = adUseClient
rs.Open strTempTable, cnn
Other than that I am pretty confused also, especially since you
said the code works fine in the Query Designer.
Try removing the above code and see if that helps?
Rob
|
|

July 11th, 2007, 09:54 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Rob,
Ok so if I take the Dim rs As ADODB.Recordset, I presumably need to take the Set rs = New ADODB.Recordset away too, but doing that I dont understand what I would need to use to replace the rs. on these lines
rs.CursorLocation = adUseClient
rs.Open strTempTable, CurrentProject.Connection
I thought that the imported table would be the recordset, or have I not understood?
Jez
|
|

July 11th, 2007, 10:34 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Jez,
You can remove all the code to do with the Recordset (as I gave above), you only need to use ADODB/DAO
Recordsets when programatically working with the DataSets, in your case, you are working with SQL commands
via the DoCmd Object, which does all the legwork for you.
You do not need to explicitly open the table, as the SQL Server (in this case Access) will handle all that for
you, you just tell it what you want it to do =)
Hope this helps,
Let me know if it does/doesn't work.
Rob
|
|

July 12th, 2007, 04:13 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Rob,
I have tried what you have said by taking out all recordset code, and re run this code, but still I am faced with the error message saying Syntax error in INSERT INTO statement.
The only thing I can think of is the bit where it says IN '" & cTables & "' " & vbCrLf & _
but when I debug.print sQRY it puts in the file path correctly, so I am baffled
Private Sub cmdImport_Click()
Dim cnn As ADODB.Connection
Dim sQRY As String
DoCmd.SetWarnings False
strTempTable = "tblCSATAddressTEMP"
strFilePath = Me.txtFilePath
'Check that BusinessType has been selected- quit if not
If IsNull(Me.cboBusinessType) Or Me.cboBusinessType = "" Then
MsgBox "You must select a Business Type from the dropdown list", vbExclamation, cApplicationName
Exit Sub
End If
If VBA.Len(strFilePath) <> 0 Then
'Import the spreadsheet
DoCmd.TransferSpreadsheet acLink, , strTempTable, strFilePath, True
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin; " & _
"Data Source=" & cTables
'insert new addresses into tblAddress
sQRY = _
"INSERT INTO tblCSATAddressA IN '" & cTables & "' " & vbCrLf & _
"( JobNumber, Address, ProjectID, Project, JobDate, TeamCode, Engineer, Contract, BusinessType )" & vbCrLf & _
"SELECT" & vbCrLf & _
"tblCSATAddressTEMP.[No]," & vbCrLf & _
"tblCSATAddressTEMP.Description," & vbCrLf & _
"tblCSATAddressTEMP.[Bill-to Customer No]," & vbCrLf & _
"tblCSATAddressTEMP.[Scheme Code]," & vbCrLf & _
"tblCSATAddressTEMP.[Planned Start Date]," & vbCrLf & _
"tblCSATAddressTEMP.[Team Code]," & vbCrLf & _
"tblFamilyTree.Engineer," & vbCrLf & _
"tblFamilyTree.Contract," & vbCrLf & _
"'" & Me.cboBusinessType & "' AS BusinessType" & vbCrLf & _
"FROM tblCSATAddressTEMP " & vbCrLf & _
"LEFT JOIN tblFamilyTree ON tblCSATAddressTEMP.[Team Code] = tblFamilyTree.TeamCode"
CurrentProject.Connection.Execute sQRY
End If
'DoCmd.DeleteObject acTable, strTempTable
MsgBox Me.cboBusinessType & " Data has been imported", vbExclamation, cApplicationName
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub
Jez
|
|

July 12th, 2007, 04:21 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Jez,
Can you please Debug.Print sQRY and copy and paste them to here please? Just so I can see
the actual SQL being passed to Access.
Rob
<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".
Thomas Jefferson</center>
|
|

July 12th, 2007, 04:27 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This is from the debug.print
debug.Print sQRY
INSERT INTO tblCSATAddressA IN 'Z:\CSITables.mdb'
( JobNumber, Address, ProjectID, Project, JobDate, TeamCode, Engineer, Contract, BusinessType )
SELECT
tblCSATAddressTEMP.[No],
tblCSATAddressTEMP.Description,
tblCSATAddressTEMP.[Bill-to Customer No],
tblCSATAddressTEMP.[Scheme Code],
tblCSATAddressTEMP.[Planned Start Date],
tblCSATAddressTEMP.[Team Code],
tblFamilyTree.Engineer,
tblFamilyTree.Contract,
'CHI' AS BusinessType
FROM tblCSATAddressTEMP
LEFT JOIN tblFamilyTree ON tblCSATAddressTEMP.[Team Code] = tblFamilyTree.TeamCode
|
|
 |