?
Solved

Code to populate a calendar table with date specific information

Posted on 2004-03-26
14
Medium Priority
?
867 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 17

Accepted Solution

by:
walterecook earned 252 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 248 total points
ID: 10711929
P.S. Sorry for trashed your question, something had happened to my connection. :)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

801 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