Simple little exercise using the MonthView Date Picker. I'm trying to loop through print all dates from dateclicked to end of month. I'm getting stuck on the comparison value to define the end of the month. Keep getting the "argument not optional error" or and infinite loop.

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
Print DateClicked
Do While Month = Month(DateClicked)
Print DateAdd("d", 1, DateClicked)
End Sub

The problem is in the Do While line. How do I define the first value for comparison?


6 Years
Discussion Span
Last Post by AndreRet

I personally find the loop prone to many errors. This is what I should have done.

To a form add a Listbox - lstDate, 2 Textboxes - txtFrom and txtTo, a Monthviewer - MonthViewer1 and a commandbutton - Command1. Add the following code to the code section -

Private Sub Command1_Click()

ListDatesBetween txtFrom.Text, txtTo.Text, lstDates 'Calling the function
End Sub

Private Sub ListDatesBetween(ByVal LowerDate As Date, ByVal UpperDate As Date, ByRef ListBox As ListBox)

Do Until LowerDate > UpperDate 'Catch when end reached
    ListBox.AddItem LowerDate 'Add each day
    LowerDate = LowerDate + 1 'Get the next day
End Sub

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)

If txtFrom.Text = vbNullString Then 'Must have a from date
    txtFrom.Text = MonthView1.Value
    txtTo.Text = MonthView1.Value 'Add the last day
End If
End Sub

Another method would be to get the actual first/last day of the month and use that as your To date. I have attached a sample on how to get the first and last day of the month. With this you can now add a loop function to print all the dates in between.

I hope this helps towards solving your problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.