Results 1 to 6 of 6
  1. #1
    NanR is offline Novice
    Windows 8 Access 2013
    Join Date
    May 2015
    Posts
    3

    VB code to Search for a Specific Record on Subform

    Hello,
    I've been searching for steps/code to be able to search for an account number on a subform (frmOverdraftAccts). The main form is frmCustomer-MainForm.
    This is what I'm using so far:
    Private Sub cmdSearch_Click()
    If IsNull(txtSearch) = False Then
    Me.Recordset.FindFirst "[AcctNum]=" & txtSearch
    Me!txtSearch = Null
    If Me.Recordset.NoMatch Then
    MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"


    Me!txtSearch = Null
    End If
    End If
    End Sub
    The code is in an Event Procedure OnClick of the command button cmdSearch. It uses a txtSearch text box for the user to enter the account number that they are looking for.
    The only thing that is returned is the message. It never finds the account number.
    I have put the text box and command button in the form header of the subform.
    I'm very new at using code. And, I'm not sure if I've explained what is happening very well.
    Any help is greatly appreciated.
    NanR

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Try:

    Code:
    If IsNull(Me.txtSearch) = False Then
        Me.RecordsetClone.FindFirst "[AcctNum]=" & Me.txtSearch
        If Me.RecordsetClone.NoMatch Then
            MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
        Else
            Me.Bookmark = Me.RecordsetClone.Bookmark
        End If
        Me.txtSearch = Null
     End If
    Is AcctNum a text type field? If so:
    Me.Recordset.FindFirst "[AcctNum]='" & Me.txtSearch & "'"

    The subform should be linked to the main form and therefore cannot display any record that is not associated with the main form record.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    NanR is offline Novice
    Windows 8 Access 2013
    Join Date
    May 2015
    Posts
    3
    Hi June7,
    I used the code you provided above and changed the Recordset for text "".
    However, I'm getting a Run Time error on: Me.Bookmark = Me.RecordsetClone.Bookmark

    The subform is linked to the main form on CustID.
    I'm at a loss as to what is the problem.
    Thank you for your help.
    NanR

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What is the exact error message?

    The code technique works for me. The code is behind the subform?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    NanR is offline Novice
    Windows 8 Access 2013
    Join Date
    May 2015
    Posts
    3
    The error message box says: Run-time error '3021': No current record.
    Yes, the code is on a command button on the header of the subform.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I don't know why the code errors. It looks fine to me. Step debug. Refer to link at bottom of my post for debugging guidelines.

    Otherwise, if you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-06-2015, 12:38 AM
  2. Replies: 4
    Last Post: 12-13-2013, 04:50 PM
  3. Search for a specific Record
    By RayMilhon in forum Forms
    Replies: 1
    Last Post: 12-04-2013, 02:28 PM
  4. Replies: 6
    Last Post: 11-13-2012, 04:29 PM
  5. Code to pull in data from a specific record
    By jdunn36 in forum Access
    Replies: 1
    Last Post: 09-20-2010, 11:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums