listview control selected item

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i am wanting to set up a double click event that would take the item being
doubleclicked will open up another form with some of the information from the
list item that was double clicked. The following code is not working for
this.
-------------------------------------------------
Private Sub lsvOpenOrders_DblClick()
'Dim Variables
Dim MyDB As DAO.Database
Dim rstWorkOrder, rstWorkTypeID, rstDeptID, rstPlantID, rstPressID,
rstToolID As DAO.Recordset
Dim intWON, intWTID, intDeptID, intPlantID, intPressID, intToolID As Integer
'Set Database and Recordset Variables
Set MyDB = CurrentDb
Set rstWorkOrder = MyDB.OpenRecordset("tblWorkOrders", dbOpenDynaset)
Set rstWorkTypeID = MyDB.OpenRecordset("tblWorkType", dbOpenDynaset)
Set rstDeptID = MyDB.OpenRecordset("tblDepartment", dbOpenDynaset)
Set rstPlantID = MyDB.OpenRecordset("tblPlant", dbOpenDynaset)
Set rstPressID = MyDB.OpenRecordset("tblPress", dbOpenDynaset)
Set rstToolID = MyDB.OpenRecordset("tblTool", dbOpenDynaset)
'Compare selected item information to table information.
rstWorkTypeID.MoveFirst
Do While rstWorkTypeID.EOF = False
If
lsvOpenOrders.ListItems.SelectedItem(lsvOpenOrders.SelectedItem.SubItem(3)) =
rstWorkTypeID.Fields("ID").Value Then
strWorkType = rstWorkTypeID.Fields("Type").Value
GoTo Continue
Else
rstWorkTypeID.MoveNext
End If
Loop
Continue:
Debug.Print CInt(strWorkType)
End Sub
 
The ListView control does not have the most intuitive programming interface
in the world. That's for sure

You might try something like this:

Private Sub lvwOpenOrders_DblClick()
Dim rstWorkType As DAO.Recordset
Dim strSQL As String
Dim strLvwID As String

strLvwID = Me.lvwOpenOrders.SelectedItem.SubItems(1)

strSQL = "SELECT Type FROM tblWorkTypeIDs WHERE ID=" & strLvwID

Set rstWorkType = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

If Not (rstWorkType.BOF And rstWorkType.EOF) Then
MsgBox "Work Type Selected: " & rstWorkType!Type
End If

rstWorkType.Close

Set rstWorkType = Nothing
End Sub

I ommitted a bunch of the other stuff you had for clarity.

Also .... I would avoid searching for the Work Type using the looping
technique you have in your code unless you intend to do something to every
record in the Recordset as you look for your value. You should let Jet do the
work for you since it will use a binary search against the index for ID it
will find it a whole lot faster than your code will.

If you insist on using the code you have written you should get rid of the
"Goto Continue" statement and corresponding line label and use an "Exit Do"
statement in it's place. The use of Goto can really make it hard to debug the
"Spagetti Code" that results.

If this answered your question. Please make sure to respond appropriatly
when asked so that it will be marked as an answer.
 
Ok, will look into this. Was actually able to get it working to do what i
need for right now. Once i get the rest of the forms and reports made for
the database, then i'll come back and take another look at your code, to try
and clean up mine.
thanks again.
 
Back
Top