Detecting first OnCurrent

  • Thread starter Thread starter Sam Hobbs
  • Start date Start date
S

Sam Hobbs

In the OnCurrent event, how can I determine that it is the first occurrence?
There are probably many ways to do this.

What I was trying to do is to check to see if the subform (actually the form
in the subform) is valid. I have a form with two subforms and one subform is
accessing the other subform. This works, except not for the first occurrence
OnCurrent event.

So I am looking for a function such as IsObject or VarType that can
determine that the (form in the) subform is valid. I can probably use "On
Error" if there is not a more direct way.

What would be a good way to detect that the OnCurrent event is the first
occurrence?
 
You could create a module-level boolean variable.
It initializes to False.
Set it to True as the last line of Form_Current.
If it is False, it is the first time the routine is called.

Last time I struck this, I ended up just trapping and ignoring the error
though. In recent versions of Access, some of the events get called muliple
times where they did not previously, so just trapping the error sounds like
the more stable way to handle this.
 
Thank you. I assume thare is not a more elegant solution, since I am sure
you would know about it.

I am not sure I understand what you mean by "trapping and ignoring the
error". I am setting a textbox in the other form to a value. If you mean
that I can ignore the error from using the textbox, then I will assume I
understand.

Yes, I sure do understand that sometimes events occur more times than we
expect. I am expecting that as a possibility. So that is probably why the
error can be ignored. I suppose I would prefer to be more certain that that
is the error being ignored.

Ignoring the error can work for now at least.
 
Of course I can use Err.Number to determine that the error is the problem I
asked about.

Unfortunately, simply ignoring the error prevents the textbox in the other
form from being set the first time. I expect I can solve that problem
somehow, so I will work on it.
 
Yes, I can do the reverse in the OnLoad of the other form to get the first
textbox in the other form to be set to the initial value. In other words,
instead of setting the textbox in the other form in the OnCurrent of the
first subform, I can initially set it in the OnLoad for the form that the
textbox is in.
 
And the following also works for bypassing the first time; this is a
sef-contained solution for use in Form_Current (and could be used many other
palces too, correct?).

Static FirstTime As Boolean
If Not FirstTime Then
FirstTime = True
Exit Sub
End If
 
Yes: the static within the event does work also. Has the same disadvantage
that if a future version of Access calls the event more than once, your code
will break.

Looks like you have lots of options now.
 
Back
Top