Select all Listbox AND Update listbox

SeBasTiaan

Registered User.
Local time
Today, 18:17
Joined
Jun 7, 2005
Messages
24
Select all in Listbox AND Update textbox

Hello,

I've got this multiple select listbox which writes data into a textbox:

Private Sub List2_AfterUpdate()

Dim Cursisten As String
Dim ctl As Control
Dim Itm As Variant

Set ctl = Me.List2

For Each Itm In ctl.ItemsSelected
If Len(Cursisten) = 0 Then
Cursisten = ctl.ItemData(Itm)
Else
Cursisten = Cursisten & "," & ctl.ItemData(Itm)
End If
Next Itm
Me.txtCursisten = Cursisten

End Sub


And I've got a SELECT ALL button to select all records in the listbox:

Private Sub cmdSelectAll_Click()
On Error GoTo Err_cmdSelectAll_Click

Dim i As Integer

If cmdSelectAll.Caption = "Alles Selecteren" Then
For i = 0 To Me.List2.ListCount
Me.List2.Selected(i) = True
Next i
cmdSelectAll.Caption = "Alles De-Selecteren"
Else
For i = 0 To Me.List2.ListCount
Me.List2.Selected(i) = False
Next i
cmdSelectAll.Caption = "Alles Selecteren"

End If

Exit_cmdSelectAll_Click:
Exit Sub

Err_cmdSelectAll_Click:
MsgBox Err.Description
Resume Exit_cmdSelectAll_Click

End Sub


The only thing is that when I use the SELECT ALL button, the function List2_Afterupdate doesn't work anymore. There must be a simple solution but I just can't figure it out. Can anyone please help me?

Tnx a lot!
 
Last edited:
Almost there

Can anyone please tell me what is wrong with this bit of script? I'm almost there but the value he writes in the textbox is duplicated. Everything that is selected in the listbox shows up 2 or 3 times like 1,1,2,3,1,2,3 instead of 1,2,3

Private Sub cmdSelectAll_Click()
On Error GoTo Err_cmdSelectAll_Click

Dim i As Integer

If cmdSelectAll.Caption = "Alles Selecteren" Then
For i = 0 To Me.List2.ListCount
Me.List2.Selected(i) = True


Dim Cursisten As String
Dim ctl As Control
Dim Itm As Variant

Set ctl = Me.List2

For Each Itm In ctl.ItemsSelected
If Len(Cursisten) = 0 Then
Cursisten = ctl.ItemData(Itm)
Else
Cursisten = Cursisten & "," & ctl.ItemData(Itm)
End If
Next Itm

Me.txtCursisten = Cursisten





Next i
cmdSelectAll.Caption = "Alles De-Selecteren"


Else
For i = 0 To Me.List2.ListCount
Me.List2.Selected(i) = False
Me!txtCursisten = Null

Next i
cmdSelectAll.Caption = "Alles Selecteren"


End If

Exit_cmdSelectAll_Click:
Exit Sub

Err_cmdSelectAll_Click:
MsgBox Err.Description
Resume Exit_cmdSelectAll_Click

End Sub


I would be so gratefull if anyone could help me! Sebastiaan
 
SeBasTiaan,

I pasted the code into a form and it worked fine.

Wayne
 
Not for me :(

WayneRyan said:
SeBasTiaan,

I pasted the code into a form and it worked fine.

Wayne

What bit of code worked fine? The Select All button works fine indeed, but the value into the txtbox not. Did that also worked fine with you??
 
SeBasTiaan,

I made List2 (filled it with "1,2,3,4,5,6,7,8,9"),

Made the textbox

Made the Command Button (it fires the code).

Pressing the Command Button put "1,2,3,4,5,6,7,8,9" in the textbox.

Post your db.

Wayne
 
Db

Ok, I will post my DB... need to make it a little smaller...
 
Remove unneeded stuff,
Tools --> Compact/Repair,
Then ZIP,
Then attach

Wayne
 
DB example

ok, I made a new DB with only the neccesary tables and forms...
 

Attachments

Last edited:
after update List2

Can it be that the List2_AfterUpdate function interferes with the select all button? It does exactly the same in the select all function, so when the select all button is clicked, the afterupdate also does his thing?

Sebastiaan...
 
Please help

Is there anyone who can help me with this last little problem? I'm finished after this and then I will post it with the example DB's for the rest of you!

Thnx again, SeBasTiaan
 
SeBasTiaan,

I downloaded & ran your sample. As I select/deselect items in the listbox, the
numbers appear in the textbox just fine.

Wayne
 
Select ALL

yes, I know that selecting items from the listbox works fine, but the SELECT ALL button doesn't work fine. please help again....
 
S,

Code:
Private Sub cmdSelectAll_Click()
On Error GoTo Err_cmdSelectAll_Click
Dim i As Integer

Me.txtCursisten = ""
If cmdSelectAll.Caption = "Alles Selecteren" Then
    For i = 0 To Me.List2.ListCount - 1
        Me.List2.Selected(i) = True
        Me.txtCursisten = Me.txtCursisten & Me.List2.ItemData(i) & ", "
        Next i
        Me.txtCursisten = Mid(Me.txtCursisten, 1, Len(Me.txtCursisten) - 2)
        cmdSelectAll.Caption = "Alles De-Selecteren"
Else
     For i = 0 To Me.List2.ListCount
        Me.List2.Selected(i) = False
        Me!txtCursisten = ""
        Next i
    cmdSelectAll.Caption = "Alles Selecteren"
End If

Exit_cmdSelectAll_Click:
    Exit Sub

Err_cmdSelectAll_Click:
    MsgBox Err.Description
    Resume Exit_cmdSelectAll_Click
    
End Sub

Wayne
 

Users who are viewing this thread

Back
Top Bottom