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!

Completion of Leave event sub closes form

Status
Not open for further replies.

sjulian

Programmer
Aug 15, 2000
56
US
My program calls a form with
Code:
frmAddLocation.ShowDialog()
Form has a couple of combo boxes and a some text boxes. On the leave event of the zipcode combo box I check the user entered zipcode against a master table and if it is not found, allow the user to add the zip after prompting for the City and State.
After the End Sub statement, it returns to the form show statement rather than staying in the frmAddLocaton form to complete the code there.
I assumed after completing the code in the combo leave sub, control would return to the form. What am I missing?
Code:
Private Sub cboZip_Leave(sender As Object, e As System.EventArgs) Handles cboZip.Leave
        Dim strSQL As New System.Text.StringBuilder
        Dim rsZip As New ADODB.Recordset
        Dim strTemp As String

        If cboZip.Text <> "" Then
            strTemp = Trim(Microsoft.VisualBasic.Left(cboZip.Text, 9))
            If IsNumeric(strTemp) = False Then
                strTemp = Microsoft.VisualBasic.Left(cboZip.Text, 5)
                If IsNumeric(strTemp) = False Then
                    MessageBox.Show("Invalid zipcode (Remove '-' from Zip+4)", MsgBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    cboZip.Focus()
                    Exit Sub
                End If
            End If
            strSQL.Clear()
            strSQL.Append("SELECT * FROM dba.zip_master WHERE zipid = '")
            strSQL.Append(strTemp)
            strSQL.Append("'")
            rsZip.Open(strSQL.ToString, dbIMS)
            If rsZip.EOF = True Then
                DialogResult = MessageBox.Show("Zipcode does not exist in master table, do you wish to add?", MsgBoxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If DialogResult = Windows.Forms.DialogResult.Yes Then
                    Dim strCity As String = InputBox("City:", MsgBoxTitle)
                    Dim strState As String = InputBox("State (2 letter abreviation):", MsgBoxTitle)
                    strSQL.Clear()
                    strSQL.Append("INSERT INTO dba.zip_master (zipid,city,state,createdby,createddt,srno) VALUES(")
                    strSQL.Append("'")
                    strSQL.Append(strTemp)                                              'zipid
                    strSQL.Append("','")
                    strSQL.Append(strCity)                                              'city
                    strSQL.Append("','")
                    strSQL.Append(strState)                                             'state
                    strSQL.Append("','")
                    strSQL.Append("cvinterface")                                        'createdby
                    strSQL.Append("','")
                    strSQL.Append(fnFormatDate(Now, "System", "Y-M-D H:M:SD"))          'createddt
                    strSQL.Append("',")
                    lngZipID = fnGetNextID("zip_master") + 1
                    strSQL.Append(lngZipID)                                              'srno
                    strSQL.Append(")")
                    dbIMS.Execute(strSQL.ToString)
                    strTemp = cboZip.Text & " " & strCity & ", " & strState
                    cboZip.Text = strTemp
                Else
                    rsZip.Close()
                    Exit Sub
                End If
            Else
                lngZipID = rsZip.Fields("srno").Value
                strTemp = rsZip.Fields("zipid").Value & " " & rsZip.Fields("city").Value & ", " & rsZip.Fields("state").Value
                cboZip.Text = strTemp
            End If
            rsZip.Close()
        End If
    End Sub
 
First of all I want to ask you in which event is the form.show statement?
Secondly did you try to cut paste the code of the leave event to a sub created by you? and call this sub from the point that you want so that when the sub will finish to return to the next statement? I checked how it works the leave event of a combo box step by step and it doesn't return somewhere else inside the code like form load, form shown event. Can you give more informations where exactly it returns and where do you want to return?

 
I have since worked around above problem but have encountered it again in different circumstances which leads me to believe the problem is not with the Leave event but with the way am calling and or trying to close a form.
In this case the form is opened by clicking a menu item on the main form
Code:
Private Sub mnuMaintainLocationMatchingToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles mnuMaintainLocationMatchingToolStripMenuItem.Click
    frmRematchLoc.ShowDialog()
End Sub
If the user selected from a combo box, a boolean (bolChangeMade) is set to "True".
If the Cancel button is clicked, the the following code is run
Code:
Private Sub cmdCancel_Click(sender As Object, e As System.EventArgs) Handles cmdCancel.Click
    If bolChangeMade = True Then
        DialogResult = MessageBox.Show("Cancel without saving changes?", MsgBoxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If DialogResult = Windows.Forms.DialogResult.No Then
            Exit Sub
        Else
            sbExitRoutine()
        End If
    Else
        sbExitRoutine()
    End If
End Sub
If the user clicks No in the messagebox the Exit Sub is executed, then the End Sub, then back to the Form Show command.
What I want to happen at the Exit Sub is for the form frmRematchLoc to stay open and wait for user action.
What am I missing?
 

Can you post the code where you actually close the form?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Your code is correct. I checked it.
After the exit sub, end sub is executed and then everything is ok.
Where is exactly the Form Show command??? I want a favour from you.
Please write us the piece of code of the event which the form show returns and executed.
 
Form show command is first block of code in my post above.
Code used to actually close the form:
Code:
Private Sub sbExitRoutine()
    'sbClearForm sets lbls, txtbox, & combos = ""
    sbClearForm()
    Me.Close()
End Sub
Thanks for looking at this, I'm stumped.
 
I ran it once more, it's ok!!! and if the user click on yes the form closes. if the user clicks on no then exit sub, end sub without the form been closed.
If form show command is first block of code, then you have to look there. If you run it step by step before the user click on the cansel button a piece of code is executed. If after cansel button, exit sub and end sub it returns to that piece then exactly in that piece of code is your problem. Even if is a sub of you, or something else. That I am trying to tell you.
 
Thank you all!
Antzelinaki, I found it before your last post, but you pointed me in the right direction.
I was calling the problem sub from code that would exit the form, so that when the problem sub completed, the form closed.
As always, it is something small that causes the biggest problems.
Again, thank you.
 
I am glad that I helped you. I have faced many times problems like this. That's why I told you to look to the sub which form show event executed, because your code was correct without any problems!!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top