Microsoft KB Archive/210583

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

Article ID: 210583

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210583



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

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



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.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example uses a list box control on a form, but you can substitute a combo box with the same results. To create a multi-column list box, you must set Case 4 in the Select Case statement of the sample 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 or paste 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  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">