Confused - setfocus with variable as control name

B

BusterB

Hi, (might be one for Douglas or Allen?)

After scouring this forum I am i bit confused.

I have created a Public Function that I call in the afterupdate event of
several text boxes on different forms and subforms.

This function checks for specific format of text. What I want to be able to
do is setfocus back to the control that called the fuction if the format was
incorrect.

The Function is "FormatDTG" and an example of the control calling it might
be "Forms!InstallationDetailsFrm!RigmoveSubFrm.Form!SignalReceivedDTG"

My confusion is this - in the afterupdate event of this control I want to
pass the control name to the function so that it knows where to setfocus back
to if format is wrong.

Do I pass this information as a string or control?

i.e Public Function FormatDTG(strDTGToFormat As String, ctlControlName As
Control) As String
or
Public Function FormatDTG(strDTGToFormat, strControlName As String) As String

Also I have found several examples in this forum of people doing similar
things like this but either in the Forms module(Me.) or Public module
refering to forms without subforms.

Help in this is appreciated, I have tied myself in knots (mentally) over this.

Thanks in advance.
 
R

Rick Brandt

BusterB said:
Hi, (might be one for Douglas or Allen?)

After scouring this forum I am i bit confused.

I have created a Public Function that I call in the afterupdate event
of several text boxes on different forms and subforms.

This function checks for specific format of text. What I want to be
able to do is setfocus back to the control that called the fuction if
the format was incorrect.

Simplify your life. Use BeforeUpdate instead of AfterUpdate. BeforeUpdate
has a Cancel argument that you can set to true when your validation fails.
Doing so means that the control never loses focus so there is no need to set
it back.
 
B

BusterB

Thanks for the quick reply Rick.

I was geting myself so tied up with finding out how to pass the control name
to a function as a variable that I lost sight of exactly what I wanted the
event to do.

However I would still like to know how to pass to a function the name of the
control that called it as it would be useful in other areas.

Thanks again.
 
K

Klatuu

If you want the name of the current control, it isn't necessary. In the
function you can use Screen.ActiveControl

If you want to pass a different control's name

= MyFunction(Me.SomeControl.Name)

You can also pass a reference to a control
= MyFunction(Me.SomeControl)

Then the function should type the argument as a control

Public Function MyFunction(ctl As Control)
 

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