dcsimg
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2003
    Posts
    48

    Question VBA Code setfocus

    I have a button on my form that adds a new record but I want it to focus a specific field on the form becasue the focus stays on the button. How do I do this.

    ROb

  2. #2
    Join Date
    Apr 2003
    Posts
    48
    nm

  3. #3
    Join Date
    Jan 2003
    Location
    UK
    Posts
    277
    MyControl.SetFocus

  4. #4
    Join Date
    Mar 2003
    Posts
    37

    Exclamation set focus

    Access has a bug in it that causes the set focus command to misbehave on occasion.

    A workaround is for you to set the focus on the control immediately prior to (i.e., immediately prior in the tab order) the one you really want focus on and then set the focus on the control you really want it on.

    Thus, in the snippet below, I want the focus on the form to go to OrderNumber, but I set it on Refinance first and then re-set it to OrderNumber.

    Clunky, yes, but it works.


    Code:
    '...this is just a snippet...
                Me!Refinance.SetFocus
                Me!OrderNumber.SetFocus
    'An Access bug makes you set the focus to the prior control on a form
    'before you can re-set the focus to the control you want it on
    '...end of snippet...

  5. #5
    Join Date
    Apr 2003
    Location
    Puerto Rico
    Posts
    4

    Red face

    On your button add the following code:

    MyField.SetFocus


    The button will now add a new record and set focus to your field.

  6. #6
    Join Date
    Mar 2003
    Posts
    37

    Lightbulb

    I came up with this solution:

    Code:
    '--------------------------------------------
    'This procedure forces the user to enter an
    'order number as well as allowing the user to 
    'choose the option of going to the last order 
    'entered
    '--------------------------------------------
    
    Private Sub OrderNumber_LostFocus()
        If IsNull(Me!OrderNumber) Then
            If (MsgBox("Enter an order number in the format 03-1234." _
            & vbCrLf & "Click CANCEL to return to the last order entered.", _
            vbOKCancel, "You must enter an order number first!") _
            = vbOK) Then
    
    '--------------------------------------------
    'An Access bug makes you set the focus to the 
    'prior or next control on a form before you 
    'can re-set the focus to the control you want 
    'it on
    '--------------------------------------------
    
                Me!Refinance.SetFocus
                Me!OrderNumber.SetFocus
            Else
                DoCmd.GoToRecord , , acLast
            End If
        End If
    End Sub

Posting Permissions

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