Microsoft KB Archive/95903

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

Article ID: 95903

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q95903



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



SUMMARY
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 &quot;Building Applications with Microsoft Access&quot; 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 &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; 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.

 Open an existing database or create a new one. Create a module and type the following line in the Declarations section if it is not already there:

Option Explicit

  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,&quot;mmmm d&quot;) Else Offset=abs((10-Weekday(Now))Mod 7) ListMonTuesdays=Format(Now+offset+7*row,&quot;mmmm d&quot;) End if        End Select End Function  Create a new form in Design view.  Add a list box control to the detail section of the form:      List Box: Name: DisplayDates RowSourceType: ListMonTuesdays  Switch the form to Form view and note that the list box displays two columns of dates.</li></ol>

<div class="references_section">