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

ID: Q95903

The information in this article applies to:

SUMMARY

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

This article shows you how to fill a list box or a combo box with values by setting the RowSourceType property of the list box or combo box 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 in the code in step 3 to the number of columns that 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

   if it is not already there:

      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."

Additional query words:

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

Last Reviewed: November 21, 1998