InLaNoche
10-28-2015, 07:19 AM
I have 2 listboxes on a form. the first is populated by a query, and lists all the box numbers for a specific office. The second listbox is populated by referencing the current switch and finding all the boxnumbers that are assigned to that switch.
What I am trying to do is be able to remove the box number from the switch by double clicking on the entry in the second listbox. This way you will know what box you are removing from the switch. The problem is that the first listbox is the actual data, so I would need to look up what was selected in the 2nd listbox in the first (boxnumber). So here is the code:
Private Sub PortList_DblClick(Cancel As Integer)
'using double click to find this record in the BoxNumber listbox
Dim aStr As String
Dim isFound As Integer
'search critera is based on what is selected
aStr = Me.PortList.Value
isFound = -1
Debug.Print aStr
Dim rs As Recordset
Me.BoxNumberList.Requery
Set rs = Me.BoxNumberList.Recordset
rs.MoveFirst
rs.FindFirst "[BoxNumber] LIKE '" & aStr & "'"
If rs.NoMatch Then
MsgBox "ERROR!!!"
Else
MsgBox "looking for " & aStr
MsgBox "found " & rs!BoxNumber & " at " & rs.AbsolutePosition
isFound = rs.AbsolutePosition
Me.BoxNumberList.Selected(isFound) = True
MsgBox Me.BoxNumberList.ListIndex & " Selected"
End If
End Sub
funny thing is that aStr does not match rs!BoxNumber. I though that rs.FindFirst would sit on the found record... Does anyone know why this is not working?
For example, if I click on the first item in the 2nd list box, the msgbox outputs are :
looking for 415-PD001
found 415-PD001 at 522
521 Selected
But when I double click on the 2nd entry I get:
looking for 415-PD002
found 415-PD001 at 522
521 Selected
most often it will find 415-PD001, regardless of what the search is for (aStr). And to make things worse, 415-PD001 is not the 522nd record, it's record 511.
What I am trying to do is be able to remove the box number from the switch by double clicking on the entry in the second listbox. This way you will know what box you are removing from the switch. The problem is that the first listbox is the actual data, so I would need to look up what was selected in the 2nd listbox in the first (boxnumber). So here is the code:
Private Sub PortList_DblClick(Cancel As Integer)
'using double click to find this record in the BoxNumber listbox
Dim aStr As String
Dim isFound As Integer
'search critera is based on what is selected
aStr = Me.PortList.Value
isFound = -1
Debug.Print aStr
Dim rs As Recordset
Me.BoxNumberList.Requery
Set rs = Me.BoxNumberList.Recordset
rs.MoveFirst
rs.FindFirst "[BoxNumber] LIKE '" & aStr & "'"
If rs.NoMatch Then
MsgBox "ERROR!!!"
Else
MsgBox "looking for " & aStr
MsgBox "found " & rs!BoxNumber & " at " & rs.AbsolutePosition
isFound = rs.AbsolutePosition
Me.BoxNumberList.Selected(isFound) = True
MsgBox Me.BoxNumberList.ListIndex & " Selected"
End If
End Sub
funny thing is that aStr does not match rs!BoxNumber. I though that rs.FindFirst would sit on the found record... Does anyone know why this is not working?
For example, if I click on the first item in the 2nd list box, the msgbox outputs are :
looking for 415-PD001
found 415-PD001 at 522
521 Selected
But when I double click on the 2nd entry I get:
looking for 415-PD002
found 415-PD001 at 522
521 Selected
most often it will find 415-PD001, regardless of what the search is for (aStr). And to make things worse, 415-PD001 is not the 522nd record, it's record 511.