473,698 Members | 2,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting dates into table does not work

Jerry Maiapu
259 Contributor
I am trying to punch into a table a bunch of dates that fall between two given dates.

When compiled,no error but when on run does not do anything..(i.e nothing in the table)

Can someone figure out what's missing?


Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Private Sub cmdinsert_Click()
  4.  
  5.   Dim db            As DAO.Database
  6.   Dim rs            As DAO.Recordset
  7.   Dim counter, numdays, mynum As Integer
  8.   Dim mydate As Date
  9.  
  10.   On Error GoTo ErrorHandler
  11.  
  12.   Set db = CurrentDb()
  13.   Set rs = db.OpenRecordset("Trialdate", dbOpenDynaset, dbAppendOnly)
  14.  
  15. numdays = DateDiff("d", Forms!frmCriteria!txtdate1, Forms!frmCriteria!txtdate2)
  16.  'will store an integer rep. days difference
  17.  
  18. counter = 0
  19. mynum = numdays + 1
  20.  
  21.  
  22. Do While counter < mynum
  23.  
  24. mydate = DateAdd("d", counter + 1, Forms!frmCriteria!txtdate1)
  25.  
  26.    rs.AddNew ' go to a new record in our temp table rs
  27.     rs!InsertedDates = mydate
  28.     rs.Update
  29. Loop
  30. MsgBox "Already Done"
  31.  
  32.  
  33. ExitHandler:
  34.   Set rs = Nothing
  35.   Set db = Nothing
  36.   Exit Sub
  37.  
  38. ErrorHandler:
  39.   Select Case Err
  40.     Case Else
  41.       MsgBox Err.Description
  42.       DoCmd.Hourglass False
  43.       Resume ExitHandler
  44.   End Select
  45. End Sub
  46.  
NB: txtdate1 and txtdate2 line 23 & 15 are the date ranges entered in form frmCriteria
It should inserted all dates between these dates into table Trialdate

Thanks in advance.

Jerry
Aug 2 '10 #1
2 1569
ADezii
8,834 Recognized Expert Expert
@Jerry Maiapu
Here is some sample and compact code that will Insert Dates 'Between' a Start and End Date (non-inclusive) into a Field named [TrialDate] in a Table named Table1. For the sake of simplicity and brevity, the code performs no Validation, and assumes that all other Requirements have been met.
Expand|Select|Wrap|Line Numbers
  1. Const conSTART_DATE As Date = #8/2/2010#
  2. Const conEND_DATE As Date = #8/12/2010#
  3. Dim intCtr As Integer
  4. Dim intDateDiff As Integer
  5.  
  6. intDateDiff = DateDiff("d", conSTART_DATE, conEND_DATE)
  7.  
  8. For intCtr = 1 To intDateDiff - 1
  9.   CurrentDb.Execute "INSERT INTO Table1 ([TrialDate]) VALUES(#" & _
  10.                      DateAdd("d", intCtr, conSTART_DATE) & "#);", dbFailOnError
  11. Next
Aug 2 '10 #2
Jerry Maiapu
259 Contributor
I figure it out myself. For..Next loop is doing a good job here.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmdinsert_Click()
  3.   Dim db            As DAO.Database
  4.   Dim rs            As DAO.Recordset
  5.   Dim counter, numdays, mynum, gotit As Integer
  6.   Dim mydate, date1, date2 As Date
  7.  
  8. date1 = Me.txtdate1
  9. date2 = Me.txtdate2
  10.   On Error GoTo ErrorHandler
  11.   Set db = CurrentDb()
  12.   Set rs = db.OpenRecordset("Trialdate", dbOpenDynaset, dbAppendOnly)
  13. numdays = DateDiff("d", date1, date2) 'will store an integer rep. days diffirence
  14. mynum = numdays
  15. For counter = 1 To mynum
  16. mydate = DateAdd("d", counter, date1)
  17.    rs.AddNew ' go to a new record in our temp table rs
  18.     rs!InsertedDates = mydate
  19.     rs.Update
  20. Next counter
  21. MsgBox "Already Done"
  22.  
  23. ExitHandler:
  24.   Set rs = Nothing
  25.   Set db = Nothing
  26.   Exit Sub
  27.  
  28. ErrorHandler:
  29.   Select Case Err
  30.     Case Else
  31.       MsgBox Err.Description
  32.       DoCmd.Hourglass False
  33.       Resume ExitHandler
  34.   End Select
  35. End Sub
