On frmAddUpdateSchedule, there are several listboxes, comboboxes, and textboxes. Multi-select listbox lbxTrainPart gets 4 records from tblTrainPartQuery. I'm using the following code to put the values into tblScheudle.
It only works if the last thing I do is select the TrainPart. If appears that the focus has to be on lbxTrainPart for the code to work.
How can I get the code to run without the focus having to be on a specific control?
Thanks, Brian
Code:
Private Sub cmdAddRecord_Click()
On Error GoTo Err_cmdAddRecord_Click
'DoCmd.SetWarnings False
Dim cbx1 As ComboBox, cbx2 As ComboBox, tbx1 As TextBox, tbx2 As TextBox, tbx3 As TextBox, lbx1 As ListBox, lbx2 As ListBox, SQL As String, DQ As String, itm As Variant
Set cbx1 = Me!cbxDateTypeID
Set cbx2 = Me!cbxDateID
Set lbx1 = Me!lbxTrainNo
Set lbx2 = Me!lbxTrainPart
Set tbx1 = Me!tbxScheduleDate
Set tbx2 = Me!tbxNotes
Set tbx3 = Me!tbxTStamp
DQ = """"
If lbx1.ListIndex = (-1) Then
MsgBox "No Train Selected!"
ElseIf lbx2.ListIndex = (-1) Then
MsgBox "No Train Part(s) Selected!"
Else
For Each itm In lbx2.ItemsSelected
SQL = "INSERT INTO tblSchedule (ScheduleDate,TStamp,Notes,TrainNo,TrainPart,DateTypeID,DateID) " & _
"VALUES(#" & tbx1.Value & "#," _
& "#" & tbx3.Value & "#," _
& DQ & tbx2.Value & DQ & "," _
& DQ & lbx1.Column(0) & DQ & "," _
& DQ & lbx2.Column(0, itm) & DQ & "," _
& cbx1.Column(0) & "," _
& cbx2.Column(0) & ");"
Debug.Print SQL
DoCmd.RunSQL SQL
Next
End If
Set lbx1 = Nothing
Set lbx2 = Nothing
Set cbx1 = Nothing
Set cbx2 = Nothing
Set tbx1 = Nothing
Set tbx2 = Nothing
Set tbx3 = Nothing
Exit_cmdAddRecord_Click:
Exit Sub
Err_cmdAddRecord_Click:
MsgBox Err.Description
Resume Exit_cmdAddRecord_Click
End Sub
It only works if the last thing I do is select the TrainPart. If appears that the focus has to be on lbxTrainPart for the code to work.
How can I get the code to run without the focus having to be on a specific control?
Thanks, Brian