Solved

Code to populate a calendar table with date specific information

Posted on 2004-03-26
14
863 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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
 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

815 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

11 Experts available now in Live!

Get 1:1 Help Now