Calling an eventprocedure

H

Hans Petter

I have a userform with a textbox. How do I call the _Exit procedure
(TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) ) for this textbox
from another procedure?


I am trying to do this:
Sub Another()
If Not Test Then ' Boolean test fails
' Call the exit procedure for the textbox
Call TextBox_Exit ' Here I get an error
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
(do something clever)
End Sub

Anybody?
 
T

Tom Ogilvy

Where is the Sub Another located?

Sub Another()
If Not Test Then ' Boolean test fails
' Call the exit procedure for the textbox
Call Userform1.TextBox_Exit ' Here I get an error
End Sub

' note: declare public if Another is outside useform module
Public Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
(do something clever)
End Sub

possibly is what you want.

Easier to put common code like this in a separate sub
Sub TakeAction()
' do something clever
end Sub

Sub Another()
If Not Test Then ' Boolean test fails
TakeAction
End if
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TakeAction
End Sub
 
H

Hans Petter

Both subs are in the same userform module. My first impulse was to create a
workaround with TakeAction as you describe. I did not make it work because I
have to alter the Boolean Cancel. If user input is not valid, the Event
status must be assigned True to prevent the control from losing focus, and
in this particular case, I have to trigger the _Exit event from another sub,
not by the event itself. The Call Userform1.TextBox_Exit did not work.



When I try this: Call TextBox_Exit the error message is: Argument not
optional

To narrow it down, I understand there has to be passed over an argument to
the Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
procedure / event. I have tried to pass over a module level Cancelled, but
get the error message Type mismatch.



Is there a way I can set the event status from outside the _Exit sub/event?
Or what argument has to be passed to the _Exit sub/event? I have looked at
the RaiseEvent statement, but the _Exit sub/event is not explicitly declared
in the form module, and can not be used





Thank you for answering
 
T

Tom Ogilvy

If the event is not being triggered by the user exiting the textbox or
whatever control it is, I am not sure setting cancel to false would have any
effect.
 

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