How to Read Database Fields Into and Out of a List BoxID: Q112195
|
The list box that comes with Visual Basic is not bound, but you can simulate a bound list box in a Visual Basic program. Visual Basic can read records from a database placing the values from each individual field within the record into columns in a list box, which can then be extracted by the Visual Basic program.
By reading each field into the list box and separating each field from the
next with a TAB character, you can create the illusion of columns.
NOTE: By using the SendMessage Windows API function and the LB_SETTABSTOPS
constant, you can set the size of your tab stops within your listbox to
create custom spacing between fields.
Here's an example:
List1.AddItem Data1.Recordset(Field1) & Chr$(9) & Data1.Recordset(Field2)
Dim X As Integer
X = InStr(List1.Text, Chr$(9))
Text1 = Mid$(List1.Text, 1, X - 1) ' Will Contain Field1
Text2 = Mid$(List1.Text, X + 1, (Len(List1.Text) - X)) ' Contains Field2
Q71067 : How to Set Tab Stops in a List Box in Visual Basic
Property Setting Comment
---------------------------------------
DatabaseName BIBLIO.MDB The sample db in the Visual Basic directory
Recordsource Authors The Authors table is in BIBLIO.MDB
Sub Form_Load()
Data1.Refresh
' Loop until you reach the last record:
Do Until Data1.Recordset.EOF
' Load the list box with fields separated with a tab:
' Enter the following two lines as one, single line:
List1.AddItem Data1.Recordset("Au_Id") & Chr$(9) &
Data1.Recordset("Author")
Data1.Recordset.MoveNext
Loop
' Initialize list box and text boxes to first item:
List1.ListIndex = 0
End Sub
Sub List1_Click()
Dim X As Integer
' Find first tab character:
X = InStr(List1.Text, Chr$(9))
' Put all characters before tab into Text1:
Text1 = Mid$(List1.Text, 1, X - 1)
' Put all characters after tab into Text2:
Text2 = Mid$(List1.Text, X + 1, (Len(List1.Text) - X))
End Sub
Additional query words: 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 18, 1999