I keep getting an Error 94 when I click the button that I have applied this code to. I think I am close but obviously missing something. When it doesn't give the error code and claims to have saved the data doesn't actually end up on the table. Please help.
Option Compare Database
Private Sub manageWorkRecords_Click()
DoCmd.OpenForm "frmWorkRecordEdit", , , , acFormEdit
End Sub
Private Sub qryAppendMassWorkRecords_Click()
On Error Resume Next
DoCmd.RunSQL "DROP TABLE Tmp"
'Error handling
On Error GoTo cmdOK_Click_err
'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 insertSQL As String
Dim strTable As String
Dim hrsWorked As Integer
Dim Rate As Integer
Dim DateStarted As Date
Dim DateEnded As Date
Dim pgmWorked As Integer
Dim volId As String
Set db = CurrentDb
'Create new temp table to hold values from the form
strSQL = "CREATE TABLE Tmp (VolunteerID VARCHAR(20), DateStarted DATE,DateEnded DATE, HoursWorked INT, Rate INT, WorkCategory INT);"
db.Execute strSQL
'Grab the values for the static vars and assign them
For Each ctl In Me.Controls
If ctl.Name = "DateStarted" Then
DateStarted = ctl.Value
End If
If ctl.Name = "DateEnded" Then
DateEnded = ctl.Value
End If
If ctl.Name = "Rate" Then
Rate = ctl.Value
End If
If ctl.Name = "HoursWorked" Then
hrsWorked = ctl.Value
End If
If ctl.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.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 & "' , #" & DateStarted & "#, #" & DateEnded & "#, " & hrsWorked & ", " & Rate & ", " & pgmWorked & ");"
db.Execute dynamicSQL
End If
End If
End If
Next ctl
'Insert records from temp table into tblWorkRecords table
insertSQL = "INSERT INTO tblWorkRecords (Volunteer, DateStarted, DateEnded, HoursWorked, Rate, WorkCategory) SELECT VolunteerID, DateStarted, DateEnded, HoursWorked, Rate, WorkCategory FROM Tmp;"
db.Execute insertSQL
'Confirmation message box
MsgBox ("Your records have been saved.")
'Clear form data
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
If Len(ctl.ControlSource) = 0 Then
ctl.Value = Null
End If
Case Else
' pass
End Select
Next
'Error handling
cmdOK_Click_exit:
Set qdf = Nothing
Set db = Nothing
Exit Sub
'Error handling
cmdOK_Click_err:
MsgBox "An unexpected error has occurred." & _
vbCrLf & "Please note the following details:" & _
vbCrLf & "Error Number: " & Err.Number & _
vbCrLf & "Description: " & Err.Description _
, vbCritical, "Error"
Resume cmdOK_Click_exit
End Sub