Selecting a subform record in Access 2013

I have a command button on my main form that opens a second form using the On-Click event but I only want the form to open after the user selects a record. How do I do verify this in code?

Who is Participating?
Dale FyeCommented:
The Form_Current event fires whenever you select a record.  LIke I said, an image of the form in design and Form view would be helpful.
Dale FyeCommented:
Well, generally, a record will automatically be selected, it is normally the first record in the form or subform.  However, depending on where the button is, and where the record is that you want to select (it would be useful to see a screen shot).  One method would be to use the Click event, something like:

If me.txt_ID & "" = "" Then
    msgbox "Please select a record"
    docmd.openform "formname",,,"[ID] = " & me.txt_ID
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
If the "second form" is an actual Subform, then you can set the SourceObject after the user selects the record:

<code here to allow the user to select the record>
Me.SubFormControl.SourceObject = "YourForm"
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

SpaceCoastLifeAuthor Commented:
Sorry. I don't think I explained thoroughly enough what I'm trying to do. The subform record source includes an Id number field. What I need is when the user clicks on a record on the subform it captures that Id. The Record Selectors aren't shown in design view for either form so the On-Click event isn't available.
Gustav BrockCIOCommented:
Something like this in your button OnClick:

Dim varID As Variant

If Me!subNameOfTheSubformControl.Form.RecordsetClone.RecordCount = 0 Then
     ' No records in subform.
    varID = Me!subNameOfTheSubformControl.Form!ID.Value
    If IsNull(varID) Then
        ' Subform is on a new record
       ' varID holds the selected ID.
        ' Open your other form.
    End If
End If

SpaceCoastLifeAuthor Commented:
Perfect! One line of code and just what I was looking for.

Thanks for the help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.