Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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

↑ Back to the top


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.

↑ Back to the top


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

↑ Back to the top


References

For more information about the RowSourceType property of list boxes andcombo boxes, click Microsoft Access Help on the Help menu, type rowsourcetype, rowsource properties in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB210583, kbusage, kbhowto

↑ Back to the top

Article Info
Article ID : 210583
Revision : 2
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 352