+ Reply to Thread
Results 1 to 5 of 5

VBA: Listbox with 1 record

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2009
    Location
    toronto, canada
    MS-Off Ver
    Excel 2003
    Posts
    2

    VBA: Listbox with 1 record

    In some cases an Access DB query will return only one record (by design).

    When this happens, and only when this happens, my listbox displays each of the values in a seperate row instead of just on one row.

    i.e. My query will return 8 values per record. If there is only 1 record returned, the listbox shows each value on a seperate row instead of on one row in 8 columns.

    The listbox works as intended when there are 2 or more records. I am now stuck. Pardon the ugly code. "questionaires" is the name of the listbox. "record_array" is the variant array containing the records.
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    
    Dim record_array As Variant
    
    
    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Data Source=reporting_tool_db.mdb;"
        .Open
    End With
    
    With rs
        .Open "SELECT Business_Unit,Inquiry_Type,Rec_Date,Comp_Date,Author,Hours,Path,ID FROM Questionaire WHERE [Client_Name] = '" & selected_client & "'", cn
        record_array = .GetRows
        .Close
    End With
    
    cn.Close
    
    With Me.questionaires
        .Clear
        .list = Application.Transpose(record_array)
        .ListIndex = -1
        .ColumnWidths = "1.5 in, 0.7 in, 1 in, 0.8 in, 0.8 in, 0.7 in, 0.1 in"
    End With
    Any help/suggestions are appreciated, thanks!
    Last edited by hendre; 07-24-2009 at 04:14 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: VBA: Listbox with 1 record

    Try to add this code:
    Me.questionaires.ColumnCount = <fieldNum>
    Regards,
    Antonio

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: VBA: Listbox with 1 record

    See this thread (as long as you are not using a Decimal data type!)
    Remember what the dormouse said
    Feed your head

  4. #4
    Forum Contributor
    Join Date
    04-01-2009
    Location
    Irvine, CA
    MS-Off Ver
    Excel 2010
    Posts
    280

    Re: VBA: Listbox with 1 record

    You could try adding a dummy record and then deleting it from the listbox...

    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    
    Dim record_array As Variant
    
    
    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Data Source=reporting_tool_db.mdb;"
        .Open
    End With
    
    With rs
        .Open "SELECT '','','','','','','' FROM Questionaire UNION " & _
                 "SELECT Business_Unit,Inquiry_Type,Rec_Date,Comp_Date,Author,Hours,Path,ID " & _
                 "FROM Questionaire WHERE [Client_Name] = '" & selected_client & "'", cn
        record_array = .GetRows
        .Close
    End With
    
    cn.Close
    
    With Me.questionaires
        .Clear
        .list = Application.Transpose(record_array)
        .RemoveItem (0)    .ListIndex = -1
        .ColumnWidths = "1.5 in, 0.7 in, 1 in, 0.8 in, 0.8 in, 0.7 in, 0.1 in"
    End With
    The first SELECT statement must contain the same number of fields as the second for the UNION to work and by using REMOVEITEM(0) you'll always remove that first (empty) record.

  5. #5
    Registered User
    Join Date
    07-23-2009
    Location
    toronto, canada
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: VBA: Listbox with 1 record

    romperstomper's solution did it!! thanks so much, I've been battling this for a while.

    thanks for everyone's input, still a bit of a noob to VBA!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1