Can I force a control to do BeforeUpdate event from VB?

F

Fjordur

Hi,

I have VB code that assigns a value to controls, used for several controls
on several forms.
OTOH some of these controls (not all) have a beforeUpdate code that does
some checking.

Is it possible in the VB code, after I have set the value of the control, to
tell it to execute its own BeforeUpdate event, without knowing if it has
that code or not?

Thanks for the help,
 
T

TC

Sure. Say the name of a control, is zzz. The following code will
execute the BeforeUpdate event of that control, if it has one:

on error resume next
zzz_BeforeUpdate 0
on error goto 0

But that is not the way to go. If your "manual" code, and the
BeforeUpdate event, need to run the same code, you should put that code
in a Function, so you can execute it from either place:

function blah() as boolean
...
blah = ... ' True for ok, False for not.
end function

...

sub zzz_BeforeUpdate (Cancel as Integer)
cancel = blah()
end sub

...

... = blah()

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
F

Fjordur

TC said:
Sure. Say the name of a control, is zzz. The following code will
execute the BeforeUpdate event of that control, if it has one:
on error resume next
zzz_BeforeUpdate 0
on error goto 0
Thanks. But actually my code goes like this:
Public gtxtCtrl As TextBox
Set gtxtCtrl = whatever control was passed as parameter
(...let user choose data..)
gtxtCtrl = the data
here i want to call the method BeforeUpdate for the control gtxtCtrl
I tried
CallByName gtxtCtrl , "BeforeUpdate", VbMethod , 0
but that line is executed silently and does not enter the BeforeUpdate
method of the control. Then
gtxtCtrl .BeforeUpdate (0)
is not syntactically correct.

Can you help further?
But that is not the way to go. If your "manual" code, and the
BeforeUpdate event, need to run the same code> But that is not the way to
go. If your "manual" code, and the
BeforeUpdate event, need to run the same code
No, they don't. The manual, general purpose, code lets the user pick a value
from a list. The beforeUpdate does some coherency checking, hiliting of
controls etc...
 
T

TC

Fjordur said:
CallByName gtxtCtrl , "BeforeUpdate", VbMethod , 0
but that line is executed silently and does not enter the BeforeUpdate
method of the control.

I'm not surprised.

I told you how to call the BeforeUpdate event of your control.

Did you try what I told you?

TC (MVP Access)
http://tc2.atspace.com
 
F

Fjordur

TC said:
I told you how to call the BeforeUpdate event of your control.
Did you try what I told you?
Of course I did.
If I do it litterally it gives code like
Public gtxtCtrl As TextBox
Set gtxtCtrl = whatever control was passed as parameter
(...let user choose data..)
gtxtCtrl = the data
gtxtCtrl _BeforeUpdate 0
which I didn't think could work (it doesn't) as gtxtCtrl is an object in my
code.
I tried to find another way (CallByName) before I posted more info about my
problem. I just can't seem to find how to call an event method of an object.
If VB were a pure object oriented language, I guess it would be someting
like gtxtCtrl .BeforeUpdate(0) which why I tried this, too, not with much
hope.
 
T

TC

Ok, you didn't say that the name of the control was only known at
runtime :)

If the name of the control is in the string variable s, you could maybe
call the BeforeUpdate event of that control, with something like this:

eval "s" & "_BeforeUpdate"
or: eval "s" & "_BeforeUpdate()"
or: eval "s" & "_BeforeUpdate 0"
or: eval "s" & "_BeforeUpdate(0)"

You may need to declare the BeforeUpodate procedure, as Public.

However - none of this alters what I said before: it is bad programming
practice to call an event procedure like that. You should move the
event procedure's code to a seperate procedure or function, so the
event - and your other code - can call that procedure or function
when-ever required.

HTH,
TC (MVP Access)
http://tc2.atspace.com
(off for dinner!)
 
G

Guest

I think I can help clear this up. I agree with TC regarding calling event
code. In a case where you don't know what control will be used for the
function, you can pass the control as a control, not a name, then the
function will know what to do with it:

Call it like:
Cancel = FooBah(Me.SomeControlName)

Function FooBah(ctl As Control) As Boolean
If ctl = "FunAndGame" Then
FooBah = True
Else
FooBah = False
End If
End Function
 
T

TC

But I think he wants to call the existing BeforeUpdate event, of a
control whose name is only known at runtime.

That's why he tried: gtxtCtrl _BeforeUpdate 0, where he set the
variable gtxtCtrl to the control in question.

Cheers,
TC (MVP Access)
http://tc2.atspace.com
 
R

RoyVidar

Fjordur wrote in message <[email protected]> :

I think that if the control has focus (setfocus if necessary, then
assign the .Text property of it, the before and after update events of
the controls will fire. I haven't tested this vigourously, and it might
disturb other necessary features of the control(s).

Note that if the before update is cancelled, trap for runtime error
2101 "The setting you entered isn't valid for this property"
Thanks. But actually my code goes like this:
Public gtxtCtrl As TextBox
Set gtxtCtrl = whatever control was passed as parameter
(...let user choose data..)

' unless the control alredy has focus
gtxtCtrl.SetFocus
gtxtCtrl.Text = the data

' handle 2101, and continue
 
F

Fjordur

TC said:
But I think he wants to call the existing BeforeUpdate event, of a
control whose name is only known at runtime.
That's right. Eval doesn't seem to trigger the event, can't understand
why..

It doesn't look like bad practice to trigger an existing event when I modify
a control in the code. Actually it looks like very good practice to trigger
it, exactly as if the control had been modified manually. This allows to
keep the code and the event independant which IS good practice. One might
even argue that this should be the default behavior.
Not being able to trigger the event, I've had to include in my
value-selecting code a call to the sub that the BeforeUpdate calls, although
they have nothing in common.
 
T

TC

It is bad coding practice to trigger an event "manually" for the
purpose of running the code in that event. If you want to "manually"
run the code within an event, you should put that code in a seperate
procedure or function for that purpose.

Say you have a form which runs some code when you query a record then
click a button. Now you want to run that code "manually". Would you do
that by writing code (say using Sendkeys) to open the form, query the
record & then click the button? Of course not. You'd remove the code
that you want to call "manually", and put it in a procedure & function
so you (and the form) can both call it from there.

Apart from anything else - this would solve the problem you're
currently having!

HTH,
TC (MVP Access)
http://tc2.atspace.com
 

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