Set the Focus

%3CLINGO-SUB%20id%3D%22lingo-sub-2521642%22%20slang%3D%22en-US%22%3ESet%20the%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2521642%22%20slang%3D%22en-US%22%3E%3CP%3E06%2F08%2F21%3C%2FP%3E%3CP%3EMany%20years%20ago%2C%20I%20acquired%20some%20code%20to%20allow%20me%20to%20have%20the%20cursor%20start%20on%20my%20first%20line%20of%20my%20forms.%20Even%20though%20I%20have%20changed%20the%20lines%20to%20correspond%20with%20each%20form%2C%20for%20some%20reason%2C%20I%20can%20no%20longer%20get%20it%20to%20work.%3C%2FP%3E%3CP%3EThe%20code%20is%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%E2%80%9CPrivate%20Sub%20Form_Current()%3C%2FP%3E%3CP%3EIf%20DateofPurchase.NewRecord%20%3D%20True%20Then%3C%2FP%3E%3CP%3EDateofPurchase.DateofPurchase.SetFocus%3C%2FP%3E%3CP%3EEnd%20If%3C%2FP%3E%3CP%3EEnd%20Sub%E2%80%9D%3C%2FP%3E%3CP%3EPreviously%2C%20this%20has%20worked%20fine.%20But%20I%20can%20no%20longer%20get%20it%20to%20work.%26nbsp%3B%20I%20also%20clicked%20Debug%2C%20Compile.%20Still%20it%20doesn%E2%80%99t%20work.%3C%2FP%3E%3CP%3EWhat%20I%20want%20is%20for%20the%20cursor%20to%20appear%20on%20the%20first%20line%20of%20the%20form%20instead%20of%20where%20it%20was%20left%20on%20the%20previous%20entry%20entered%20by%20end%20user.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2521642%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2525158%22%20slang%3D%22en-US%22%3ERe%3A%20Set%20the%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2525158%22%20slang%3D%22en-US%22%3E%3CP%3EI%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1097303%22%20target%3D%22_blank%22%3E%40faxylady%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20see%20that%20nobody%20has%20responded%2C%20possibly%20because%2C%20like%20me%2C%20other%20forum%20members%20aren't%20quite%20clear%20what%20you%20are%20asking.%3C%2FP%3E%3CP%3E1.%20Please%20clarify%20what%20type%20of%20form%20you%20are%20using%3A%20single%2Fcontinuous%2Fdatasheet%2Fsplit%3C%2FP%3E%3CP%3E2.%20When%20you%20refer%20to%20the%20first%20line%20of%20your%20forms%2C%20do%20you%20mean%20the%20first%20record%20or%20the%20first%20control%3F%3C%2FP%3E%3CP%3E3.%20Why%20do%20you%20need%20to%20set%20the%20focus%20to%20DateOfPurchase%20using%20code%3F%3C%2FP%3E%3CP%3EHave%20you%20tried%20altering%20the%20tab%20order%20so%20that%20control%20is%20the%20first%20item%20in%20the%20tab%20order%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2525182%22%20slang%3D%22en-US%22%3ERe%3A%20Set%20the%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2525182%22%20slang%3D%22en-US%22%3E%3CP%3EForgot%20to%20say...%3C%2FP%3E%3CP%3EThe%20NewRecord%20property%20is%20for%20the%20form...not%20a%20specific%20control.%3C%2FP%3E%3CP%3ESomething%20like%20this%20should%20work%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20NewRecordMark(frm%20As%20Form)%20%0A%20%20%20If%20frm.NewRecord%20%3D%20True%20Then%20%20MsgBox%20%22You're%20in%20a%20new%20record.%22%20%0AEnd%20Sub%20%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2529984%22%20slang%3D%22en-US%22%3ERe%3A%20Set%20the%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2529984%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1007894%22%20target%3D%22_blank%22%3E%40isladogs%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20responding%20so%20quickly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESingle%20form.%3C%2FP%3E%3CP%3EWhat%20I%20am%20trying%20to%20do%20is%20get%20the%20cursor%20to%20appear%20on%20the%20first%20control%20of%20the%20record%20when%20the%20end%20user%20is%20finished%20entering%20data%20on%20the%20previous%20record.%26nbsp%3B%20What%20the%20cursor%20is%20doing%20at%20present%20is%20remaining%20on%20the%20last%20control%20entered%20by%20the%20end%20user%20when%20a%20new%20record%20is%20begun%2C%20although%20on%20the%20new%20record.%3C%2FP%3E%3CP%3EI%20was%20given%20this%20code%20many%20years%20ago%20and%20it%20always%20worked.%20But%20now%20it%20is%20not.%20I%20am%20trying%20to%20figure%20out%20what%20is%20wrong.%26nbsp%3B%20I%20do%20not%20write%20code%20yet.%20Will%20be%20learning%20soon.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Form_Current()%3C%2FP%3E%3CP%3EIf%20Me.NewRecord%20%3D%20True%20Then%3C%2FP%3E%3CP%3EMe.DateofPurchase.SetFocus%3C%2FP%3E%3CP%3EEnd%20If%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2529986%22%20slang%3D%22en-US%22%3ERe%3A%20Set%20the%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2529986%22%20slang%3D%22en-US%22%3EDateofPurchase%20is%20the%20first%20control%20of%20the%20form.%20I%20want%20the%20cursor%20to%20appear%20here%20when%20a%20new%20record%20is%20started.%20Thank%20you.%3C%2FLINGO-BODY%3E
Occasional Contributor

