I am trying to add multiple records based on a date range. I found some examples and have tried them but am getting errors and I can't seem to figure out what the problem is. I am not a VBA programmer so am a little lost. Any help would be appreciated.
As an example, the user would enter in the following:
4
522
6/5/2010
6/10/2010
Once the user selected OK it would enter 6 records into the schedule table as the following:
4, 522, 6/5/2010
4, 522, 6/6/2010
4, 522, 6/7/2010
4, 522, 6/8/2010
4, 522, 6/9/2010
4, 522, 6/10/2010
Following are the examples I am using and the error messages I am getting when I run them.
Option 1
This results in a Type Mismatch error.
Option 2
This results in a Compile Error. Syntax Error
As an example, the user would enter in the following:
4
522
6/5/2010
6/10/2010
Once the user selected OK it would enter 6 records into the schedule table as the following:
4, 522, 6/5/2010
4, 522, 6/6/2010
4, 522, 6/7/2010
4, 522, 6/8/2010
4, 522, 6/9/2010
4, 522, 6/10/2010
Following are the examples I am using and the error messages I am getting when I run them.
Option 1
Code:
Private Sub cmd_addRecord_Click()
On Error GoTo Err_cmd_addRecord_Click
Dim i
For i = Int(CVDate(Me!txt_beginDate)) To Int(CVDate(Me!txt_EndDate))
If (DatePart("w", i, vbMonday) <> 6) And (DatePart("w", i, vbMonday) <> 7) Then
DoCmd.RunSQL "Insert INTO schedule([contact ID], [Event ID], Date) Values('" + Me!txt_ContactID + "', " + Format(i, "m/d/yyyy") + ",8);"
End If
Next i
Exit_cmd_addRecord_Click:
Exit Sub
Err_cmd_addRecord_Click:
MsgBox Err.Description
Resume Exit_cmd_addRecord_Click
End Sub
Option 2
Code:
Private Sub cmd_addRecord_Click()
On Error GoTo Err_cmd_addRecord_Click
Dim RepeatEvent
For RepeatEvent = Int(CDate(Me!txt_beginDate)) To Int(CDate(Me!txt_EndDate))
DoCmd.RunSQL "INSERT INTO schedule ([contact ID], [Event ID], Date) " & _
"SELECT Forms![Form2]!txt_ContactID AS Expr1," & _
[Forms]![Form2]![txt_EventID] AS Expr2,#" & _
Format$(RepeatEvent, "m/d/yyyy")"#;"
Next RepeatEvent
Exit_cmd_addRecord_Click:
Exit Sub
Err_cmd_addRecord_Click:
MsgBox Err.Description
Resume Exit_cmd_addRecord_Click
End Sub
Comment