twpc
twpc

Reputation: 709

Visual Basic - how to force event to fire

I'm new to VB (using VBA, actually) and I would like to force an event to fire. Specifically, I am updating the value in a textbox and I would like to call that textbox's AfterUpdate() event.

Private Sub cmdCreateInvoice_Click()
  Me.txtInvDate = '11/01/10'
  Me.txtInvDate.AfterUpdate
End Sub

During run time, I get an error that says "Compile Error: Invalid Use of Property". If I try something similar, but with the click event (ex: cmdCreateInvoice.Click), which does NOT have a property that shares the name, I get an error that says "Compile Error: Method or Data member not found".
I know there must be a way to fire one event from another. But what is the proper syntax? Thanks!

Upvotes: 1

Views: 32817

Answers (5)

roldy
roldy

Reputation: 95

Is there a way to keep a textboxes event firing until I click on another text box? I'm needing to stay "in" the text box which has Do While loop for a selection. Once the selection is made, I want the user to be able to replace the selection without having to click on the text box again. If the user is satisfied with the select, they can click on a different text box.

Upvotes: 0

xstack
xstack

Reputation: 1

Some code apparently will (regretfully) force events to fire:

    Me.Dirty = False

forces BeforeUpdate and AfterUpdate to fire, as I understand it. There are probably more cases too. Any secret hooks which attach to your code in that manner reflect bad design in my view and will inevitably lead to inadvertant & frustrating loops being created.

Upvotes: 0

RolandTumble
RolandTumble

Reputation: 4703

AFAIK, there is no way to "fire an event manually" in VB(A). What you can do is call the event handler manually, and for this, rdkleine has given you the answer already:

Call txtInvDate_AfterUpdate()

This will have exactly the same effect as if the event had fired (though it does not give you the whole chain of events that may also fire along with it--unless you Call their handlers as well).

IgorM has another valid point, in comments on his answer--it's "cleaner" to write a different Sub to do the work you want done, then call it from both the event handler & wherever you're trying to do it now (button click?). So:

Private Sub txtInvDate_AfterUpdate()
    DoWhatever
End Sub

Private Sub button_Click()
    DoWhatever
End Sub

Private Sub DoWhatever
    'your desired effect
End Sub

You could even make DoWhatever a Public Sub in a Module.

Edit

And no, in VB(A) it doesn't matter what order you define your Sub (or Function) routines.

Upvotes: 11

Ralf de Kleine
Ralf de Kleine

Reputation: 11745

Call

txtInvDate_AfterUpdate()

Upvotes: 3

Igor
Igor

Reputation: 1486

Try to use something like this:

Private Sub TextBox1_LostFocus()
  TextBox1.Value = "5"
End Sub

Use LostFocus event if you change value manually.

Or you can use another way:

Private Sub CommandButton1_Click()
   TextBox1.Value = "new value"
End Sub

Private Sub TextBox1_Change()
   MsgBox TextBox1.Value
End Sub

Try to use Change event.

Upvotes: 0

Related Questions