How to Read Database Fields Into and Out of a List Box

Last reviewed: June 21, 1995
Article ID: Q112195
The information in this article applies to:

- Professional Edition of Microsoft Visual Basic for Windows, version 3.0

SUMMARY

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.

MORE INFORMATION

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)

This makes two columns in the list box. Field1 is separated from Field2 by the TAB character. You can use the TAB character to parse the columns back into separate fields. For example:

   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

For more information, please see the following article in the Microsoft Knowledge Base:

ARTICLE-ID: Q71067

TITLE     : How to Set Tab Stops in a List Box in Visual Basic

Step-by-Step Example

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.

  2. Place two Text Boxes (Text1 and Text2), a List Box (List1), and a data control (Data1) on Form1.

  3. Set the following properties of the Data Control:

       Property       Setting      Comment
       ---------------------------------------
       DatabaseName   BIBLIO.MDB   The sample db in the Visual Basic directory
       Recordsource   Authors      The Authors table is in BIBLIO.MDB
    
    

  4. Add the following code to the Form_Load event:

       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
    
    

  5. Add the following code to the Click event of List1:

       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
    
    

  6. Press the F5 key to run the program. Select an item in the List Box. The Author ID should be in Text1 and Author name should be in Text2.


Additional reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgDataAcc


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.