How can i Focus a field?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have the following problem

In a form i have a button which call a public procedure (the code has 2
parameters)

1. Call Undo_Rec(Me.Form.Name, Me!City.Name) the second parameters is the
field that i want to sent the focus after the undo

2. Public Sub Undo_Rec(FormNam As String, FormFld As Varriant) this is the
procedure

My problem is that i don't know the syntax of the command to return the
focus in the field of the form.

If NoUndo = False Then Forms(FormNam!FormFld).SetFocus "this command is
wrong"
 
Kimon said:
I have the following problem

In a form i have a button which call a public procedure (the code has 2
parameters)

1. Call Undo_Rec(Me.Form.Name, Me!City.Name) the second parameters is the
field that i want to sent the focus after the undo

2. Public Sub Undo_Rec(FormNam As String, FormFld As Varriant) this is the
procedure

My problem is that i don't know the syntax of the command to return the
focus in the field of the form.

If NoUndo = False Then Forms(FormNam!FormFld).SetFocus "this command is
wrong"


You have the parenthesis in the wrong place and the use of
the FormFld argument is wrong. Try this:

If NoUndo = False Then Forms(FormNam)(FormFld).SetFocus

I also suspect that the call is inappropriate or, at least,
redundant. I think it should be:

Call Undo_Rec(Me.Name, Me!City.Name)

Not that using Me!City.Name is just the string "City", so
that would be the same as:

Call Undo_Rec(Me.Name, "City")
 
You can pass the whole form like so,

sub signature
Public Sub Undo_Rec(InOutForm As Form)

call sub like so
Undo_Rec Me

or like so
Call Undo_Rec(Me)

controls are referred to like so
InOutForm.City.SetFocus

this allows for anything to be examined without changing the procedure
signature and every call to it, especially when using code/class modules.

FYI - John
 
Thanks for your help it works.

I have also and something else to ask you, if you can help me.
How can i know in which field was the user (had the focus) before click the
button undo ?
If i have the name of this field i can sent this name or other name in the
second parameter of the procedure "undo_rec" and not only the "city" which is
the main field of the form.

Thanking you in advance
 
Kimon said:
Thanks for your help it works.

I have also and something else to ask you, if you can help me.
How can i know in which field was the user (had the focus) before click the
button undo ?
If i have the name of this field i can sent this name or other name in the
second parameter of the procedure "undo_rec" and not only the "city" which is
the main field of the form.


The form's ActiveControl property will tell you which of the
form's controls has the focus.

Your call would then look like:
Call Undo_Rec(Me.Name, Me.ActiveControl.Name)

Be sure to consider what John said. It's usually better to
pass the object you want to work on instead of the name of
the object. If you procedure declaration were:
Public Sub Undo_Rec(frm As Form, FormFld As As Control)

and called it with:
Undo_Rec Me, Me.ActiveControl

Then, the line of code you asked about would be:
If NoUndo = False Then FormFld.SetFocus

Actually, I haven't seen enough of your procedure or the
context of where you call it, but there may not be any need
for the frm argument. Even if you do want to reference the
form that contains the control, you can use FormFld.Parent
 
I tried the Me.ActiveControl.Name but this command returns the name of the
button that the user clicks for undo, i want the name of the field that the
user was before clicks the button undo.
 
Kimon said:
I tried the Me.ActiveControl.Name but this command returns the name of the
button that the user clicks for undo, i want the name of the field that the
user was before clicks the button undo.


In that case, you can try TC's suggestion, which should work
in most situations. If you have a timer event running or
some other esoteric scenario, the you may need to use each
control's Exit event to set a global variable that can be
used to simulate a form's previous control kind of property.
 
Thanks, it works

Ο χÏήστης "Marshall Barton" έγγÏαψε:
 
thanks, it works

Because i had the option "selected = true" in the field behaviour, after
your suggestion Application.Screen.PreviousControl.SetFocus
i wrote SendKeys "{F2}" and works fine

thanks again
 
Kimon said:
Because i had the option "selected = true" in the field behaviour, after
your suggestion Application.Screen.PreviousControl.SetFocus
i wrote SendKeys "{F2}" and works fine


Don't use SendKeys if there is any other way. In this case
try using:

With Application.Screen.PreviousControl
.SetFocus
.SelLength = 0
.SelStart = Len(.Text)
End With
 
Ok i changed it. Thanks

I want ask you also about your suggetion for the command "Sendkeys"
Is there any other command to use when i want to send "ESC", "DEL" or
others....
because when i want to empty a field i use the command sendkeys.
 
Kimon said:
Ok i changed it. Thanks

I want ask you also about your suggetion for the command "Sendkeys"
Is there any other command to use when i want to send "ESC", "DEL" or
others....
because when i want to empty a field i use the command sendkeys.


It depends on the context. This is one of the big problems
with SendKeys, if the wrong object is selected, you have no
idea what it will operate on. Most objects have methods or
properties that will do what you are trying to do with
SendKeys without being concerned about which object has the
focus.

If by "field", you mean a control on a form, then Esc undoes
any editing to the control's value and in VBA would be:
Me.controlname.Undo
Two consecutive Esc undoes all the changes to the form's
current record:
Me.Undo

Deleting the contents of a control is usually just clearing
its value:
Me.controlname = Null

Whenever you think you need to use SendKeys to accomplish
something, check Help for all the methods and properties of
the appropriate object to find an alternative. If you can't
figure it out, post a question here with the specific
situation you're having trouble with.
 

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

Back
Top