Solved

Code to populate a calendar table with date specific information

Posted on 2004-03-26
14
862 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

911 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

25 Experts available now in Live!

Get 1:1 Help Now