ACC: How to Fill a Multiple-Column List Box Using Code

Last reviewed: November 10, 1997
Article ID: Q95903
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article demonstrates a technique to fill a list box or a combo box with values by setting the RowSourceType() function to the name of a custom procedure.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0

MORE INFORMATION

The following example uses a list box control on a form, but you can substitute a combo box with the same results. In order to create a multi- column list box you must set Case 4 in the Select Case statement to the number of columns you want in your list box. In Case 6 of the Select Case statement, define the data that you want to display in each column and row of the list box.

  1. Open an existing database or create a new one.

  2. Create a module and type the following line in the Declarations section:

          Option Explicit
    

  3. Type the following procedure:

    '===================================================================

          ' The following function uses a Select Case statement to fill a
          ' two-column and four-row list box. The function fills the first
          ' column of the list box with the dates of the next four Mondays. The
          ' second column is filled with the dates of the next four Tuesdays.
    
    '===================================================================

          Function ListMonTuesdays(fld As Control, id, row, col, code)
    
             Dim offset
             Select Case Code
                Case 0                          'Initialize.
                   ListMonTuesdays=True
                Case 1                          'Open.
                   ListMonTuesdays=id           'Unique ID number for control
                Case 3                          'Number of rows.
                   ListMonTuesdays=4
                Case 4                          'Number of columns.
                   ListMonTuesdays=2
                Case 5                          'Column width.
                   ListMonTuesdays=-1           'Use default width.
    
     '===================================================================
          ' In the next Case statement:
          '
          ' Offset is the formula for finding the next four Mondays.
          ' If column=0, then fill in with the dates for the next four
          ' Mondays in column 1. If column=1, then fill in with the dates
          ' for the next four Tuesdays in column 2.
     '===================================================================
                Case 6                          'Get Date
                   Offset=abs((9-Weekday(Now))Mod 7)
                      If col=0 then
                         ListMonTuesdays=Format(Now()+offset+7*row,"mmmm d")
                      Else
                         Offset=abs((10-Weekday(Now))Mod 7)
                         ListMonTuesdays=Format(Now()+offset+7*row,"mmmm d")
                      End if
             End Select
          End Function
    
    

  4. Create a new form in Design view.

  5. Add a list box control to the detail section of the form:

          List Box:
    
             Name: DisplayDates
             RowSourceType: ListMonTuesdays
    
    

  6. Switch the form to Form view and note that the list box displays two columns of dates.

REFERENCES

For more information about the RowSourceType property of list boxes and combo boxes, type "rowsourcetype" in the Office Assistant, click Search, and then click to view "RowSourceType Property."

Keywords          : kbusage PgmObj FmsCmbo
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


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: November 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.