Inserting data using VBA

  • Thread starter Thread starter JOHN SMITH
  • Start date Start date
J

JOHN SMITH

Hi,

if my starting value is 01/01/2005 and my finish value is 01/04/2005

how do i insert (using code) into my table the days

01/01/2005
01/02/2005
01/03/2005
01/04/2005
 
Put two textboxes, StartDate and FinishDate, and a command buttonon your
form. Put the following code in the button's click event:

If IsNull(Me!StartDate) Or IsNull(Me!FinishDate) Then
Exit Sub
End If
Dim Db As DAO.Datebase
Dim Rst As DAO.Recordset
Dim StartDate As Date
Dim FinishDate as Date
Dim InsertDate as Date
Set Db As CurrentDB()
Set Rst As DB.OpenRecordset("MyTable")
StartDate = Me!NameOfStartDateTextbox
FinishDate = Me!NameOfFinishDateTextbox
For InsertDate = StartDate To FinishDate
With Rst
..Add
!NameOfDateFieldInTable = InsertDate
..Update
Next
..Close
End With
Set Rst = Nothing
Set Db = Nothing
 
Private Sub cmdRun_Click()
If IsNull(Me!StartDate) Or IsNull(Me!EndDate) Then
MsgBox "Please enter both A Start Date and an End Date!"
Exit Sub
End If
Dim v_StartDate As Date
Dim v_FinishDate As Date
Dim v_InsertDate As Date
Dim v_SQL As String
v_StartDate = Me!StartDate
v_FinishDate = Me!EndDate
For v_InsertDate = v_StartDate To v_FinishDate
v_SQL = "Insert into ReceiptTransactions (ReceiptDate) "
v_SQL = v_SQL & "Values (#" & v_InsertDate & "#)"
DoCmd.RunSQL (v_SQL)
Next
End Sub
 
Back
Top