Link to home
Start Free TrialLog in
Avatar of girls_kick_ass_hard
girls_kick_ass_hard

asked on

Code to populate a calendar table with date specific information

Hi,

I need to create a command button when clicked will take the calendar month enter in a textbox and populate a calendar table for me.  This table is called CalendarDates and is used in some reports where a listing of all the days in the month are needed.  The textbox will contain a date for the first day of that month (ie 03/01/2004 = March 01 2004).

The table is structured to have the fields:
Date (03/01/2004)
Month (03)
Year (2004)
Week's End date (03/06/2004)  Last day of the week which the current record's date follows into

I don't have a clue how to do this.  Can someone please help out?

Thank you for your help.

PS.
ASKER CERTIFIED SOLUTION
Avatar of walterecook
walterecook
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This query  is then the source of your dates, you can left join that to your data table.

Walt
Avatar of goliak
goliak

Your table doesn't need to have Month and Year fields.
And here is the procedure which will paste entered values in your table and calculate the last day of the week:

Private Sub Button0_Click()
Dim Dt As Date
Dim i As Integer
Dt = CDate(TxtBox1.Value)
For i = 0 To 6
    If Weekday(DateAdd("d", i, Dt)) = vbSaturday Then
        CurrentDb.Execute "INSERT INTO CalendarDates (fldDate, WeekEndDate) VALUES (#" & Dt & "#, #" & DateAdd("d", i, Dt) & "#)"
        End
    End If
Next i
End Sub

Ask for the further instructions.

     
Your table doesn't need to have Month and Year fields.
And here is the procedure which will paste entered values in your table and calculate the last day of the week:

Private Sub Button0_Click()
Dim Dt As Date
Dim i As Integer
Dt = CDate(TxtBox1.Value)
For i = 0 To 6
    If Weekday(DateAdd("d", i, Dt)) = vbSaturday Then
        CurrentDb.Execute "INSERT INTO CalendarDates (fldDate, WeekEndDate) VALUES (#" & Dt & "#, #" & DateAdd("d", i, Dt) & "#)"
        End
    End If
Next i
End Sub

Ask for the further instructions.

     
Your table doesn't need to have Month and Year fields.
And here is the procedure which will paste entered values in your table and calculate the last day of the week:

Private Sub Button0_Click()
Dim Dt As Date
Dim i As Integer
Dt = CDate(TxtBox1.Value)
For i = 0 To 6
    If Weekday(DateAdd("d", i, Dt)) = vbSaturday Then
        CurrentDb.Execute "INSERT INTO CalendarDates (fldDate, WeekEndDate) VALUES (#" & Dt & "#, #" & DateAdd("d", i, Dt) & "#)"
        End
    End If
Next i
End Sub

Ask for the further instructions.

     
Your table doesn't need to have Month and Year fields.
And here is the procedure which will paste entered values in your table and calculate the last day of the week:

Private Sub Button0_Click()
Dim Dt As Date
Dim i As Integer
Dt = CDate(TxtBox1.Value)
For i = 0 To 6
    If Weekday(DateAdd("d", i, Dt)) = vbSaturday Then
        CurrentDb.Execute "INSERT INTO CalendarDates (fldDate, WeekEndDate) VALUES (#" & Dt & "#, #" & DateAdd("d", i, Dt) & "#)"
        End
    End If
Next i
End Sub

Ask for the further instructions.

     
Your table doesn't need to have Month and Year fields.
And here is the procedure which will paste entered values in your table and calculate the last day of the week:

Private Sub Button0_Click()
Dim Dt As Date
Dim i As Integer
Dt = CDate(TxtBox1.Value)
For i = 0 To 6
    If Weekday(DateAdd("d", i, Dt)) = vbSaturday Then
        CurrentDb.Execute "INSERT INTO CalendarDates (fldDate, WeekEndDate) VALUES (#" & Dt & "#, #" & DateAdd("d", i, Dt) & "#)"
        End
    End If
Next i
End Sub

Ask for the further instructions.

     
Your table doesn't need to have Month and Year fields.
And here is the procedure which will paste entered values in your table and calculate the last day of the week:

Private Sub Button0_Click()
Dim Dt As Date
Dim i As Integer
Dt = CDate(TxtBox1.Value)
For i = 0 To 6
    If Weekday(DateAdd("d", i, Dt)) = vbSaturday Then
        CurrentDb.Execute "INSERT INTO CalendarDates (fldDate, WeekEndDate) VALUES (#" & Dt & "#, #" & DateAdd("d", i, Dt) & "#)"
        End
    End If
Next i
End Sub

Ask for the further instructions.

     
goliak. u having trouble posting?
Avatar of girls_kick_ass_hard

ASKER

Hi,

Thanks for the code goliak, this is close to what I need but it only inserts one record into the table.  I need to insert one record for each day in the month with the appropriate weekend dates for each day.  Can the code be expanded to do this?

Walt, I need to the table due to the use of a query (two distinct tables into one query using union) and the nature of the report.  Your query gives me ideas though to modify goliak code though.  Thanks.

PS.
Expanded code.

Private Sub Button0_Click()
Dim Dt As Date
Dim i As Integer
Dim curMonth As Integer
Dim dt2 As Date
Dt = "01.01.2004"
curMonth = Month(Dt)
For j = 0 To 31
    dt2 = DateAdd("d", j, Dt)
    If Month(dt2) = curMonth Then
        For i = 0 To 6
            If Weekday(DateAdd("d", i, dt2)) = vbSaturday Then
                CurrentDb.Execute "INSERT INTO CalendarDates" & _
                "(fldDate, WeekEndDate) " & _
                "VALUES (#" & Replace(dt2, ".", "/") & "#, #" & _
                Replace(DateAdd("d", i, dt2), ".", "/") & "#)"
            End If
        Next i
    Else
        End
    End If
Next j
End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial