vbams-access-2016ms-access-forms

Access 2016: Selecting a record in a datasheet when form is loaded


My problem relates to selecting a record in a datasheet-subform in Access 2016.

(Note: I have fixed this problem using a form timer which runs once after opening, but I don't think this is an ideal solution. Surely there's a better way?)

I have 2 underlying tables, with a one-to-many relationship.

I have a Main form (opened on startup), with a datasheet subform. The main form has an un-bound combo box to select a record and the datasheet subform is populated based on the selected item. I have just about everything working as it should.

My problem is upon opening the database - I want the datasheet subform to go to a particular record (got this working using Form.RecordsetClone then a FindFirst and then a Bookmark), and I want that record to be SELECTED (ie, a red border around the entire record) - this is the part I can't get working without the use of a timer.

When using the Timer method, and I watch very closely, I think it selects the record (with red border), de-selects it (no red border), then selects it again (with red border). This all happens very quickly in about 1 second.

To use the timer method, I set the 'Timer Interval' property to 1, and use the following code:


Private Sub Form_Load()
    'Initialise combo box (go to its first item)
    Me.cboSeries.SetFocus
    Me.cboSeries = Me.cboSeries.ItemData(0)

    Dim rs As Recordset
    Dim rs2 As Recordset
    'The following code sets the 'Main' form record to the chosen combo box item, and re-queries the datasheet subform 'frmEps'
    Set rs = Form_Main.RecordsetClone
    rs.FindFirst "[SeriesID] = " & str(Nz(Form_Main.cboSeries, 0))
    If Not rs.EOF Then Form_Main.Bookmark = rs.Bookmark
    Form_Main.frmEps.Form.Requery

    'The following code finds the first Watched=false record and bookmarks it
    Set rs2 = Form_Main.frmEps.Form.RecordsetClone
    rs2.FindFirst "[Watched] = false"
    If Not rs2.EOF Then Form_Main.frmEps.Form.Bookmark = rs2.Bookmark

    'This code selects the record. Omit from here when using Timer method.
    'Form_Main.frmEps.SetFocus
    'Form_Main.frmEps.Form.SeasonNumber.SetFocus 'First control
    'RunCommand acCmdSelectRecord

    rs.Close
    rs2.Close
    Set rs = Nothing
    Set rs2 = Nothing
    'No more code in Sub
End Sub


Private Sub Form_Timer()
    Me.TimerInterval = 0 'Turn off timer
    Form_Main.frmEps.SetFocus
    Form_Main.frmEps.Form.SeasonNumber.SetFocus 'SeasonNumber is the first control in the datasheet
    RunCommand acCmdSelectRecord
End Sub

The following screenshot is how it ends up when using the Timer method (how it should look):

Record is selected with red border

When trying the solution WITHOUT a Timer, I set the 'Timer Interval' property to 0 and un-comment the 3 lines in Form_Load. When this is done, and the database is opened, the correct record is selected (with red border) momentarily for about half a second, then while remaining on the correct record, it is de-selected and focus moved to the first control in the record. It all happens very quickly. This screenshot shows what it looks like at this stage (no red border):

Control has focus. Entire record is not selected

The issue seems to be that focus is being moved around by some other process, causing the de-select to happen, but I can't figure out where. I have tried these fixes:

- Tried adding Exit Sub to the start of every other event handler in the database, so no other code is running - this did not fix the problem.

- Tried putting the above code into the Form_Current Sub, as I understand this is the last event that fires when opening the form (am I wrong?). Same behavior.

- Removed all 'Conditional Formatting' rules that I had in place. No fix.

- Played around with various form properties. No fix.

What am I missing? Please help.


Solution

  • Problem is that the subform has its own life and, strangely, needs to lose focus before selecting the record. At least, that works for me.

    So, in the main form, have these three lines in your OnLoad event:

        ' This code selects the record. 
        Form_Main.frmEps.SetFocus
        Me.SetFocus
        RunCommand acCmdSelectRecord