Enable Button based on Two list boxes

Y

YorkieU

Please help with some logic.
I have two list boxes and one command button. I want the button to be
disabled until BOTH list boxes are selected. I am having some dificulty with
the logic especially when the list boxes are requeried. When the requery
causes either or both of the list boxes to not have a selection, how do I
make the button disabled until both boxes have a selection again? Thanks for
some help!
 
J

Jack Leach

In your form's module, make a private sub to check this and set the button
enabled depending on the values. At some point your code is requerying the
list boxes, so in that same event, call this sub to check the values. You
will also want to call it from the afterupdate event of the list boxes, and
probably the OnCurrent event of the form.

Ex:

Private Sub ListBox1_AfterUpdate()
Call psSetCommandButton()
End Sub

Private Sub psSetCommandButton()
If (Len(Nz(Me.ListBox1, "")) <> 0) AND _
(Len(Nz(Me.ListBox2, "")) <> 0) Then

'both have a selection
Me.CommandButton.Enabled = True

Else

'one of them doesn't have a selection
Me.CommandButton.Enabled = False

End If
End Sub



Note though, that ListBoxes have various ways to get the current selection
depending on the properties of the control. You may have to play around with
the code a little to get it to work in your situation, but this should be a
pretty good example to get what you need.

hth

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
J

John Smith

Sorry but I think that you would need:

Me.CommandButton.Enabled = Len(Nz(Me.ListBox1,"")) And Len(Nz(Me.ListBox2,""))

to check that both have entries.

HTH
John
##################################
Don't Print - Save trees
 
K

Klatuu

John is incorrect, Bruce is correct. Actually it should be:

Me.CommandButton.Enabled = Trim(Len(Nz(Me.ListBox1,"")) > 0 And
Trim(Len(Nz(Me.ListBox1,"")) > 0

That way it will require some value in both controls.
 
J

John Smith

My version would have worked because anything other than zero is true but
unfortunately I left out the 'NOT' that should have been after the equals so it
would have done the reverse of what was intended.

John
##################################
Don't Print - Save trees

Klatuu wrote:
John is incorrect, Bruce is correct. Actually it should be:
Me.CommandButton.Enabled = Trim(Len(Nz(Me.ListBox1,"")) > 0 And
Trim(Len(Nz(Me.ListBox1,"")) > 0
That way it will require some value in both controls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top