Results 1 to 4 of 4
  1. #1
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162

    Export listbox to Excel - listbox shows null value in immediate window

    Hi all,



    Im trying to export the results of a VBA query which populates a listbox to Excel. If I use DoCmd.TransferSpreadsheet acExport I get the error invalid use of null but I can see there is data in the listbox. Any ideas please - code below:

    Code:
    Private Sub ExportTotal_btn_Click()
    Dim strSQL As String
    Dim strQry As String
    Dim sQryOutput As String
    sQryOutput = Me.TotalHrs_Listbox.Value
    strSQL = "SELECT * FROM sQryOutput"
    strQry = "TempQueryName"
     
    Set db = CurrentDb
    Set qdf = db.CreateQueryDef(strQry, strSQL)
     
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, _
       strQry, "D:\Folder\output.xlsx", True
     
    DoCmd.DeleteObject acQuery, strQry
    End Sub
    Thanks

  2. #2
    kevinnice is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2012
    Posts
    10
    I'm no expert here Nick but you can't use a variable as a table name without the correct syntax:
    '" & sQryOutput & "'
    Didnt try it, but hope that helps...
    The big brains here will surely give the definitive answer...
    best of luck

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Is this a multi-select listbox and you are trying to export the selected items?

    I don't think can reference a listbox as datasource for a query. Queries are to manipulate data of tables and other queries, not data controls on form. The items selected can be used as criteria in an sql statement. This article shows are to build the WHERE clause from selected items http://allenbrowne.com/ser-50.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Got it working with

    Code:
    Private Sub Exprt2Excel_btn_Click()
        Dim i As Integer
        Dim n As Integer
        Dim strLine As String
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set a = fs.CreateTextFile("Export_location\Export.csv", True)
        For i = 0 To Me.Control_Listbox.ListCount - 1
        
            For n = 0 To Me.Control_Listbox.ColumnCount - 1
            
                strLine = strLine & """" & Me.Control_Listbox.Column(n, i) & ""","
                
            Next n
            
            strLine = Left(strLine, Len(strLine) - 1)
            
            a.writeline (strLine)
            
            strLine = ""
            
        Next i
        
        MsgBox "Your file is exported"
    End Sub
    Thanks for all your help

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

Similar Threads

  1. Export Filtered ListBox output to a Excel Sheet
    By gokul1242 in forum Programming
    Replies: 4
    Last Post: 10-11-2012, 02:19 PM
  2. Replies: 1
    Last Post: 09-10-2012, 11:21 PM
  3. Replies: 1
    Last Post: 07-26-2012, 11:45 AM
  4. Replies: 7
    Last Post: 06-05-2012, 03:22 PM
  5. Exporting report selected from a listbox to excel
    By GARCHDEA in forum Import/Export Data
    Replies: 1
    Last Post: 08-10-2010, 07:45 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