Forum Discussion

faxylady's avatar
faxylady
Copper Contributor
Jul 07, 2021

Set the Focus

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

  • faxylady's avatar
    faxylady
    Copper Contributor
    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
  • Ifaxylady 

    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?

     

     

     

    • faxylady's avatar
      faxylady
      Copper Contributor
      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.
    • faxylady's avatar
      faxylady
      Copper Contributor
      DateofPurchase is the first control of the form. I want the cursor to appear here when a new record is started. Thank you.
      • michaelmerlin's avatar
        michaelmerlin
        Copper Contributor
        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

    • faxylady's avatar
      faxylady
      Copper Contributor

      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

       

       

Resources