Results 1 to 8 of 8
  1. #1
    LUTINO is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    7

    how to make columns in ListBox

    Dear All,

    I googled how to populate list box (with columns) but could not succeed..

    Situation:
    I have a recordset with 3 fields. I want to populate listbox (add Columns) in following style. (Dashes or - is just to maintain symtry)
    ID------Name--------AGE
    1-----Alice-------------22
    2-----Chris-------------21


    3------Asha------------23

    But I am getting results like this

    1
    Alice
    22
    2
    Chris
    21
    3
    Asha
    23


    I just simplified above mentioned scenioro for understanding..actual code is following.

    Dim Sql_Search As String
    Dim RowStr As String



    Sql_Search = "select tbl_Company.cmp_id , tbl_Company.cmp_id_char,tbl_Company.cmp_short_name , tbl_Company.cmp_full_name, tbl_Company.cmp_address, tbl_Company.cmp_city , tbl_Company.cmp_fone, tbl_Company.cmp_fax, tbl_Company.cmp_limit, tbl_Company.cmp_status, tbl_Company.cmp_purchaser, tbl_Company.cmp_Purchaser_mobile, tbl_Company.cmp_accounts, tbl_Company.cmp_accounts_mobile from Tbl_company where tbl_company.cmp_id_char = '" & Txt_Cmp_Char & "' "

    Set Rst = Dbs.OpenRecordset(Sql_Search)



    If (Rst.RecordCount <> 0) Then
    Do While Not Rst.EOF
    ' show Fields in 1 row
    RowStr = Rst.Fields(0).Value & " " & Rst.Fields(1).Value & " " & Rst.Fields(2).Value & " " & Rst.Fields(3).Value
    Lst_Search.AddItem (RowStr)
    Rst.MoveNext

    Loop
    End If




    End Sub
    Attached Thumbnails Attached Thumbnails IMG_20160620_204129.jpg  

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    In the listbox propery,
    set COLUMNS to 3
    set COLUMWIDTHS to 0;1;0.5

  3. #3
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Besides setting the Column Count and Column Widths you need some correction in the following expression in your Code:
    RowStr = Rst.Fields(0).Value & " " & Rst.Fields(1).Value & " " & Rst.Fields(2).Value & " " & Rst.Fields(3).Value
    Add a semicolon ( between column items. The column count property value will ensure that each line of items is in group of 3.
    Code:
    Set Rst = Dbs.OpenRecordset(Sql_Search)
        
    
    
    If (Rst.RecordCount <> 0) Then
              Do While Not Rst.EOF
                     ' show Fields in 1 row 
                    RowStr = RowStr & Rst.Fields(0).Value & ";" &  Rst.Fields(1).Value & ";" & Rst.Fields(2).Value & ";"  & Rst.Fields(3).Value & ";"
                    Lst_Search.AddItem (RowStr)
                    Rst.MoveNext
               
               Loop
    End If
    'Next statement removes the extra semicolon at the end of the string
    RowStr = Left(RowStr,Len(RowStr)-1) 
    
    'Replace Combobox with your combo box name
    Me.Combobox.RowSource = RowStr 
    Me.Combobox.ReQuery
    End Sub
    Last edited by apr pillai; 09-07-2016 at 10:48 AM. Reason: corrections to Code

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Why the VBA code? All you need is a query...

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by ssanfu View Post
    Why the VBA code?...
    Because this is the programming forum.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by ItsMe View Post
    Because this is the programming forum.
    Now THAT's funny..... ROTFLMAO


    Thanks!! I needed that.

  7. #7
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Corrections:

    Code:
    Set Rst = Dbs.OpenRecordset(Sql_Search)
        
    
    Lst_Search.RowSourceType = "Value List"
    If (Rst.RecordCount <> 0) Then
              Do While Not Rst.EOF
                     ' show Fields in 1 row 
                    RowStr = RowStr & Rst.Fields(0).Value & ";" &  Rst.Fields(1).Value & ";" & Rst.Fields(2).Value & ";"  & Rst.Fields(3).Value & ";"
               'Lst_Search.AddItem RowStr
                    Rst.MoveNext
               
               Loop
            rst.close
    End If
    'Next statement removes the extra semicolon at the end of the string
    RowStr = Left(RowStr,Len(RowStr)-1) 
    
    Me.Lst_Search.RowSource = RowStr 
    Me.Lst_Search.ReQuery
    End Sub
    OR

    Code:
    Set Rst = Dbs.OpenRecordset(Sql_Search)
        
    
    Lst_Search.RowSourceType = "Value List"
    If (Rst.RecordCount <> 0) Then
              Do While Not Rst.EOF
                     ' show Fields in 1 row 
                    RowStr = Rst.Fields(0).Value & ";" &  Rst.Fields(1).Value & ";" & Rst.Fields(2).Value & ";"  & Rst.Fields(3).Value
                    Lst_Search.AddItem RowStr
                    Rst.MoveNext
               
               Loop
             rst.close
    End If
    Me.Lst_Search.ReQuery
    End Sub

  8. #8
    LUTINO is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2016
    Posts
    7
    Thank you Mr. APR PiLLAI & all team..
    My problem solved.

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

Similar Threads

  1. Listbox adjustable columns
    By chook in forum Access
    Replies: 4
    Last Post: 03-07-2013, 03:41 AM
  2. Replies: 2
    Last Post: 12-06-2011, 01:02 AM
  3. Replies: 2
    Last Post: 08-12-2011, 01:01 PM
  4. Replies: 6
    Last Post: 03-27-2010, 11:18 AM
  5. Columns in a Listbox
    By craigalaniz in forum Access
    Replies: 3
    Last Post: 01-07-2010, 01:11 PM

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