View Full Version : Solved: List Box - force selection
tallen
12-09-2010, 10:25 AM
I have a list box that works fine, however, I would like to force a person to make a selection from the list before moving on to my next form. At the moment when I click OK it just loads up my next form. Is this possible?
Private Sub CommandButton1_Click()
'list box 3
Dim pWardAccess As String
Dim i As Long
Selection.GoTo What:=wdGoToBookmark, Name:="WardAccess"
pWardAccess = ""
With Me.ListBox3
For i = 0 To .ListCount - 1
If .Selected(i) Then
pWardAccess = pWardAccess & .List(i) & ", "
End If
Next i
End With
pWardAccess = Left(pWardAccess, Len(pWardAccess) - 2) 'Strip off last comma and space.
ActiveDocument.Bookmarks("WardAccess").Range.InsertAfter pWardAccess
Unload UserForm3
Load UserForm4
End Sub
Private Sub Label1_Click()
End Sub
Private Sub UserForm_Initialize()
'list box for Wards
With Me.ListBox3
.AddItem "ABC Ward"
.AddItem "DEF Ward"
.AddItem "Heart"
.AddItem "Bloods"
.AddItem "Baby Unit"
End With
End Sub
Private Sub CommandButton2_Click()
End
End Sub
fumei
12-09-2010, 10:33 AM
Please use the VBA code tags when posting code. One way (there are others) is a boolean variable whose value is dtermined by whether Selected is True.
Dim bolYeah As Boolean
With Me.ListBox3
For i = 0 To .ListCount - 1
If .Selected(i) Then
pWardAccess = pWardAccess & .List(i) & ", "
bolYeah = True
End If
Next i
End With
If bolYeah = False Then
MsgBox "Nothing has been selected. Exiting."
Exit Sub
End If
tallen
12-09-2010, 10:47 AM
Sorry, I am new to this, I did paste in all of my code???
Thanks for your example, however I do not want it to exit, I would like the form to be displayed again so that they have to select an item - although, I suppose they may have selected the form in error, the choice to exit or go back to the form would be great. Thanks for your help.
gmaxey
12-09-2010, 11:01 AM
In cases like this I prefer to be proactive and prevent clicking OK until the necessary controls are complete. In the example below the OK command button is initially disabled and stays that way until the ListBox selection is made:
Private Sub ListBox1_Change()
If Me.ListBox1.ListIndex > -1 Then
Me.CommandButton1.Enabled = True
Else
Me.CommandButton1.Enabled = False
End If
End Sub
Private Sub UserForm_Initialize()
Me.CommandButton1.Enabled = False
Me.ListBox1.AddItem "1"
Me.ListBox1.AddItem "2"
End Sub
I have a list box that works fine, however, I would like to force a person to make a selection from the list before moving on to my next form. At the moment when I click OK it just loads up my next form. Is this possible?
Private Sub CommandButton1_Click()
'list box 3
Dim pWardAccess As String
Dim i As Long
Selection.GoTo What:=wdGoToBookmark, Name:="WardAccess"
pWardAccess = ""
With Me.ListBox3
For i = 0 To .ListCount - 1
If .Selected(i) Then
pWardAccess = pWardAccess & .List(i) & ", "
End If
Next i
End With
pWardAccess = Left(pWardAccess, Len(pWardAccess) - 2) 'Strip off last comma and space.
ActiveDocument.Bookmarks("WardAccess").Range.InsertAfter pWardAccess
Unload UserForm3
Load UserForm4
End Sub
Private Sub Label1_Click()
End Sub
Private Sub UserForm_Initialize()
'list box for Wards
With Me.ListBox3
.AddItem "ABC Ward"
.AddItem "DEF Ward"
.AddItem "Heart"
.AddItem "Bloods"
.AddItem "Baby Unit"
End With
End Sub
Private Sub CommandButton2_Click()
End
End Sub
fumei
12-09-2010, 11:50 AM
I am inclined to agree with Greg.
"Thanks for your example, however I do not want it to exit, I would like the form to be displayed again so that they have to select an item - although, I suppose they may have selected the form in error, the choice to exit or go back to the form would be great."
It only exited the Sub that was doing the action (your commandbutton_click). It would return focus to the userform. The userform is not exited.
fumei
12-09-2010, 11:54 AM
tallen, are you following what greg is suggesting?
The userform starts with the commandbutton disabled. It can NOT be clicked. It can only be clicked if you change the listbox to a listed item. In other words, unless you DO select something you can NOT click the commandbutton.
tallen
12-10-2010, 03:15 AM
Yes, I have just done it and it works. Thanks so much.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.