I've got this working much better now. Here's the code I wound up using, complete with (crappy) documentation.
Code:
Private Sub SwapButton_Click()
Dim OldToolSQL, NewToolSQL, UpdateToolTaskSQL As String
'The following "If..ElseIf..End If" block is used to make sure that all of
'the required fields are filled out before we try to construct the SQL
'statements. Could be made more generalized, but I don't write code for
'a living. Only the first instance is completely documented
If (IsNull(Me.Requestor)) Then 'Check to see if the Requestor field is blank
MsgBox "Please fill out the Requestor before swapping tools." 'Display a message informing the user that the field is blank
Me.Requestor.SetFocus 'Set the focus to the field that needs to be filled in.
Exit Sub 'Exit this function
ElseIf (IsNull(Me.Processor)) Then
MsgBox "Please fill out your name before swapping tools."
Me.Processor.SetFocus
Exit Sub
ElseIf (IsNull(Me.Tool_ID)) Then
MsgBox "Please fill out the Tool ID before swapping tools."
Me.Tool_ID.SetFocus
Exit Sub
ElseIf (IsNull(Me.Reason)) Then
MsgBox "Please fill out the reason for the swap before swapping tools."
Me.Reason.SetFocus
Exit Sub
ElseIf (IsNull(Me.New_Model_Number)) Then
MsgBox "Please fill out the new tool model number before swapping tools."
Me.New_Model_Number.SetFocus
Exit Sub
ElseIf (IsNull(Me.New_Serial_Number)) Then
MsgBox "Please fill out the new tool serial number before swapping tools."
Me.New_Serial_Number.SetFocus
Exit Sub
ElseIf (IsNull(Me.Torque)) Then
MsgBox "Please fill out the torque before swapping tools."
Me.Torque.SetFocus
Exit Sub
End If
'This creates the SQL to insert the old tool into the database.
OldToolSQL = "INSERT INTO tbl_ChangeHistory ([Model Number], " & _
"[Serial Number], [Date], [ChangeType], [Requestor], [Processor]) " & _
"VALUES (""" & Me.[Model Number].Column(1) & """, """ & _
Me.Serial_Number.Column(1) & """, #" & Me.Date & "#, " & _
"""" & Me.Reason.ItemData(0) & """, """ & Me.Requestor & """, """ & _
Me.Processor & """);"
'This creates the SQL to insert the new tool into the database.
NewToolSQL = "INSERT INTO tbl_ChangeHistory ([Model Number], " & _
"[Serial Number], [Date], [ChangeType], [Requestor], [Processor], " & _
"[Final Torque]) " & _
"VALUES (""" & Me.New_Model_Number & """, """ & _
Me.New_Serial_Number.Column(1) & """, #" & Me.Date & "#, " & _
"""" & Me.Reason.ItemData(0) & """, """ & Me.Requestor & """, """ & _
Me.Processor & """, " & Me.Torque & ");"
'This creates the SQL to update the tool to task relationship table.
UpdateToolTaskSQL = "UPDATE tbl_Tool_Task SET [Model Number] = """ & _
Me.New_Model_Number & """, [Serial Number] = """ & _
Me.New_Serial_Number.Column(1) & """, [Date Issued] = #" & _
Me.Date & "# WHERE [Tool ID] = """ & Me.Tool_ID & """;"
' Debug.Print OldToolSQL
' Debug.Print NewToolSQL
' Debug.Print UpdateToolTaskSQL
DoCmd.SetWarnings False 'turns off warnings
DoCmd.RunSQL OldToolSQL 'inserts the old tool into the DB
DoCmd.RunSQL NewToolSQL 'inserts the new tool into the DB
DoCmd.RunSQL UpdateToolTaskSQL 'updates the tool to task relationship
DoCmd.SetWarnings True 'turns warnings back on
'The following clears out all the form fields
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl
Me.Date = Now() 'Fills in the date field with the current date and time
Me.Requestor.SetFocus 'Sets the focus to the Requestor field.
End Sub
The trickiest part was writing the SQL using access variables. Several places I had to use Me.[Field].Column(1) to get the right piece of data, since my selection criteria for those combo boxes is a bit complex. I hope somebody else finds this useful. Let me know if I should post the complete database again.