Set the Focus

Copper 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