I am new with VBA programming. I have designed one form in which there is a button. After clicking this button I want to insert record in my table having one date column. The number of dummy record should be equals to number of days in current month. If Month is May 2016 then record will inserted with date 1/5/2016, 2/5/2016.......31/5/2016 like that.
Thanks in advance.
Private Sub Command0_Click()
Dim strQuery As String
Dim currDateTime As Date
currDateTime = Now()
strQuery = "INSERT INTO tbl_ShipOrders (IDate ) VALUES (" & currDateTime & " )"
CurrentDb.Execute (strQuery)
End Sub
The following expression will give you an integer, the number of days in the current month:
This is the zeroth day of the following month, which is the last day of the current month. This expression will still work if moving from December to January.
Store this in a variable, say
lastDay
then use a loop,For x = 1 To lastDay
to perform the inserts.Within the loop, this expression
will give you the dates 1/5/2016, 2/5/2016,.., 31/5/2016.
You should also surround the dates with date delimiters # when inserting. Combine this with ISO formatting of the date
yyyy-mm-dd
(so that months won't be interpreted as days):where dtValue is the date you've just formed using the previous DateSerial expression.
You can create a table containing every possible day number
and then just use a query like this to generate one row for each day in the current month
To use it as an INSERT query would be
Please, see below code and read comments:
Here's a fancy query that will return a month's dates:
That said, I would write a loop adding records to a recordset using VBA, not the slow SQL call of yours.