|
-
September 2nd, 2008, 03:55 AM
#1
listbox two flds
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
-
September 2nd, 2008, 05:56 AM
#2
Re: listbox two flds
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
-
September 3rd, 2008, 02:38 AM
#3
Re: listbox two flds
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?
-
September 3rd, 2008, 07:34 AM
#4
Re: listbox two flds
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
-
September 3rd, 2008, 06:58 PM
#5
Re: listbox two flds
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
-
September 4th, 2008, 07:24 AM
#6
Re: listbox two flds
 Originally Posted by Bezzie
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?
both are string and fix, first element to return to textbox
-
September 4th, 2008, 08:44 AM
#7
Re: listbox two flds
Select your recordset (RS)
Populate your Listbox:
Code:
Do While Not RS.EOF
List1.AddItem RS("Field1") & Chr(9) & RS("Field2") 'the fields in your recordset separated with TAB character
RS.MoveNext Loop
BTW: Can someone tell me how to resize this Code blocks to show more code!
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
Last edited by Bezzie; September 4th, 2008 at 08:50 AM.
-
September 4th, 2008, 12:47 PM
#8
Re: listbox two flds
 Originally Posted by Bezzie
Select your recordset (RS)
Populate your Listbox:
Code:
Do While Not RS.EOF
List1.AddItem RS("Field1") & Chr(9) & RS("Field2") 'the fields in your recordset separated with TAB character
RS.MoveNext Loop
BTW: Can someone tell me how to resize this Code blocks to show more code!
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
Did you try my code? It sets the tab stops!
-
May 19th, 2009, 03:19 AM
#9
Re: listbox two flds [resolve]
thanks all gurus, i've use the split(.......) and works fine for me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|