Results 1 to 4 of 4
  1. #1
    tobinjames is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2011
    Posts
    4

    Error when trying to export from table to Excel

    I have some VBA code that sits behind a button on a form. The code runs a query to insert results into a temporary table, export the data from the table to an Excel file with a defined worksheet name, and then delete the temporary table. The query itself uses parameters that are provided by the user from the form prior to them clicking on the button. When I click the button, I receive the following error: "Too few parameters. Expected 1."

    The VBA code I am using is as follows:

    Private Sub Command34_Click()
    On Error GoTo Err_Command34_Click

    Dim strSQL As String
    Dim strTable As String
    Dim strElementTable As String
    Dim strRemLifeFrom As String
    Dim strRemLifeTo As String
    Dim strContractor As String
    Dim strExcelFile As String
    Dim strWorkSheet As String

    strTable = "tblTemp"


    strElementTable = [Forms]![PROGFORM]![ElementTable]
    strRemLifeFrom = [Forms]![PROGFORM]![RemLifeFrom]
    strRemLifeTo = [Forms]![PROGFORM]![RemLifeTo]
    strContractor = [Forms]![PROGFORM]![Contractor]
    strExcelFile = "C:\Temp\ProgrammeExport\"
    strExcelFile = strExcelFile & strContractor & "_"
    strWorkSheet = strElementTable

    strSQL = "SELECT WorkPlanner.UPRN, WorkPlanner.[Block Ref], WorkPlanner.[Flat Number], WorkPlanner.[Block/House name], WorkPlanner.[House Number], WorkPlanner.[Street 1], WorkPlanner.[Street 2], WorkPlanner.Locality, WorkPlanner.Town, WorkPlanner.Postcode, "
    strSQL = strSQL & strElementTable & ".[Rem Life], " & strElementTable & ".[Rem Life Year], " & strElementTable & ".[Year Programmed] "
    strSQL = strSQL & "INTO " & strTable
    strSQL = strSQL & " FROM " & strElementTable & " INNER JOIN WorkPlanner ON " & strElementTable & ".UPRN = WorkPlanner.UPRN "
    strSQL = strSQL & "WHERE WorkPlanner.UPRN IN (SELECT UPRN FROM " & strElementTable & " WHERE (Contractor = " & strContractor & " AND ([Rem Life] BETWEEN " & strRemLifeFrom & " AND " & strRemLifeTo & ")))"

    CurrentDb.Execute strSQL

    DoCmd.TransferSpreadsheet acExport, 8, strTable, strExcelFile, True, strWorkSheet

    DoCmd.DeleteObject acTable, strTable

    Exit_Command34_Click:
    Exit Sub

    Err_Command34_Click:
    MsgBox Err.Description
    Resume Exit_Command34_Click

    End Sub


    I'm not sure what this error means or what part of my code is failing.

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Is Contractor text or numeric? If text you need quotes. Also, is Rem Life a date? If so, you need octothorpes (#).

    Also, for this code:

    strExcelFile = strExcelFile & strContractor & "_"

    you need to provide the file extention if it isn't already there in strContractor, which I doubt it would be:

    strExcelFile = strExcelFile & strContractor & "xls"


  3. #3
    tobinjames is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2011
    Posts
    4
    Hi Bob,

    Thanks for your advice. I realised once I had posted the code that I had missed out the file extension.

    The Contractor value is text, but I'm not sure where I need to put the quotes and whether they should be single or double quotes. Sorry for seeming a bit thick, but could you advise? Rem Life is a numeric value so I'm assuming this is ok as it is?

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    The quotes for Contractor would be like this:

    strSQL = strSQL & "WHERE WorkPlanner.UPRN IN (SELECT UPRN FROM " & strElementTable & " WHERE (Contractor = " & Chr(34) & strContractor & Chr(34) & " AND ([Rem Life] BETWEEN " & strRemLifeFrom & " AND " & strRemLifeTo & ")))"


    I like to use Chr(34) which is a double quote because it is easier to read than three double quotes and/or using single quotes which can be a problem for things like Bob's Contracting where the name has a single quote in it.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 10-07-2011, 07:49 AM
  2. Export table to excel using excel template
    By allenjasonbrown@gmail.com in forum Programming
    Replies: 1
    Last Post: 06-27-2011, 02:10 AM
  3. Macro export to excel error
    By Andy_d in forum Import/Export Data
    Replies: 7
    Last Post: 04-15-2011, 09:54 AM
  4. Export Pivot table to Excel Error
    By Whalen16 in forum Import/Export Data
    Replies: 3
    Last Post: 09-30-2010, 12:54 PM
  5. Export Access Pivot Table to Excel
    By graviz in forum Programming
    Replies: 1
    Last Post: 11-13-2009, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums