Set focus to a particular column on empty query grid

56 Views Asked by At

The ACCESS form opens a simple query in acReadOnly mode. Using a button on the form, I would like to set the focus to a specific column in the query grid. The code works correctly if the query returns at least one record.
If the query returns 0 records, the dataSheet.Controls("Field2").SetFocus statement returns error 2105 - "You can't go to the specified record".
Is there a way to set the Focus to a particular column in the empty query grid?

'main module
Option Compare Database
Option Explicit

Sub start()
    DoCmd.OpenForm "frmtest"
End Sub
'frmTest module
Option Compare Database
Option Explicit

Dim dataSheet As Object

Private Sub Form_Load()
    DoCmd.OpenQuery "qryTest1", acViewNormal, acReadOnly
    'qryTest1: select field1,field2,field3 from myTable
    
    Set dataSheet = Screen.ActiveDatasheet
End Sub

Private Sub cmdSetFocus_Click()
    dataSheet.SetFocus
    MsgBox dataSheet.SelTop
    dataSheet.Controls("Field2").SetFocus
End Sub   
0

There are 0 best solutions below