Referring to a Text Box not the value of the Text Box in code

D

Design by Sue

We’d like to store the reference (for lack of a better word) of a control in
a variable for future use. For example:

Me.Suffix1 is a text box on a form so things like Me.Suffix1.SetFocus will
set the focus to the text box. When we loose focus on this text box we’d
like to do some like:

ctrLastFocus = Me.Suffix1

where in a module we have

Public ctrLastFocus as Control

Then elsewhere we could write something like: ctrLastFocus.SetFocus
Of course this doesn’t work as Me.Suffix1 isn’t the control, but the value
of it. How do you do this?

Thanks
Sue
 
K

Klatuu

If what you want to know is what was the last control to have the focus, you
can just use:
Screen.PreviousControl

Dim ctl As Control

Set ctl = Screen.PreviousControl
 
B

Bob Quintal

=?Utf-8?B?RGVzaWduIGJ5IFN1ZQ==?=
We’d like to store the reference (for lack of a better word) of
a control in a variable for future use. For example:

Me.Suffix1 is a text box on a form so things like
Me.Suffix1.SetFocus will set the focus to the text box. When we
loose focus on this text box we’d like to do some like:

ctrLastFocus = Me.Suffix1

where in a module we have

Public ctrLastFocus as Control

Then elsewhere we could write something like:
ctrLastFocus.SetFocus Of course this doesn’t work as Me.Suffix1
isn’t the control, but the value of it. How do you do this?

Thanks
Sue
Option Compare Database
Option Explicit
Dim ctrlastfocus As Control

Private Sub CmdGoBack_Click()
ctrlastfocus.SetFocus
End Sub

Private Sub Text2_Enter()
Set ctrlastfocus = Me.Text2
End Sub

Private Sub Text4_Enter()
Set ctrlastfocus = Me.Text4
End Sub

Private Sub Text6_Enter()
Set ctrlastfocus = Me.Text6
End Sub


It works ok for me.
 
M

Marshall Barton

Design said:
We’d like to store the reference (for lack of a better word) of a control in
a variable for future use. For example:

Me.Suffix1 is a text box on a form so things like Me.Suffix1.SetFocus will
set the focus to the text box. When we loose focus on this text box we’d
like to do some like:

ctrLastFocus = Me.Suffix1

where in a module we have

Public ctrLastFocus as Control

Then elsewhere we could write something like: ctrLastFocus.SetFocus
Of course this doesn’t work as Me.Suffix1 isn’t the control, but the value
of it. How do you do this?


When setting an object variable, you need to use a Set
statment, not an assignment statement.

Public ctrLastFocus as Control

Set ctrLastFocus = Me.Suffix1

Then your SetFocus will work.
 
J

J_Goddard via AccessMonster.com

Shouldn't

me(ctrLastFocus).setfocus

work, if ctrlastfocus is the name if the control?

John
 
M

Marshall Barton

Linq said:
Would not

ctrLastFocus = Me.Suffix1.Name

do the job?


If ctrLastFocus was previously set to the appropriate
control object, then that would set the control's Value to
the control's Name (i.e Suffix1), which seems kind of silly
to me.

I think was Sue was asking about was this kind of logic:

Dim ctrLastFocus As Control
. . .
Set ctrLastFocus = Me.Suffix1
. . .
ctrLastFocus.SetFocus

And I think(?) you are referring to something more like:

Dim strLastFocus As String
. . .
strLastFocus = "Suffix1" 'or Me.ActiveControl.Name
. . .
Me(strLastFocus).SetFocus

Both are equally viable in most cases, but I thing the first
approach is slightly more efficient.
 
B

Bob Quintal

Shouldn't

me(ctrLastFocus).setfocus

work, if ctrlastfocus is the name if the control?

John

I'd expect it would too but it doesn't, at least in the ways I've
tried.

Q
 
S

Stuart McCall

J_Goddard via AccessMonster.com said:
Shouldn't

me(ctrLastFocus).setfocus

work, if ctrlastfocus is the name if the control?

John

If ctrlastfocus is the _literal_ name of the control then it should be:

me("ctrLastFocus").setfocus
 

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