PDA

Click to See Complete Forum and Search --> : MultiSelect listbox


cajsoft
Aug 16th, 2000, 07:48 AM
Hello again, I thought I'd strike again while the iron is hot!!

Ok, I have 2 events (addfaultcause and deletefaultcause) - on_click

I want to be able to multiselect the items in my listbox (lstfaultcause) and be able to delete the items from both the list box and my SQL table. At present I can delete only one selection at a time, but I'd like to be able to select multiple entries and delete them all at once.

Can someone help? I need to be able to keep the index (shown in code below). I'm using RDO to connect

---Code Begins---

Private sub addfaultcause_click()
Dim infault As String

infault = InputBox("Please enter the Fault Cause? ", "Enter Fault Cause", "")
If infault = "" Then Exit Sub
ans = MsgBox("Are you Sure? ", vbQuestion + vbYesNo, "Question")
If ans = 6 Then
cnCon.Connect = "uid=sa;pwd=;server=" & txtsvr & ";driver={SQL Server};database=" & txtdb & ";"
cnCon.CursorDriver = rdUseOdbc
cnCon.EstablishConnection 'rdDriverNoPrompt
cnCon.AddCause infault
cnCon.Close
lstfaultcause.AddItem (infault)
End Sub

Private Sub deletefaultcause_click()
Dim DelSet As rdoResultset
cnCon.Connect = "uid=sa;pwd=;server=" & txtsvr & ";driver={SQL Server};database=" & txtdb & ";"
cnCon.CursorDriver = rdUseOdbc
cnCon.EstablishConnection 'rdDriverNoPrompt
cnCon.DelCause lstfaultcause.ItemData(lstfaultcause.ListIndex) 'Delcause is SQL-Defined code
Set DelSet = cnCon.LastQueryResults
lstfaultcause.RemoveItem (lstfaultcause.ListIndex)
cnCon.Close
end sub


I guess that I'd have to change the deletefaultcause_click event with need code to allow Multiselect in the list box.

Thanks
Craig.

davidrobin
Aug 17th, 2000, 07:54 AM
I have never used rdo and ODBC but maybe this will help. Have you tried traversing through the items in the list and if selected remove the item in the list and the subsequent record from the database.
The following code will remove the highlighted rows from the listbox, it works from the last to the first this is because I use the listCount property.

Dim intLoop As Integer
For intLoop = List1.ListCount - 1 To 0 Step -1
If List1.Selected(intLoop) Then
List1.RemoveItem (intLoop)
'place the relevant record delete code here also
End If
Next

cajsoft
Aug 17th, 2000, 09:21 AM
Thanks... That did the trick! I was nearly there with the code, as I tried something similar but never used the correct syntax.

I'm a very new VB programmer as you can see...

Thanks again,

These forums are great!

Craig.