Conditional closing of a form

D

Derek

In Outlook 2000 I have created a form that opens, builds
a list (in UserForm_Initialize) and displays the list in
a combo box.

If the list is empty I would like to display a message to
that effect (no problem!) and then close the form without
it being displayed.

This is the code I have written ...

Private Sub UserForm_Initialize()
' Perform the appropriate initialise routine
' according to the selection type.
Select Case g_intSelectType
Case c_intCategory
DoCategory
Case c_intParent
DoParent
Case c_intLocation
DoLocation
Case c_intNames
DoNames
End Select

' If there are no items to select from (except
' Categories), display an error and close the form.
If cmbItems.ListCount = 0 Then
Select Case g_intSelectType
Case c_intParent
MsgBox "No documents available."
' CODE TO CLOSE FORM HERE?
Case c_intCategory
' Do nothing
Case Else
MsgBox "No items to select."
' CODE TO CLOSE FORM HERE?
End Select
End If
End Sub

How do I do that?

Thanks,
Derek.
 
G

Guest

Hi Sue,

I originally tried Unload Me, but when the End Sub is
executed, it causes a run-time error 91: object variable
or with block variable not set.

Thanks,
Derek.
 
D

Derek

Just in case I missed something, this is my full original
code for this event:

Private Sub UserForm_Initialize()

On Error GoTo error_handler

' Name search gets a second parameter, which is
loaded into a text box for use when selecting.
' If the text box is invisible, it's contents are
inaccessible. Set the left property of the box
' so that it is off the form and invisible to the
user even thought the visible property is True.
lstKeys.Left = 1000

' Perform the appropriate initialise routine
according to the selection type.
Select Case g_intSelectType
Case c_intCategory
DoCategory
Case c_intParent
DoParent
Case c_intLocation
DoLocation
Case c_intNames
DoNames
End Select

exit_point:

' If there are no items to select from (except
Categories), display an error and close the form.
If cmbItems.ListCount = 0 Then
Select Case g_intSelectType
Case c_intParent
MsgBox "There are no documents available
for the categories selected."
Unload Me
Case c_intCategory
' Do nothing
Case Else
MsgBox "There are no items to select
from."
Unload Me
End Select
End If

frmNewEntry.StatusBar1.Panels(1).Text = "Ready"
Exit Sub

error_handler:

MsgBox "ERROR: " & Err.Number & " - " &
Err.Description
Resume exit_point

End Sub
 
S

Sue Mosher [MVP-Outlook]

I assumed that since you're using UserForm_Initialize, this is a VBA
userform. If it's not, please tell us what it is.
 
D

Derek

Sue,

This is indeed a userform, created using Insert->UserForm
from the Outlook toolbar.

Thanks,
Derek.
 
S

Sue Mosher [MVP-Outlook]

So it is a VBA form? Weird, because Me is the intrinsic object in a userform
that represents itself, so Unload Me should always work. Instead, I guess
you can try replacing Me with the name of the userform
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Yeah weird is right! ;-)

If I add a watch on Me, it shows up as I would expect as
the current form object. If I replace Me with the form
name I get the same error on the Exit Sub and End Sub
statements.

This has given me a headache! he he

Ta,
Derek.
 
S

Sue Mosher [MVP-Outlook]

Is the form modal or nonmodal?

Yeah weird is right! ;-)

If I add a watch on Me, it shows up as I would expect as
the current form object. If I replace Me with the form
name I get the same error on the Exit Sub and End Sub
statements.

This has given me a headache! he he

Ta,
Derek.
 
D

Derek

The form is called as vbModal. However I have tried it as
non-modal, but this seems to have no effect.

Thanks,
Derek.
 
S

Sue Mosher [MVP-Outlook]

Try this code on a new clean form:

Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Unload Me
End Sub

If that has the same problem, I'd conclude that the fault lies in VBA
itself. It might be time to export all modules as a backup and start over
with a new Vbaproject.otm file. It also wouldn't hurt to run Help | Detect
and Repair.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
D

Derek

I tried creating a new user form with only the following
code in it ...

Private Sub UserForm_Initialize()

Unload Me

End Sub

I ran this form independently and it caused the same
error as the other form when executing the End Sub
statement.
 
D

Derek

Sue,

I tried the code and it worked fine. However, I suspect
that is because the form is allowed to load completely
before attempting to unload.

Derek.
 
G

Guest

Sue,

Following along with the lines of the problem being
caused by the form not being fully loaded at the point I
try to unload it, I moved the check to the activate event.

With a hide at the beginning and a show only if there's
something in the list, apart from a split-second "blink"
of the form appearing, before it is hidden, all seems
well.

Thanks,
Derek.
 

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