Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding more than one column to list box?

Status
Not open for further replies.

AccessDevJunior

Programmer
Apr 14, 2004
81
GB
hya,
i have a listbox and would like it display 4 columns rather the 1 column it current displays, i have this code in place, im not sure how to add more columns:

Do Until RS.EOF
ListUser.AddItem RS("Employee Email Address")
RS.MoveNext
Loop

the other 3 columns are 'employee tel number, employee mob number' employee name'

if anyone could show me how to add these extra columns to this code it would be a great help?
 
There are 3 easy alternatives.

1. A VB listbox control only has one column. You can fake it by using a Tab character as a separator and get the appearance of multi-column.

2. Use a Listbox from the Microsoft Forms set

3. Probably best - use a Listview control

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
The ListBox control doesn't support different columns. You can fake it by changing the listbox Font to a fixed font (Courier New for example) and then:
Code:
Listuser.AddItem _
    Left$(rs![Employee Email Address] & Space(20),20) & _
    Left$(rs!['employee tel number] & Space(12),12) & _
    Left$(cStr(rs![employee mob number])) & Space(6), 6) & _
    Left$(rs![employee name] & space(30),30 )

I have made some assumptions about field lengths.

You will of course need to write some code to extract the specific values when you are pulling information from the ListBox.

The more elegant solution is to use a disconnected ADO recordset as the datasource for an ADO DataGrid. That's a bit more work but produces a much more pleasing result.
 
Use tabstops

Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_SETTABSTOPS = &H192

Sub SetTabStops(ListBox As ListBox, ParamArray tabStops())
    Dim numEls As Long, tabs() As Long, i As Long
    
    numEls = UBound(tabStops) + 1
    
    ReDim tabs(0 To numEls - 1)
    tabs(0) = tabStops(0)
    For i = 1 To numEls - 1
        tabs(i) = tabs(i - 1) + Abs(tabStops(i))
        If tabStops(i) < 0 Then tabs(i) = -tabs(i) ' right aligned tabs are negative
    Next
    
    SendMessage ListBox.hwnd, LB_SETTABSTOPS, numEls, tabs(0)
End Sub

Private Sub Form_Load()
    Dim i As Long

    
    SetTabStops Me.List1, 50, 30, -40
    
    For i = 1 To 7
        Me.List1.AddItem WeekdayName(i) _
            & vbTab & WeekdayName(i, True) _
            & vbTab & "/" _
            & vbTab & 10 ^ i
    Next
End Sub

Now, if someone can tell me how to do this with a combo box, that will sort my problem.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top