# Database Discussions > Microsoft Access >  VBA Code setfocus

## superrob5

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

----------


## superrob5

nm

----------


## KnooKie

MyControl.SetFocus

----------


## judgehopkins

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.




```
'...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...
```

----------


## pachecf

On your button add the following code:

MyField.SetFocus


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

----------


## judgehopkins

I came up with this solution:



```
'--------------------------------------------
'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
```

----------