06/08/21

Many years ago, I acquired some code to allow me to have the cursor start on my first line of my forms. Even though I have changed the lines to correspond with each form, for some reason, I can no longer get it to work.

The code is,

 “Private Sub Form_Current()

If DateofPurchase.NewRecord = True Then

DateofPurchase.DateofPurchase.SetFocus

End If

End Sub”

Previously, this has worked fine. But I can no longer get it to work.  I also clicked Debug, Compile. Still it doesn’t work.

What I want is for the cursor to appear on the first line of the form instead of where it was left on the previous entry entered by end user.

14 Replies

I@faxylady 

I see that nobody has responded, possibly because, like me, other forum members aren't quite clear what you are asking.

1. Please clarify what type of form you are using: single/continuous/datasheet/split

2. When you refer to the first line of your forms, do you mean the first record or the first control?

3. Why do you need to set the focus to DateOfPurchase using code?

Have you tried altering the tab order so that control is the first item in the tab order?

 

 

 

Forgot to say...

The NewRecord property is for the form...not a specific control.

Something like this should work

 

Sub NewRecordMark(frm As Form) 
   If frm.NewRecord = True Then  MsgBox "You're in a new record." 
End Sub 

@isladogs 

 

Thank you for responding so quickly.

 

Single form.

What I am trying to do is get the cursor to appear on the first control of the record when the end user is finished entering data on the previous record.  What the cursor is doing at present is remaining on the last control entered by the end user when a new record is begun, although on the new record.

I was given this code many years ago and it always worked. But now it is not. I am trying to figure out what is wrong.  I do not write code yet. Will be learning soon.

 

Private Sub Form_Current()

If Me.NewRecord = True Then

Me.DateofPurchase.SetFocus

End If

End Sub

 

 

DateofPurchase is the first control of the form. I want the cursor to appear here when a new record is started. Thank you.
Your code did not work when I hit the Next Record Button or the Add New Record Button. The cursor should appear on the very first line, "DateofPurchase."
Instead of writing code, another option might be just setting properties of various controls, most importantly the tab order which is
Tab Index

also combined with these 2 additional properties:
Tab Stop
Enabled

Thank you for responding.

I don't believe this would work as the form is created from the table.

Remember what I am trying g to do is have the cursor blink on the first control for each new record.
When the last info is entered and enter is pressed the cursor should go to the first line of the next record. Can you do remote session?
It isn't obvious who you are asking. Suggest you upload a cut down version of your database for someone to look at
Thanks for your response. Maybe we can get together next week to help solve a few other problems.

I have copied and pasted this code into what I believe is a module. When I click compile, the statement appears in red with the error message, "Expected: End of Statement" The If is highlighted in blue.
What code?
How do I upload? I do not see an option to upload after the Reply hyperlink. Thanks.
I screwed up here. This is not the correct code that I previously received.

Private Sub Form_Current()

If Me.NewRecord = True Then

Me.NewRecord.SetFocus

End If

End Sub

I was supposed to substitute NewRecord with my own Text Box Caption
The code in your first response to me.

Sub NewRecordMark(frm As Form)
If frm.NewRecord = True Then MsgBox "You're in a new record."
End Sub