473,471 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple Records Based On A Date Range

3 New Member
Hello:

I am trying to create multiple records in a table named tblTravel based on the input of a date range in two form controls. The inputs for the form are LastName, TravelDate, EndDate, Event, LocationCity, and LocationState. Everything works perfectly, except for the fact that the dates do not correctly enter into the table. For example, if I enter a date range of March 3, 2008 - March 5, 2008 and click OK, 3 records are generated. The first has the correct date (March 3, 2008), but the next two records return dates of 12-31-1899. The code I am using is pasted below. Please help!!!

Expand|Select|Wrap|Line Numbers
  1. Private Sub OK_onclick()
  2.  
  3. Dim i
  4.         For i = Int(CVDate(Me!fldTravelDate)) To Int(CVDate(Me!fldEndDate))
  5.         If (DatePart("w", i, vbMonday) <> 6) And (DatePart("w", i, vbMonday) <> 7) Then
  6.         DoCmd.RunSQL "INSERT INTO tblTravel(fldName,fldTravelDate,fldEventName,fldLocationCity,fldLocationState) VALUES ('" + Me!fldName + "'," + Format(i, "dd/mm/yy") + ",'" + Me!fldEventName + "','" + Me!fldLocationCity + "','" + Me!fldLocationState + "');"
  7.         End If
  8.         Next i
  9.  
  10. End Sub
Mar 18 '08 #1
5 2488
Scott Price
1,384 Recognized Expert Top Contributor
Take a good look at lines 4 and the Format(i) function in line 6.

You take a date or string value from the fldTravelDate and convert it to a Variant with subtype Date, then you use the Int() function to return only the integer portion of it. Then in line 6 you are asking the database to look at the integer portion and format it in a specific way.

Try instead of going through the rigmarole of the CVDate and then the Int, using the CDate() function instead. The CDate() function returns a Date/Time value which is stored behind the scenes as an Integer value, and then formatted according to your Regional settings in the Control Panel.

The only real reason to use CVDate() over CDate() is if you are using an older legacy database, or you have a lot of Null values in the fldTravelDate, etc. If you do have Null values, it would be far better, in this case, to test for the null first.

Regards,
Scott
Mar 18 '08 #2
jrodcody
3 New Member
Sorry, I have been playing around with this further. The original code was copied and altered from an old post regarding this subject. I am new with working in VBA, and am struggling to fix according to the suggestions mentioned. I wonder if you can help guide me through this.
Mar 18 '08 #3
Scott Price
1,384 Recognized Expert Top Contributor
Well, instead of Int(CVDate(Me!fldTravelDate)) you would have CDate(Me!fldTravelDate).

To test for nulls:

Expand|Select|Wrap|Line Numbers
  1. If Not Isnull(Me!fldTravelDate) And Not Isnull(Me!fldEndDate) Then
  2.     For i = CDate(Me!fldTravelDate) To Cdate(Me!fldEndDate)
  3. ..... bla bla blah
  4. Next i
  5. End If
  6.  
Regards,
Scott
Mar 18 '08 #4
jrodcody
3 New Member
I have changed the code to appear as below:

Expand|Select|Wrap|Line Numbers
  1.     Private Sub OK_onclick()
  2.  
  3.     Dim i
  4.             For i = CDate(Me!fldTravelDate) To CDate(Me!fldEndDate)
  5.             If Not Isnull(Me!fldTravelDate) And Not Isnull(Me!fldEndDate) Then
  6.             DoCmd.RunSQL "INSERT INTO tblTravel(fldName,fldTravelDate,fldEventName,fldLo  cationCity,fldLocationState) VALUES ('" + Me!fldName + "'," + Format(i, "dd/mm/yy") + ",'" + Me!fldEventName + "','" + Me!fldLocationCity + "','" + Me!fldLocationState + "');"
  7.             End If
  8.             Next i
  9.  
  10.     End Sub
  11.  
I also reversed lines 4 and 5, (I think this is how the last post suggested), but this gave an error related to "if block".
I tried to change line 3 to "Dim i as Date"
I tried playing around with i down in the sql.

Here's the strange thing. When I hold my mouse over i in the sql statement, it returns i = 3/1/08, which is exactly correct. However, in the form, when I click OK and then go check the table column (formatted as short date), it shows 12-31-1899. I changed the table to show general date, and those 3 new records have values of 01:00:00, 02:00:00 and 03:00:00.

Hopefully this information will help shed some light on what's happening and you can help save my last shred of sanity. Thanks again !!!
Mar 19 '08 #5
Scott Price
1,384 Recognized Expert Top Contributor
SQL specifies that it only works with date formats of MM/DD/YYYY, which is the North American format. Access is remarkably forgiving when working with date formats, and can correctly interpret many non-ambiguous dates, however the problem arises when you feed it an ambiguous date, like you have given me as an example: 3/1/08... Does that means March 1, 2008, or does it means January 3, 2008? I notice later in your code you have it formatting to the DD/MM/YYYY format, is this your control panel setting?

What happens if you remove the Format(i, "dd/mm/yy") in your SQL statement and replace it simply with i? If this evaluates the dates correctly, and you NEED to have them back in the dd/mm/yy format, you'll have to then wrap your Format() construct in another CDate() or CVDate() function like this: CVDate(Format(i, "dd/mm/yy"))

Also, I notice you are using the + concatenation character. This is fine, but it allows null propagation, which in this instance probably isn't what you really want. If I were you, I'd change the + signs in the SQL statement to & ampersand characters.

Regards,
Scott
Mar 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: arthur-e | last post by:
I can filter a query for a report on a field of a subform BUT now.... I'd like to be able to select more than one item in a multi-list box to select all the records. ALSO to use two fields (or...
2
by: Terry | last post by:
I have a database that I would like to query for a specific date range. I want to create a report based on this query that will pull any records from the last 7 days. Is this possible? Can...
0
by: allyn44 | last post by:
HI--I have 2 tables Cut: cut ID, HistNumb, Block, date: Cut Id is the primary key, the other 3 fileds are indexed to be unique Slides: Cutid SlideID, and various other fields: there can be...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
4
by: Joanie | last post by:
I have a form that records dates of unavailability for a worker. Based on what is entered in the simple table behind the form, many calculations take place to create employee "load" balance. Each...
2
by: chrisale | last post by:
Hi All, I've been racking my brain trying to figure out some sort of Sub-Select mySQL statement that will create a result with multiple rows of averaged values over a years time. What I have...
4
by: Eugene Anthony | last post by:
I have a table that has a DateTime column which uses a DataTime datatype. How do I retrieve a range of records based on the month and year using ms sql? Eugene Anthony *** Sent via...
1
by: RussCRM | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.