OL 2003 VB Macro- How to close an open dialog box?

I have a macro that opens a dialog box with command buttons in it.  Each button opens a child dialog box with txt boxes and when Go is clicked a new OLmessage window is suppose to open and be prefilled with the txt box info and HTML content from the code in the macro.

I say "suppose to" becuase it stops here with an error that basically says the initial dialog box called FrmChoose is still open.

How do I close this dialog after clicking one of the command buttons?

I tried several things, I thought me.hide would do it but it does not help.  I don't understand modal and modeless, could it be the culprit?


Here is the code for FrmChoose

Private Sub CommandButton1_Click()
'Opens a new NFS form
Dim a As New NFS
a.Show
Me.hide
'unload me
'a.hide
End Sub

Private Sub CommandButton2_Click()
'opens a new ACAT form
Dim b As New ACAT
b.Show
Me.hide
'Unload Me
'b.hide
End Sub

Private Sub CommandButton3_Click()
'Opens a new IRA form
Dim c As New ira
c.Show
Me.hide
'Unload Me
'c.hide
End Sub

Thanks,
...Jeff

LVL 1
jshefferAsked:
Who is Participating?
 
David LeeCommented:
Hi jsheffer,

I don't believe you're going to be able to unload or close FrmChoose with this code.  The problem is that FrmChoose is calling the other form, transfering program control to it.  I'm pretty sure the second form has to be able to return to the calling form (FrmChoose).  The way around this is to change the approach.  You need a main routine that loads and displays FrmChoose.  When the user clicks one of the buttons FrmChoose returns a value indicating which button was clicked.  The code then checks that value and loads/displays the second form.  Something like this.

Public intChoice As Integer

Sub Main()
    Dim x As New FrmChoose, _
        y As Object
    FrmChoose.Show
    Select Case intChoice
        Case 1
            Set y = New NFS
        Case 2
            Set y = New ACAT
        Case 3
            Set y = New IRA
    End Select
    Y.Show
    Set x = Nothing
    Set y = Nothing
End Sub

The code in FrmChoose would be something like this.

Sub CommandButton1_Click()
    intChoice = 1
    Unload Me
End Sub

Sub CommandButton2_Click()
    intChoice = 1
    Unload Me
End Sub

Sub CommandButton3_Click()
    intChoice = 1
    Unload Me
End Sub



Cheers!
0
 
meintsiCommented:
Try ....

frm A.Show
Unload frmChoose
End Sub
0
 
jshefferAuthor Commented:
I'm pretty sure I tried that, but I'll try again.  Look for another post.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
jshefferAuthor Commented:
Same outcome.  Thanks for playing though : )
0
 
jshefferAuthor Commented:
BlueDevilFan, Thanks for the info.  I think I put it all in the right place.  Unfortunately when I step through the code, it does not work.

I've notice that stepping through, when it gets to the Case Select statements, it never hits the "set y=nfs" lines.  So I think that none of the cases are true.  It steps down to y.show and then I get a "Object Variable or With Block not set" error.

Here is the code which is cut and paste from your post.  Please let me know if I've put it in the wrong place.

"ThisOutlookSession"
Sub MAIN()
    Dim x As New FrmChoose, _
        y As Object
    FrmChoose.Show
    Select Case intChoice
        Case 1
            Set y = New NFS
        Case 2
            Set y = New ACAT
        Case 3
            Set y = New ira
    End Select
    y.Show
    Set x = Nothing
    Set y = Nothing
End Sub

"FrmChoose"
Sub MAIN()
    Dim x As New FrmChoose, _
        y As Object
    FrmChoose.Show
    Select Case intChoice
        Case 1
            Set y = New NFS
        Case 2
            Set y = New ACAT
        Case 3
            Set y = New ira
    End Select
    y.Show
    Set x = Nothing
    Set y = Nothing
End Sub
0
 
David LeeCommented:
Are you sure that's the code you're using?  It looks to me like the code for both the form and for the main sub are identical.  I also don't see any sign of the public variable to hold a the result of the button click.
0
 
jshefferAuthor Commented:
Sorry about that.  I was a bit hasty.

Here is the code for FrmChoose

Sub CommandButton1_Click()
    intChoice = 1
    Unload Me
End Sub

Sub CommandButton2_Click()
    intChoice = 1
    Unload Me
End Sub

Sub CommandButton3_Click()
    intChoice = 1
    Unload Me
End Sub

Can you eloaborate on the Public Variable comment.  I think I understand it in principle, but I don't know what to remedy it.

Thanks Again.  I've got to step out for a couple hours.  I'll be back on it as soon as I return.
..Jeff
0
 
David LeeCommented:
The public variable I mentioned is a variable declared as being Public (i.e visible to all procedures) outside of any module.  That's required in this case because any variable declared within FrmChoose is going to be destroyed when FrmChose unloads, leaving nothing to tell us what selection the user chose.  Make sense?

No problem on being away.  I have to step away shortly too and am not sure when I'll be back.
0
 
jshefferAuthor Commented:
Okay, so the statement "Public intChoice As Integer" which I placed in "thisOutlooksession" (also accidently omitted from my first code post) is in the wrong place????  Would you agree?

Public intChoice As Integer
Sub MAIN()
    Dim x As New FrmChoose, _
        y As Object
    FrmChoose.Show
    Select Case intChoice
        Case 1
            Set y = New NFS
        Case 2
            Set y = New ACAT
        Case 3
            Set y = New ira
    End Select
    y.Show
    Set x = Nothing
    Set y = Nothing
End Sub
0
 
David LeeCommented:
No, it's in the right place.
0
 
jshefferAuthor Commented:
Well I commented it out of "thisOutlookSession" and added it to module1 and it seems to work great.  Put it back in "thisoutlooksession" and still doesn't work.  Am I okay to leave it in module1?
0
 
David LeeCommented:
Yes, it's okay to leave it in Module1.
0
 
jshefferAuthor Commented:
Thanks, you're awesome.
0
 
David LeeCommented:
Thanks.  No problem.
0
 
jshefferAuthor Commented:
I've got one more hurdle to overcome.  I'm posting a new 500 pt. question.  Has to do with saving a copy of this message in a public folder after sending.
0
 
David LeeCommented:
Ok.  I'll watch for it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.