Edited since original posting: I realized that I had declared the volId variable as an Integer, but it's being read as a string. Once I changed the declaration to "Dim volId As String" the SQL code appears to be getting generated properly. Now I just need help in figuring out why the records are not being inserted into the temporary table.
I am trying to create a form that will allow the user to create multiple work records without having to re-enter the date, hours, and category information. (Ex: 10 people worked the same shift at the holiday party.) The way I plan to do this is to create a temp table, write a complete record to the table for each volunteer selected, then do an INSERT query to select everything from the temp table and insert the records into the real Work_Records table. (This second portion is not done yet. I'm debugging as I go, and have gotten stuck with the first part.)
The problem is that my records do not appear to actually be getting inserted into the temp table. I'm guessing that something is wrong with my SQL code.
Thanks!
My code:
Private Sub qryAppendMassWorkRecords_Click()
On Error Resume Next
DoCmd.RunSQL "DROP TABLE Tmp"
'Declare Vars
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rstRecords As Recordset
Dim dynamicSQL As String
Dim strSQL As String
Dim strTable As String
Dim hrsWorked As Integer
Dim DateWorked As Date
Dim pgmWorked As Integer
Dim volId As String
'Set initial values
Set db = CurrentDb
Set qdf = db.QueryDefs("qryTempAppendTable")
'Create new temp table to hold values from the form
strSQL = "CREATE TABLE Tmp (VolunteerID VARCHAR(20), DateWorked DATETIME, HoursWorked INT, WorkCategory INT);"
db.Execute strSQL
'Grab the values for the static vars and assign them
For Each ctl In Me.Controls
If ctl.Properties("Name") = "DateWorked" Then
DateWorked = ctl.Value
End If
If ctl.Properties("Name") = "HoursWorked" Then
hrsWorked = ctl.Value
End If
If ctl.Properties("Name") = "WorkCategory" Then
pgmWorked = ctl.Value
End If
Next ctl
'If combo box length > 0, create an INSERT statement to add the record to the temp table
For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Then
'Category is the only combo box other than the ones for volunteer names; make sure it's not getting picked up
If ctl.Properties("Name") <> "WorkCategory" Then
'Verify that the field contains a name
If Len(ctl.Value) > 0 Then
volId = ctl.Value
'Reset the dynamicSQL to the initial code & add form values
dynamicSQL = "INSERT INTO Tmp VALUES (" _
& "'" & volId & "', #" & DateWorked & "#, " & hrsWorked & ", " & pgmWorked & ");"
db.Execute dynamicSQL
End If
End If
End If
qdf.SQL = dynamicSQL
Next ctl
DoCmd.OpenTable "Tmp", acViewPreview
End Sub
Try this:
dynamicSQL = "INSERT INTO Tmp VALUES ('" _
& volId & "', #" & DateWorked & "#, " & hrsWorked & ", " & pgmWorked & ");"
It will get a good Access SQL string:
INSERT INTO Tmp VALUES ('15', #12/02/2013#, 5, 3);
In Access Database, Datetime must be quoted with sharp #, like: #mm/dd/yyyy hh:mm:ss#, or #yyyy-mm-dd hh:mm:ss#...
#12/02/2013#
More your VolunteerID is a string, so single quote like this:
'15'
Please notify, Access can only execute one single "INSERT" instruction, not multiple INSERT's as in MySQL. If you have many records, you must do a loop using VBA.
And run it to insert into Tmp, that you have not done:
db.Execute dynamicSQL
For example, this worked for me:
Sub qryAppendMassWorkRecords_Click()
On Error Resume Next
Dim strSQL As String
Dim db
'Set initial values
Set db = CurrentDb
strSQL = "CREATE TABLE Tmp (VolunteerID VARCHAR(20), DateWorked DATETIME, HoursWorked INT, WorkCategory INT);"
db.Execute strSQL
strSQL = "INSERT INTO Tmp VALUES ('242013', #12/2/2013#, 4, 39);"
db.Execute strSQL
DoCmd.OpenTable "Tmp", acViewPreview
End Sub
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments