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)
Loop
End Sub

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

Thanks

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
Loop
End Sub

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)

If txtFrom.Text = vbNullString Then 'Must have a from date
    txtFrom.Text = MonthView1.Value
        Else
    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.

That's what I was looking for. Thanks so much

Only a pleasure...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.