Row Source Type function to populate a list Ms Access Gurus

VBA > Control > Row Source Type Callback Function

Use a custom function to populate the rows of a list box or combo box.

Quick Jump

Benefits

Advantages of using a custom Row Source Type function include

  1. Not limited by the size of the RowSource property
  2. Contents are automatically updated as they need to change
  3. Great for data that depends on Windows, such as day and and month names in the language specified.

Goto Top  

video tutorials

video: RowSourceType custom function (12:33)

YouTube: RowSourceType Callback Function in Microsoft Access

YouTube: CalendarMaker presentation to Access User Groups, hosted by Dale Fye (49:05)

YouTube: CalendarMaker presentation to Access User Groups

Example

Specify a custom function in the RowSourceType. This function must have particlar parameters, that can be named whatever you like. Here is an example setting on the property sheet, and the declaration of a callback function in the code behind the form.

use a callback function in the Row Source Type property for a combo box or list box

CalendarMaker form with a listbox using a callback function in the RowSourceType so day names are displayed in the language specified in the Region settings for Windows.

Menu form showing listbox with RowSourceType custom function

This code and example came from a free tool. Download the CalendarMaker, to make calendars with Access for any start day of the week, with day and month names in your language, and that shows data you want from your database on each day.

Goto Top  

Code

'*************** Code Start *****************************************************  
' download:  
'  http://msaccessgurus.com/VBA/Code/ctl_RowSourceTypeFx.htm  
'-------------------------------------------------------------------------------  
' Purpose  : Get day names in any language for custom function in RowSourceType   
' Author   : crystal (strive4peace)  
' License  : below code  
' Code List: www.msaccessgurus.com/code.htm  
'-------------------------------------------------------------------------------  
'           GetMyDayNames  
'-------------------------------------------------------------------------------  
Private Function GetMyDayNames( _
   pControl As Control _
   , pvID As Variant _
   , pnRowNumber As Long _
   , pnColNumber As Long _
   , piCode As Integer _
   ) As Variant
'191207,18 strive4peace 200223, 25, 27  
' This list-filling callback function is specified in the  
'  RowSourceType property of the FirstDay listbox control  
'     Programmatically specify items while the application runs.  
'     If Windows language changes, day names will automatically change  
'     Use as alternative to AddItem, which also needs to be triggered.  
'        Another advantage is that large data (which this doesn't have)  
'        won't run be limited by the size of the RowSource property  
' Thanks to  
'     Gustav Brock (Access MVP) for the order of occurrence  
'     Shane Groff (Microsoft) for explanations that aren't documented  
   'cases are in order of execution  
   '  EXCEPT acLBGetValue (6) is at top since it is called the most  
   Select Case piCode ' Type of information Access Is requesting
   Case acLBGetValue '=6 at top since this Is executed more times
      'Value displayed in row and column  
      If pnColNumber = 0 Then
         'Row/Column Index starts with 0. FirstDay for functions starts with 1, so +1  
         'first column - day number.  
          GetMyDayNames = pnRowNumber + 1
      Else
         'second column - day name  
         GetMyDayNames = WeekdayName(pnRowNumber + 1, False, vbSunday)
      End If
   Case acLBInitialize '=0
      ' 0 if function can fill the list, executes once  
       GetMyDayNames = True
        
   'not used here 2 = Open as variant. Alternative to acLBOpen  
   ' Shane prepare for external dependency such as query  
   '     not as implemented, so be careful if used  
   Case acLBOpen '=1
       GetMyDayNames = Timer 'unique identifier
       
   'defined in control, not needed  
   '4 = acLBGetColumnCount -- Number of columns  
   '5 = acLBGetColumnWidth -- Width (in twips) of pnColNumber  
   Case acLBGetRowCount '=3
      '7 rows, one for each day of the week  
       GetMyDayNames = 7
       'You can also specify -1, which means not known  
       '    this speeds process for large data so it doesn't have to be calculated  
       '    side effect vertical scrollbar won't be right till you scroll to end  
       
   'not needed  
   '7 = acLBGetFormat -- Format string to be used for column  
   '8 = acLBClose -- not used  
   '9 = acLBEnd -- when done -- ie clean up  
   End Select
End Function

'   You may freely use and share this code  
'     provided this license notice and comment lines are not changed;  
'     code may be modified provided you clearly note your changes.  
'   You may not sell this code alone, or as part of a collection,  
'     without my handwritten permission.  
'   All ownership rights reserved. Use at your own risk.  
'   ~ crystal (strive4peace)  www.msaccessgurus.com  
'*************** Code End *******************************************************  

Goto Top  

Logic

Put a callback function name in the RowSourceType property of a combo or list box, with no equal sign before, and no parentheses after.

Callback Function Parameters

Parameter   Example Name   Data Type  
1 pControl Control
2 pvID Variant
3 pnRowNumber Long
4 pnColNumber Long
5 piCode Integer

Goto Top  

Download

Click HERE to download the zipped TXT file containing the code for a custom Row Source Type function.
(2 kb, unzips to a TXT file)

License

This code may be used freely, but you may not sell it in whole or in part. You may include it in applications you develop for others.

Goto Top  

Reference

RowSourceType property

Docs / Office VBA Reference / Access / Object model / ListBox object / Properties / RowSourceType

Help: ListBox.RowSourceType property

RowSource property (Access)

Docs / Office VBA Reference / Access / Object model / ListBox object / Properties / RowSource

Help: ListBox.RowSource property (Access)

WeekdayName function

Docs / Language reference / Reference / Functions / WeekdayName

Help: WeekdayName function

Timer function

Docs / Language reference / Reference / Functions / Timer

Help: Timer function

Goto Top  

Backstory

Sometimes using this method to populate rows of a list is the only way it makes sense to do. I wasn't sure if semi-colon was used in all languages to delimit a list, and looked for a way to figure out what was used so I could make a value list. But this way works! and has other benefits.

Special thanks to Gustav Brock, Shane Groff, Daniel Pineault, Arvin Meyer, and Wayne Phillips

Goto Top  

Share with others

here's the link to copy:
http://msaccessgurus.com/VBA/Code/ctl_RowSourceTypeFx.htm

Share your comments

Let's communicate, collaborate, and appreciate ... we all get better by sharing. Email me anytime at info@msAccessGurus.com. I enjoy hearing from Access users and developers.

Do you want help?

Do you have a project that could benefit from another set of eyes? Let's connect and team-develop, and build your application together. ~ crystal

Goto Top