473,382 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Looking through date in SQL

Jon
I have a form that is to be used to populate a table (tblBookings) of future
Bookings for a chosen weekday - that is selected by a choice of Checkbox's
(so they can have one or more days).

The user also chooses what year, the dates will be every chosen weekday for
that year, (from 1/1 if not this year, from today if this year).

I need to create a routine for each Weekday, but I can't figure all of the
looping and weekday bits!

So far I have:

Dim MonSQL As String
'Monday
If Me.Monday.Value = True Then
'populate all the entries
MonSQL = "INSERT INTO tblBookings ( CollectionDate, CollectionTime ) VALUES
(" _
& "#" _
& ThisWillBeTheCalculatedDateBit _
& "#" _
& ", '" & Me.CollectionTime _
& "' );"
DoCmd.RunSQL MonSQL
End If

Can anyone either advise on the correct way to loop through this and choose,
for this bit, the Mondays.

Or is there a better way to do this?

In anticipation, many thanks

Jon
Nov 13 '05 #1
3 1500
whoa... I think you're going about this all wrong. This is about as
hard as falling down.

Fields on your form:
txtStartDate (formatted as a short date)
txtEndDate (ditto)
'if you want every day, use 1, if you want weekly, use 7.

Sub CreateDates(byval intNumDays)
dim dtTemp as Date
dim rs as dao.recordset

set
rs=dbengine(0)(0).openrecordset("tblBookings",dbAp pendOnly)
for dtTemp=cdate(txtStartDate) to cdate(txtEndDate) Step
intNumDays
rs.addnew
rs.fields("CollectionDate")=dtTemp
rs.update
next dtTemp

rs.close
set rs=nothing

end sub

as you can tell from the lowercase reserved words, I didn't test a bit
of this. But you're making your life much more difficult than it needs
to be. I never use RunSQL, because the one time I did in a loop, it
was a disaster.

hope this gets you started.

Nov 13 '05 #2
Jon wrote:
I have a form that is to be used to populate a table (tblBookings) of future
Bookings for a chosen weekday - that is selected by a choice of Checkbox's
(so they can have one or more days).

The user also chooses what year, the dates will be every chosen weekday for
that year, (from 1/1 if not this year, from today if this year).

I need to create a routine for each Weekday, but I can't figure all of the
looping and weekday bits!

So far I have:

Dim MonSQL As String
'Monday
If Me.Monday.Value = True Then
'populate all the entries
MonSQL = "INSERT INTO tblBookings ( CollectionDate, CollectionTime ) VALUES
(" _
& "#" _
& ThisWillBeTheCalculatedDateBit _
& "#" _
& ", '" & Me.CollectionTime _
& "' );"
DoCmd.RunSQL MonSQL
End If

Can anyone either advise on the correct way to loop through this and choose,
for this bit, the Mondays.

Or is there a better way to do this?

In anticipation, many thanks

Jon


You can cut and paste the following code into a module. Change the year
(intYear) and day (strDay) and run it. Look in the debug window for the
results. If acceptable, change strDay and intYear method to use the
values from the input form, comment out the Debug.Print, and uncomment
the method to add the records.

Sub CheckDate()
Dim intYear As Integer 'year entered
Dim intDay As Integer 'acSunday to acSaturday value
Dim intWeekday As Integer
Dim strDay As String 'day selected
Dim datHold As Date

'change these values for testing
strDay = "Tuesday" 'this will be from checkbox of day selected
intYear = 2007 'year selected by operator

Select Case strDay
Case "Sunday"
intDay = 1 'acSunday value...and so on
Case "Monday"
intDay = 2
Case "Tuesday"
intDay = 3
Case "Wednesday"
intDay = 4
Case "Thursday"
intDay = 5
Case "Friday"
intDay = 6
Case "Saturday"
intDay = 7
End Select

If intYear = Year(Date) Then
'get current date
datHold = Date
Else
'get the first of the year
datHold = DateSerial(intYear, 1, 1)
End If

'determine the day of the week respective of the day to use
intWeekday = WeekDay(datHold, intDay)

If intWeekday <> 1 Then
datHold = datHold + (8 - intWeekday)
End If

'what is the calculated date?
'see if the day and date match. comment out if acceptable
MsgBox Format(datHold, "Long Date")

'now run subroutine to insert dates
If Year(datHold) = intYear Then
'it's possible someone wants to add dates for this year
'around Christmas time and the next date for day specified
'would go into next year and that would not be correct

'uncomment out if msgbox above is acceptable
'AddDatesToTable datHold, intYear
End If

End Sub
Private Function AddDatesToTable(datLoop As Date, intYear As Integer)
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblBookings", dbOpenDynaset)

Do While Year(datLoop) = intYear
'use debug to see which days will be inserted. Comment
'out debug line if not in test mode
Debug.Print datLoop

'update the record. uncomment to update
'rst.AddNew
'rst!CollectionDate = datLoop
'rst!CollectionTime = Me.CollectionTime
'rst.Update

'now add 7 days and loop
datLoop = datLoop + 7

Loop
rst.Close
Set rst = Nothing
End Function
Nov 13 '05 #3
Jon
Thank you both VERY much for the time taken to respond, I'm re-writing the
code right now!!!

Jon
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
Hello, I'm looking to create a PHP script that will automatically generate an index/menu/list (whatever) based on the PDF files that are within a particular directory. I would like the script...
12
by: Daniela Nii | last post by:
Hi there, I am looking for a javascript function that someone might have already written to calculate the paid-time-off. Given are the start and end date and time. I need to calculate the...
13
by: Hussein Patwa | last post by:
Hi there. I'm new to this group. I'm also partially sighted so navigating the web is sometimes quite difficult. I'm looking for a javascript date picker, you know the ones that travel sites...
5
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August...
2
by: Frank | last post by:
The DB in question contains daily stock transactions keyed by ticker (symbol) and date. Unfortunately all dates do not exist for all ticker symbols. To assist the process, there are two...
3
by: RSB | last post by:
Hi Every one , IS there any Date Time Control with .Net. All i want to read is the Date and Time in a Single Field like 12/31/2004 09:23:23AM. if there is any then any examples for it. and if not...
8
by: gumi | last post by:
Hi, I am looking for code for a alarm clock program that pops up a messege to be used as part of my VB.Net class project. Any help is very much appreciated. Thanks
12
by: Orchid | last post by:
Hello all, I have different version of reports which used for different months. For example, I am using report version 1 up to September, but we have some design changes on the report for October,...
3
by: AMDRIT | last post by:
I am working with ObservableCollection and looking to implement sorting. I ran across code from Paul Stovell and he has: In the collection class the derives from ObservableCollection public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.