Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Losing Focus of multi-select listbox

Status
Not open for further replies.

BrianLe

Programmer
Feb 19, 2002
229
US
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.

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
 
Found my answer in an earlier thread. Added this after
DQ = """"

Code:
 ' ListBox must have the Focus
   Me.lbxTrainPart.SetFocus

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top