Select First Row in a Sub Form Programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how to select the entire first row of a subform in visual
basic?

Thanks,

Sarah
 
Hi, Sarah.
Does anyone know how to select the entire first row of a subform in visual
basic?

For an example, place a button on the main form and paste the following code
into the main form's module:

Private Sub SelRecBtn_Click()

On Error GoTo ErrHandler

Me!subformCtrlName.SetFocus
Me.Controls("subformCtrlName").Form.Requery ' Sets focus to 1st
record.
RunCommand acCmdSelectRecord

Exit Sub

ErrHandler:

MsgBox "Error in SelRecBtn_Click( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub ' SelRecBtn_Click( )

.. . . where subformCtrlName is the name of the subform control, and
SelRecBtn is the name of the button. Save and compile the code, then open
the form in Form View. Navigate to any record except the first one on the
subform, then select the button on the main form. Focus will jump to the
first record and it will be selected. This will work even if the main form
is bound and has records displayed.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact info.
 
Thanks Gunny... that worked great!

'69 Camaro said:
Hi, Sarah.


For an example, place a button on the main form and paste the following code
into the main form's module:

Private Sub SelRecBtn_Click()

On Error GoTo ErrHandler

Me!subformCtrlName.SetFocus
Me.Controls("subformCtrlName").Form.Requery ' Sets focus to 1st
record.
RunCommand acCmdSelectRecord

Exit Sub

ErrHandler:

MsgBox "Error in SelRecBtn_Click( ) in" & vbCrLf & _
Me.Name & " form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Sub ' SelRecBtn_Click( )

. . . where subformCtrlName is the name of the subform control, and
SelRecBtn is the name of the button. Save and compile the code, then open
the form in Form View. Navigate to any record except the first one on the
subform, then select the button on the main form. Focus will jump to the
first record and it will be selected. This will work even if the main form
is bound and has records displayed.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact info.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top