Ending A Process

ddrew

seasoned user
Local time
Today, 16:09
Joined
Jan 26, 2003
Messages
911
I have a form with a button on that closes the current form and opens a new form
Code:
Private Sub btn_AddContact_Click()
On Error GoTo btn_AddContact_Click_Err

    DoCmd.OpenForm "frm_Contacts", acNormal, "", "", , acNormal
    DoCmd.Close acForm, "frm_AddToLists"


btn_AddContact_Click_Exit:
    Exit Sub

btn_AddContact_Click_Err:
    MsgBox Error$
    Resume btn_AddContact_Click_Exit

End Sub
And it works fine, on the same button I also use the On Mouse Move Event
Code:
Private Sub btn_AddContact_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    ControlTip Me.Name, Me.btn_AddContact.Tag, 1
End Sub
I also have a function that works with the Mouse Move Function:
Code:
Private Function ControlTip(ByVal frmName As String, Optional strtext As String = "Welcome User: ", Optional xswitch As Integer = 0)
On Error GoTo ControlTip_Err
With Forms(frmName).Controls("lblTip")

    Select Case xswitch
        Case 1
            If .Caption <> strtext Then
                .Caption = strtext
            End If
        Case Else
            If .Caption <> strtext Then
                .Caption = strtext & CurrentUser
            End If
    End Select
End With

ControlTip_Exit:
Exit Function

ControlTip_Err:
MsgBox Err.Description, , "ControlTip()"
Resume ControlTip_Exit
End Function
My problem is that when I use the button to close the form and open the new one, the Move Mouse Function is still looking for the Form frm_AddToLists, so produces an error.
I guess I need to put some code in the On Click event to stop the On Mouse Move function.

How do I do that please?
 
With a module variable:

Code:
Private AddContactClicked As Boolean

Then

Code:
Private Sub btn_AddContact_Click()
    AddContactClicked = True
    On Error GoTo btn_AddContact_Click_Err
    DoCmd.OpenForm "frm_Contacts"
    DoCmd.Close acForm, Me.Name
btn_AddContact_Click_Exit:
    AddContactClicked = False
    Exit Sub
btn_AddContact_Click_Err:
    MsgBox Error$
    Resume btn_AddContact_Click_Exit
End Sub

And

Code:
Private Sub btn_AddContact_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Not AddContactClicked Then ControlTip Me.Name, Me.btn_AddContact.Tag, 1
End Sub
 
Last edited:
Thanks for that, I've put it all in place except this piece:
Code:
Private AddContactClicked As Boolean
where do I put this?
 
In the form's module at the top beneath the Option lines (before and not inside the subs and functions).
 
Ok, did that but when the new form comes up I get a Message as follows:

ControlTip()

Microsoft Access cannot find the referenced form 'frm_AddToLists'.

With an 'OK' button
 
You;ve included the
If Not AddContactClicked Then
line in the MouseMove event and the
AddContactClicked = True
line in the Click event?

If so then it's probably easiest just to handle the error and ignore it.
 
Yup, I copied and pasted in your code. I will strip all out my DB and post the two forms so that maybe you could take a look, hope you dont mind!
 
Oh I see, it happens when the form loads.

Yeah I think I'd just handle the error there (probably easiest and wouldn't cause any problems that I can see):

Code:
Private Sub btn_AddContact_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error GoTo btn_AddContact_MouseMove_Exit
    If Not AddContactClicked Then ControlTip Me.Name, Me.btn_AddContact.Tag, 1
btn_AddContact_MouseMove_Exit:
    Exit Sub
End Sub

Which would solve your initial problem anyway :o

In fact you could just take out the line

MsgBox Err.Description, , "ControlTip()"

from the ControlTip function. Does the user need to know if there's an error there? I'd even go so far as to say the ControlTip function should just On Error Resume Next: it does what it can quietly and doesn't bother the user either way.
 
Last edited:
Heres a stripped down version theres no tables or anthing. Open up the form frm_AddToList and tehn click on Add Contact. Thanks
 

Attachments

One other thing: passing the name of the form to the ControlTip function and then have the function lookup a label called lblTip on the form isn't a great way to do it. It's not very encapsulated. A better way would be to pass the label itself so that ControlTip would work no matter what the label was called:

Code:
Private Sub ControlTip(ByRef lblTip As Label, Optional strtext As String = "Welcome User: ", Optional xswitch As Integer = 0)
    On Error Resume Next
    With lblTip
         If .Caption <> strtext Then .Caption = strtext & IIf(xswitch = 1,"",CurrentUser)
    End With
End Sub

And you'd change the MouseMove code to:

Code:
Private Sub btn_AddContact_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error Resume Next
    If Not AddContactClicked Then ControlTip Me.lblTip, Me.btn_AddContact.Tag, 1
End Sub
 
Worked a treat, thanks for all your help. I can apply it to all my other buttons accordingly now.
 

Users who are viewing this thread

Back
Top Bottom