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? - Option Compare Database
-
Option Explicit
-
Private Sub cmdinsert_Click()
-
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim counter, numdays, mynum As Integer
-
Dim mydate As Date
-
-
On Error GoTo ErrorHandler
-
-
Set db = CurrentDb()
-
Set rs = db.OpenRecordset("Trialdate", dbOpenDynaset, dbAppendOnly)
-
-
numdays = DateDiff("d", Forms!frmCriteria!txtdate1, Forms!frmCriteria!txtdate2)
-
'will store an integer rep. days difference
-
-
counter = 0
-
mynum = numdays + 1
-
-
-
Do While counter < mynum
-
-
mydate = DateAdd("d", counter + 1, Forms!frmCriteria!txtdate1)
-
-
rs.AddNew ' go to a new record in our temp table rs
-
rs!InsertedDates = mydate
-
rs.Update
-
Loop
-
MsgBox "Already Done"
-
-
-
ExitHandler:
-
Set rs = Nothing
-
Set db = Nothing
-
Exit Sub
-
-
ErrorHandler:
-
Select Case Err
-
Case Else
-
MsgBox Err.Description
-
DoCmd.Hourglass False
-
Resume ExitHandler
-
End Select
-
End Sub
-
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
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. - Const conSTART_DATE As Date = #8/2/2010#
-
Const conEND_DATE As Date = #8/12/2010#
-
Dim intCtr As Integer
-
Dim intDateDiff As Integer
-
-
intDateDiff = DateDiff("d", conSTART_DATE, conEND_DATE)
-
-
For intCtr = 1 To intDateDiff - 1
-
CurrentDb.Execute "INSERT INTO Table1 ([TrialDate]) VALUES(#" & _
-
DateAdd("d", intCtr, conSTART_DATE) & "#);", dbFailOnError
-
Next
I figure it out myself. For..Next loop is doing a good job here. -
-
Private Sub cmdinsert_Click()
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim counter, numdays, mynum, gotit As Integer
-
Dim mydate, date1, date2 As Date
-
-
date1 = Me.txtdate1
-
date2 = Me.txtdate2
-
On Error GoTo ErrorHandler
-
Set db = CurrentDb()
-
Set rs = db.OpenRecordset("Trialdate", dbOpenDynaset, dbAppendOnly)
-
numdays = DateDiff("d", date1, date2) 'will store an integer rep. days diffirence
-
mynum = numdays
-
For counter = 1 To mynum
-
mydate = DateAdd("d", counter, date1)
-
rs.AddNew ' go to a new record in our temp table rs
-
rs!InsertedDates = mydate
-
rs.Update
-
Next counter
-
MsgBox "Already Done"
-
-
ExitHandler:
-
Set rs = Nothing
-
Set db = Nothing
-
Exit Sub
-
-
ErrorHandler:
-
Select Case Err
-
Case Else
-
MsgBox Err.Description
-
DoCmd.Hourglass False
-
Resume ExitHandler
-
End Select
-
End Sub
NB: Results excludes date ranges..To Include simply replace 1 by 0 in code# 15
For interested members out there
Cheers
Jerry
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
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,
|
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.
|
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
|
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
| |
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#
|
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
|
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" %>
|
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.
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |