Home > other >  Use the result of a loop statement as a combobox's rowsource
Use the result of a loop statement as a combobox's rowsource

Time:01-08

I am trying to make the rowsource of a combobox the results of a loop statement.

Dim intDay As Date

For intDay = BegDate To EndDate
    If Format(intDay, "ddd") = "Sat" Then
    Debug.Print intDay
    End If
Next intDay

The loop returns all Saturdays between a period. The dates are in a table field named WDATE so I wanted BegDate and EndDate to be something like Min(WDATE) and Max(WDATE)

CodePudding user response:

Never check literal weekdays or monthnames as these are localised.

Your loop could look:

Dim WeekdayDate As Date

For WeekdayDate = BegDate To EndDate
    If Weekday(WeekdayDate) = vbSaturday Then
        Debug.Print WeekdayDate
    End If
Next

However, such combo/listboxes can be better build using a little known gem of Access: Callback functions.

Study both simple and extended examples in my project:

VBA.Callback.

The last example covers your case (and a bit more).

  •  Tags:  
  • Related