Solved

Code to populate a calendar table with date specific information

Posted on 2004-03-26
14
860 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:girls_kick_ass_hard
14 Comments
 
LVL 17

Accepted Solution

by:
walterecook earned 63 total points
ID: 10688698
Personally I'd say why bother.
You can have a table that lists all the days, but there really isn't any need to populate the other stuff as you can look calculate it as necessary.  And I'm not really sure why you'd need to do this on an ongoing basis as that could be done by query too.
If you were running a report for this month this query would provide the dates.  You could then put criteria on month and year:
SELECT yourTable.SomeDate, Month([somedate]) AS YourMonth, Year([somedate]) AS YourYear, DateAdd("d",(7-Weekday(DateValue([SomeDate]))),[SomeDate]) AS EndOfWeek
FROM yourTable;

Make sense?
Walt

0
 
LVL 17

Expert Comment

by:walterecook
ID: 10688707
This query  is then the source of your dates, you can left join that to your data table.

Walt
0
 
LVL 4

Expert Comment

by:goliak
ID: 10688774
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.

     
0
 
LVL 4

Expert Comment

by:goliak
ID: 10688775
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.

     
0
 
LVL 4

Expert Comment

by:goliak
ID: 10688777
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.

     
0
 
LVL 4

Expert Comment

by:goliak
ID: 10688778
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.

     
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 4

Expert Comment

by:goliak
ID: 10688782
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.

     
0
 
LVL 4

Expert Comment

by:goliak
ID: 10688785
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.

     
0
 
LVL 11

Expert Comment

by:phileoca
ID: 10689720
goliak. u having trouble posting?
0
 

Author Comment

by:girls_kick_ass_hard
ID: 10692022
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.
0
 
LVL 4

Expert Comment

by:goliak
ID: 10711920
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
0
 
LVL 4

Assisted Solution

by:goliak
goliak earned 62 total points
ID: 10711929
P.S. Sorry for trashed your question, something had happened to my connection. :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now