ListBox / UserForm interaction

L

Luke

I have a UserForm1 with a ListBox1 in it. When I click on an entry from
ListBox1, UserForm2 appears. UserForm2 has the equivalent of "OK" and
"Cancel" buttons on it (among other things).

I had been so busy testing other features that it never occurred to me to
test the Cancel button until now. What I found is that, when Cancel is
clicked, UserForm2 unloads (as it should) and then immediately reappears.
The "UserForm2.Show" line of code is the first line in the ListBox1_Click()
sub. I assumed that, since an entry was still selected in ListBox1, it was
counting that as a click and re-running the ListBox1_Click() sub.

To fix this, I added this line of code to the CommandButton2_Click() sub
(the Cancel button): UserForm1.ListBox1.ListIndex = 0. That line of code
came after the "Unload UserForm2" line in the same sub. However, I then got
a Run-time error 400: Form already displayed; can't show modally. The
highlighted line of code was the UserForm2.Show line in the ListBox1_Click()
sub.

How can I click the Cancel button on UserForm2, unload UserForm2, and have
ListBox1 (in UserForm1) sitting there ready for me to choose a new entry?
 
C

Chip Pearson

It would be helpful if you posted the relevant code so that one could
try to recreate your situation. Based on your description, I created
Userform1 with a ListBox containing 10 items. The click event is this:

Private Sub ListBox1_Click()
UserForm2.Show vbModal
End Sub

This shows UserForm2 modally. Userform2 has a Cancel button attached
to the code

Private Sub btnCancel_Click()
Unload Me
End Sub

When I display Userform1 and click in ListBox1, Userform2 displays
until closed. Userform1 remains open with the same item still
selected in the ListBox. Userform2 does not reappear until another
click on ListBox1 on UserForm1.

I suspect that if you posted the relevant code (enough to faithfully
recreate the problem, but no more than that), someone will find out
what is really going on.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
 
L

Luke

Never mind.

Seeing "vbModal" below got me thinking. Both UserForm1 and UserForm2 had
ShowModal set to True. I changed them both to False and everything worked
like I wanted.

Sorry to have taken up your time. I should have thought of this in the
beginning.
 

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