Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old July 11th, 2007, 07:43 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Reply With Quote
  #12 (permalink)  
Old July 11th, 2007, 08:41 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #13 (permalink)  
Old July 11th, 2007, 08:54 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Reply With Quote
  #14 (permalink)  
Old July 11th, 2007, 09:29 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #15 (permalink)  
Old July 11th, 2007, 09:41 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Reply With Quote
  #16 (permalink)  
Old July 11th, 2007, 09:54 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
  #17 (permalink)  
Old July 11th, 2007, 10:34 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Reply With Quote
  #18 (permalink)  
Old July 12th, 2007, 04:13 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
  #19 (permalink)  
Old July 12th, 2007, 04:21 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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>
Reply With Quote
  #20 (permalink)  
Old July 12th, 2007, 04:27 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Error: Microsoft Jet Engine asters ASP.NET 1.0 and 1.1 Professional 2 April 3rd, 2007 09:33 AM
Microsoft JET Database Engine error '80040e14' aprnip Classic ASP Databases 2 November 15th, 2004 02:06 AM
Microsoft JET Database Engine error '80040e07' aprnip Classic ASP Databases 1 October 22nd, 2004 07:19 AM
Trying to find the Microsoft JET OLEDB download marthaj Access ASP 3 September 23rd, 2003 11:53 PM
Microsoft JET Database Engine error '80040e10' nvillare Classic ASP Databases 1 August 20th, 2003 12:09 PM



All times are GMT -4. The time now is 03:14 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.