Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2009
    Posts
    4

    Unanswered: Set focus on new record

    I have set my forms' focus on their records' ID field when the forms are opened. For example: when I open the Meetings' form, the cursor is in the MtngID field. I added this line to the form's OnCurrent property: MtngID.SetFocus

    I want to modify the forms so that when a new record is added, the focus will change to the first data entry field on the form.

    This is the current code for the Meetings form's OnCurrent property:

    Private Sub Form_Current()
    'Provide a record counter to use with
    'custom navigation buttons (when not using
    'Access' built-in navigation).
    Dim rst As DAO.Recordset
    Dim IngCount As Long

    Set rst = Me.RecordsetClone

    With rst
    .MoveFirst
    .MoveLast
    IngCount = .RecordCount
    End With

    'Show the result of the record count in the text box (txtRecordNo)

    Me.txtRecordNo = Me.CurrentRecord & " of " & IngCount

    MtngID.SetFocus

    End Sub

    This is the current code for the AddRecord command button on the Meetings form:

    Private Sub cmdAdd_Click()
    On Error GoTo Err_cmdAdd_Click

    DoCmd.GoToRecord , , acNewRec

    Exit_cmdAdd_Click:
    Exit Sub

    Err_cmdAdd_Click:
    MsgBox err.Description
    Resume Exit_cmdAdd_Click

    End Sub

    Is it possible to have two different SetFocus commands working in the same form?

    I want MtngID to be the focus when the form is opened, but I want the focus to change to Mtng (the form's first data entry field) when I click the AddRecord command button.

    Please advise.

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    There shouldn't be any problem adding a SetFocus in the other event.
    Paul

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    In the Form_Current event check whether you're dealing with a new record or not and set the focus accordingly:
    Code:
    Private Sub Form_Current()
    'Provide a record counter to use with
    'custom navigation buttons (when not using
    'Access' built-in navigation).
    Dim rst As DAO.Recordset
    Dim IngCount As Long
    
    Set rst = Me.RecordsetClone
    
    With rst
    .MoveFirst
    .MoveLast
    IngCount = .RecordCount
    End With
    
    'Show the result of the record count in the text box (txtRecordNo)
    
    Me.txtRecordNo = Me.CurrentRecord & " of " & IngCount
    
    If Me.NewRecord Then
      Mtng.SetFocus
    Else
      MtngID.SetFocus
    End If
    
    End Sub
    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •