Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I close forms w/o letting them run any more code?

Status
Not open for further replies.

brailian

Programmer
Oct 11, 2001
11
US
This might be a tough one. Does anyone know of a way to close a bunch of modal forms without letting them execute any more code? For example, I want to close forms 2 and 3. Form3 is called modally from a command button on form2 (see code below). How do I kill both forms but prevent the msgboxes from running?

Private Sub Command1_Click()
Dim frm As Form3

Set frm = New Form3
frm.Show vbModal
Set frm = Nothing
MsgBox "i told you so"
MsgBox "did not"
MsgBox "did too"

End Sub

Ideas, examples, suggestions appreciated.
 
Since this abort process is part of your application's algorithm, you have to code for it.

Use a global status flag, Base action following the return from an invoked form on this flags value. For example
[tt]
Set frm = New Form3
frm.Show vbModal
Set frm = Nothing
if Not gbAbort then
MsgBox "i told you so"
MsgBox "did not"
MsgBox "did too"
end if
[/tt]
If you want a dead stop there is the terrible END statement.



Wil Mead
wmead@optonline.net

 
Do not use frm.Show vbModal outside of Form3
Try This
Code:
    Dim blnCancel as boolean
    Set frm = New Form3
    blnCancel = frm.MyShow 
    Set frm = Nothing
    if blnCancel then exit sub
    MsgBox "i told you so"
    MsgBox "did not"
    MsgBox "did too"
'..In Form3.........
Private mblnCancel as boolean ' Form Level variable
Public Function MyShow() as Boolean
    ' Wait until HIDE (Do Not UNLOAD elsewhere)    ' 
    Me.Show vbModal 
    Unload Me
    MyShow = blnCancel
End Function

Compare Code (Text)
Generate Sort in VB or VBScript
 
Minor correction.
Code:
Private mblnCancel as boolean ' Form Level variable
Public Function MyShow() as Boolean
    ' Wait until HIDE (Do Not UNLOAD elsewhere)    ' 
    Me.Show vbModal
    DIM BLNCANCEL AS BOOLEAN
    ' Accessing Form Level variable after Unload can, could
    ' maybe did sometime cause problems. 
    ' Better to stash in local variable before Unload.
    BLNCANCEL = MBLNCANCEL
    Unload Me
    MyShow = blnCancel
End Function
Compare Code (Text)
Generate Sort in VB or VBScript
 
Hmm, I was afraid that would be the only answer. I can't use End because (a) I hate it and (b) I'm actually just exiting all but the first form. Unfortunately, if I were to call form3 from a proc called by a proc called by a proc called by the form2 command button, then I would have to play the same return code game all the way back to prevent executing ANY form2 code. :( Oh well.

Hmm, maybe I could put forms2-X inside an ActX EXE and terminate the process somehow...?
 
Can you use state loop to manage your programs execution?

The loop invokes procedures and/or forms based on some "state" variable in a select statement. By collecting the driving force to your application into a single place the If Abort also only needed in one place. State machine programming is a different, some say wild, style of coding. It comes in handy in many situations. This is easy to visualize when there are a few states, lots of states make it cumbersome, sometimes overly so.

Each states procedure simply sets the next state by setting the "State Variable" and the loop manages the transition. This can even be automaticly done at the end of the loop. Your solution might include an abort or restart state to unload all forms and restart. Identifying and designing the states is the trick. After that it's a down hill slide.

Data validation is a good use of this method... You have alist of things that need to be validated and want to pop the user to the invalid field when there is an error. I'm not throwing in a variable message, but that's easily added... May not solve your problem directly but perhaps you've coded this in the conventional way and can make the connections.

[tt]
private enum eState
eUserId = 0
ePassword = 1
eField2 = 2
eField3 = 3
eField4 = 4
eField5 = 5
eField6 = 6
eDone = 7
eError = 8
end enum

Do While iState < eDone
select case iState
Case of eUSerId
ctl = txtUserId
if 6 < len(ctl.Text) then iState = eError
Case ePassword
ctl = txtPassword
if 8 < len(ctl.Text) then iState = eError
.
.
.
End Select

' Auto increment state with error trap

If iState = eError Then
ctl.Setfocus
msgBox &quot;Gimme good stuff!&quot;
Else ' Next state
iState = iState + 1
End If
Loop

[/tt]



Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top