i need to use listbox, but i need it to display 2 fields extracted from table.
when i click the listbox it should only take the 1 field to store in a textbox.
any suggestion
tks
cyrus
Printable View
i need to use listbox, but i need it to display 2 fields extracted from table.
when i click the listbox it should only take the 1 field to store in a textbox.
any suggestion
tks
cyrus
To get the selected Item from the Listbox into the Textbox, all you need is :
Code:Private Sub Command1_Click()
Text1.Text = List1.List(List1.ListIndex)
End Sub
What type of data will be in the two fields? Fixed or variable length?
Do you want the first or the second field returned into textbox?
Ideally the data you want to add to the List Box is reasonably predictable, otherwise you will end up with a ListBox not quite lined up
To separate 2 fields you should use a special character like a TAB
So If Field1 is A1234 and Field2 is Widgets, by doing
ListBox1.AddItem Field1 & Chr(9) & Field2 you will get an entry in the listbox looking approximately like
A1234 Widgets
By doing as Hannes has suggested, you can extract the value into either a Textbox or a string, then strip out the left and right values (which have been split by the tab)
This is done using the SPLIT Command
Example
Code:Public LineData As String
Public LineVar() As String
Public Sub SplitStringOnTab
LineData = ListBox1.Text 'the Item Clicked in the ListBox which has 2 fields separated by the TAB
LineVar = Split(LineData, CHR(9)) 'this splits the line which is separated by the TAB (CHR(9)
Field1 = LineVar(0)
Field2 = LineVar(1)
End Sub
Better idea. Use an API:
Code:Option Explicit
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
Private Sub Form_Load()
ReDim TabStop(0 To 2) As Long
'assign some values to the tabs for the second third and fourth
'column (the first is flush against the listbox edge)
TabStop(0) = 90
TabStop(1) = 130
TabStop(2) = 185
'clear then set the tabs
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(0))
List1.AddItem "Big" & Chr(9) & "Brown" & Chr(9) & "Dog"
List1.AddItem "Brown" & Chr(9) & "Big" & Chr(9) & "Dog"
List1.AddItem "Dog" & Chr(9) & "Brown" & Chr(9) & "Big"
List1.Refresh
End Sub
Quote:
Originally Posted by Bezzie
both are string and fix, first element to return to textbox
Select your recordset (RS)
Populate your Listbox:
BTW: Can someone tell me how to resize this Code blocks to show more code!Code:Do While Not RS.EOF
List1.AddItem RS("Field1") & Chr(9) & RS("Field2") 'the fields in your recordset separated with TAB characterLoop
RS.MoveNext
In List1.Click event extract the part you want
Code:Text1.Text = Left(List1.List(List1.ListIndex),10) 'extract the number of chars of field1
Quote:
Originally Posted by Bezzie
Did you try my code? It sets the tab stops!
thanks all gurus, i've use the split(.......) and works fine for me