NB: Results excludes date ranges..To Include simply replace 1 by 0 in code# 15

For interested members out there

Cheers

Jerry
Aug 3 '10 #3

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

Similar topics

1
3288
by: Fons Dijkstra | last post by:
Hello, I'm using the mx.ODBC.Windows package in order to read/write a MSAccess database. Everything works fine apart from the DATE format handling. I'm using the default "datetimeformat" (i.e. DATETIME_DATETIMEFORMAT) as suggested. The date columns in the MSAccess database have "Short Date" format. When I read a DATE item everything works fine, like:
2
11319
by: tperovic | last post by:
Using SS2K, I'm getting the following error while bulk inserting: Column 'warranty_expiration_date' cannot be modified because it is a computed column. Here is my bulk insert statement: BULK INSERT dbo.TestData FROM 'TestData.dat' WITH (CHECK_CONSTRAINTS,
4
2015
by: Little PussyCat | last post by:
Hello, I have had a request, one of our tables is used as a report and I have been asked that all fieldnames for months have dashes in them, like Jan-05 instead of Jan05 and so on... Now what we have is a CURSOR which loops through all values in another table generating these fieldnames, like 'Jan-05', Feb-05' etc.. Then the table definition is modified so these months display as fieldnames.
1
2018
by: Joe Philip | last post by:
I created a file called data.del using export like this: export to data.del of del select * from sy9.c001 Now, I want to import this data to a different database into a different table. This table does not already exists in the database. For example, I tried this: import from data.del of del insert into c001prod When I do this, I get the error that table does not exist. How do I instruct
4
12373
by: Tom Dauria | last post by:
What I am trying to do is write a resume into a word document from information in an Access database. I have been using bookmarks and inserting table results into the document and so far it's working but I have run into a problem. The final section of the resume deals with Experience which is subgrouped by Market Segments and then experience. What I want it to look like is
9
1526
by: Andrew Banks | last post by:
I'm running the following code in a C#.NET page and it doesn't enter the values into the DB. I'm certain the problem is to do with the txtBirth field. It allows users to enter a DOB as dd/mm/yyyy and I think it's the slashes(/) that are causing the problem. If I don't enter a DOB in this field then all the data enters into the database without a problem. Any ideas? SQL Server 2000, VS.NET, C#
1
6793
by: siddharth | last post by:
when i am finding a particular row in a database through a datatable.row.find(the particular text to find) it gives me table does not have primary key although it has a primary key
15
1580
by: Naha | last post by:
Hi guys, Having a problem, my table does not get displayed, when i load the page it gives me the following error(p.s-the codes below): <%@ page import="java.util.*,java.io.*" %> <%@ page import="testBean.TestBean" %>
19
1989
by: RP | last post by:
I have a DateTimePicker with format dd-MM-yyyy. While attempting to insert this date in SQL Server Date column, following exception is thrown: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. Please help.
12
3381
by: kalyson | last post by:
I am using a variable of type: DATE Our instance of Oracle has: NLS_DATE_FORMAT DD-MON-YY I select a DATE from table A into this date variable. I then insert that variable into another table, B. Dates that were before 2000 change -- like 1995 becomes 2095. I do nothing to the date between select and insert. My PL/SQL is being executed in Toad version 9.1. Oracle is release 1002000300 Does anyone know what is...
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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 we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.