Results 1 to 2 of 2
  1. #1
    nyneave is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2012
    Posts
    24

    Creating excel spreadsheet from access vba

    I am using Access 2003 vba to create a spreadsheet with variable tab names. Below is the code I am using. I am using the same code on another button with different data and it works just fine; the only variance between the 2 code sets is that the one that works has an integer value instead of text (where highlighted red below). When I run the code below - I get a pop up asking me for a value for "General" (which is one of the categories). If I look at the temporary query that was created in Access, it has the criteria as [General] instead of "General" - which explains the parameter request; but I don't understand why it is doing that.

    Any help would be much appreciated!

    Private Sub Command0_Click()

    Dim qdf As DAO.QueryDef
    Dim dbs As DAO.Database
    Dim rstDisc As DAO.Recordset
    Dim strSQL As String, strTemp As String, strDisc As String
    Const strFileName As String = "Name of File"
    Const strQName As String = "zExportQueryDis"
    Stop
    Set dbs = CurrentDb
    strTemp = dbs.TableDefs(0).Name
    strSQL = "SELECT * FROM [" & strTemp & "] WHERE 1=0;"
    Set qdf = dbs.CreateQueryDef(strQName, strSQL)
    qdf.Close


    strTemp = strQName
    Stop
    strSQL = "SELECT DISTINCT CategoryFROM DescType;"
    Set rstDisc = dbs.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
    Stop
    If rstDisc.EOF = False And rstDisc.BOF = False Then
    rstDisc.MoveFirst
    Do While rstDisc.EOF = False

    strDisc = rstDisc!Category.Value

    strSQL = "SELECT * FROM qryMemberData WHERE [Date_Submitted] between #" & Me.txtBeginDate & "# AND #" & Me.txtEndDate & "# AND Category = " & rstDisc!Category.Value & ";"

    Set qdf = dbs.QueryDefs(strTemp)

    qdf.Name = strDisc

    strTemp = qdf.Name

    qdf.SQL = strSQL
    qdf.Close
    Set qdf = Nothing
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strTemp, strFileName & ".xls"
    rstDisc.MoveNext
    Loop
    End If
    rstDisc.Close
    Set rstDisc = Nothing
    dbs.QueryDefs.Delete strTemp
    dbs.Close
    Set dbs = Nothing

    MsgBox "Description Form Created", vbInformation
    Cancel = True

    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    # AND Category = '" & rstDisc!Category.Value & "';"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. How to import excel spreadsheet into an Access subform
    By upfish in forum Import/Export Data
    Replies: 1
    Last Post: 10-03-2012, 01:53 PM
  2. Open Excel spreadsheet in Access
    By carlyd in forum Forms
    Replies: 1
    Last Post: 02-17-2012, 01:09 PM
  3. Replies: 1
    Last Post: 02-02-2012, 08:55 AM
  4. Email Excel Spreadsheet from Access
    By Nancy in forum Access
    Replies: 2
    Last Post: 11-09-2010, 02:37 PM
  5. Replies: 2
    Last Post: 02-19-2010, 08:05 PM

Tags for this Thread

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