Results 1 to 3 of 3
  1. #1
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65

    VBA Command to Display Columns in Listbox

    I’m trying to display data in multiple columns in a listbox on a form and am running into trouble with an “Object Required” error message. My listbox is set to Value List, Column Count of 2, and I’ve assigned appropriate widths to the columns (through the property sheet). I think I’m not correctly referencing the columns. The code I’m using is shown below.



    I’ve checked that the recordset is querying correctly and the “With lstParks” code works correctly if I remove the reference to specific columns (which unfortunately I do need).


    Any ideas?
    Thanks.


    Code:
       Set rs = db.OpenRecordset("Select * from [tbl_parks] " & _
            "where tbl_records_objectives.UNIQUE_SECTION_LINK = " & ConservationAreaID & "")
            
      'Display NAME in first column of list box
         With lstParks.Column(0)
           Do Until rs.EOF
            .AddItem rs("NAME")
            rs.MoveNext
           Loop
         End With
    
    'Display LOCATION in second column of listbox
         With lstParks.Column(1)
           Do Until rs.EOF
            .AddItem rs("LOCATION")
            rs.MoveNext
           Loop
         End With

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    You don't add columns to listboxes in Access like how you are trying. You need to add them at the same time (and it will need to be set to use VALUE LIST if it hasn't already been set):

    Code:
    Set rs = db.OpenRecordset("Select * from [tbl_parks] " & _ 
    "where tbl_records_objectives.UNIQUE_SECTION_LINK = " & ConservationAreaID) 
     
    With lstParks 
     
    .RowSourceType = "Value List"
     
    Do Until rs.EOF 
    .AddItem rs("NAME") & ";" & rs("LOCATION") 
    rs.MoveNext 
    Loop
     
    End With
    Last edited by boblarson; 08-12-2011 at 12:36 PM. Reason: remove formatting code that stayed from copying.

  3. #3
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    Got it! Thank you so much!

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

Similar Threads

  1. Display 2 Columns in Combo Box?
    By 10 Gauge in forum Forms
    Replies: 3
    Last Post: 04-12-2011, 10:24 AM
  2. Replies: 3
    Last Post: 02-01-2011, 09:47 AM
  3. Replies: 9
    Last Post: 01-20-2011, 02:22 PM
  4. Columns in a Listbox
    By craigalaniz in forum Access
    Replies: 3
    Last Post: 01-07-2010, 01:11 PM
  5. Replies: 0
    Last Post: 12-14-2009, 09:57 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