DoCmd.GoToControl

  • Thread starter Thread starter NES
  • Start date Start date
N

NES

I have a command button in the header area of a form. The command is simply
to go to a new record. And it works. However, I want the result of this
action to change the focus to the first control in the body of the form. I'm
assuming I can do it by following the command VBA code with a
DoCmd.GoToControl. But I don't know where to find the proper format. The
control name in the body of the form is "Patient Name".

I'm hoping someone will help me with that short piece of code. Thanks in
advance for any light you may shed on this.
 
Forms!FormName!ControlName.SetFocus

Or if you want you can use the current event of your form and use
Me.ControlName.SetFocus

Or if you only want to do that when the button on the form is clicked then
use the above after (or before) your code that "goes" to a new record.

--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
NES said:
I have a command button in the header area of a form. The command is simply
to go to a new record. And it works. However, I want the result of this
action to change the focus to the first control in the body of the form.
I'm
assuming I can do it by following the command VBA code with a
DoCmd.GoToControl. But I don't know where to find the proper format. The
control name in the body of the form is "Patient Name".

I'm hoping someone will help me with that short piece of code. Thanks in
advance for any light you may shed on this.

Me("Patient Name").SetFocus

or:

Me!Patient Name.SetFocus

or even:

Me.Patient_Name.SetFocus
 
Thanks John. This is the code, per your suggestion. It's bombing. I've been
a QuickBASIC programmer for years, but I'm not familir with VBA at all. I'm
trying to learn bit by bit with small changes in existing code. Thus we
have....

Private Sub New_Prescription_Click()
On Error GoTo Err_New_Prescription_Click

DoCmd.GoToRecord , , acNewRec
Forms!Prescriptions_Form!Patient_Name.SetFocus

Exit_New_Prescription_Click:
Exit Sub

Err_New_Prescription_Click:
MsgBox Err.Description
Resume Exit_New_Prescription_Click

End Sub

The above code is failing. I am still not familiar with what format is
required.
--
Norm Shimmel
Butler, PA


John Spencer said:
Forms!FormName!ControlName.SetFocus

Or if you want you can use the current event of your form and use
Me.ControlName.SetFocus

Or if you only want to do that when the button on the form is clicked then
use the above after (or before) your code that "goes" to a new record.

--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
Me!Patient Name.SetFocus

Oops! Slight mistake there. Should be:

Me![Patient Name].SetFocus
 
John, thanks for putting me onto the proper code. It was the final formatting
that threw me. I've got it now.
--
Norm Shimmel
Butler, PA


John Spencer said:
Forms!FormName!ControlName.SetFocus

Or if you want you can use the current event of your form and use
Me.ControlName.SetFocus

Or if you only want to do that when the button on the form is clicked then
use the above after (or before) your code that "goes" to a new record.

--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
Me.Patient_Name.SetFocus

The above is a copy of the code that works just fine. No Problems. Thanks
again.
--
Norm Shimmel
Butler, PA


Stuart McCall said:
Me!Patient Name.SetFocus

Oops! Slight mistake there. Should be:

Me![Patient Name].SetFocus
 
Back
Top