Close one form if record is found

Anonymous
2025-04-09T16:20:11+00:00

If a record is found, i want to close the search form. It isn't working currently.

This is the code I'm using

Private Sub cmdChecknr_Click()
DoCmd.OpenForm "CDfrmchecksort", , , "CheckNr =" & CheckNr & ""
DoCmd.GoToControl "CheckNr"
DoCmd.GoToControl "DemoClientID"
DoCmd.FindRecord "CheckNr"

Exit_cmdChecknr_Click:
Exit Sub

Err_cmdChecknr_Click:
MsgBox Err.Description
Resume Exit_cmdChecknr_Click
End Sub

Microsoft 365 and Office | Access | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Newest
  1. ScottGem 68,840 Reputation points Volunteer Moderator
    2025-04-09T21:33:07+00:00

    I'm also curious as to what your purpose here is. Why would you search for a record then close the form when its found? A little more background might help us help you more.

    Was this answer helpful?

    0 comments No comments
  2. Tom van Stiphout 40,216 Reputation points MVP Volunteer Moderator
    2025-04-09T17:49:34+00:00

    > DoCmd.FindRecord "CheckNr"

    This looks fishy. Comment it out.

    Your OpenForm line already opens the form to the given check number.

    Was this answer helpful?

    0 comments No comments
  3. George Hepworth 23,120 Reputation points Volunteer Moderator
    2025-04-09T17:44:45+00:00

    " It isn't working currently"

    First, there's nothing in that code to close a form, only open one called "CDfrmchecksort".

    Second, what DOES happen? What does it mean to say "it isn't working correctly"?

    I would expect nothing to happen inasmuch as there is no code to close any form in that Sub. That would be something like

      DoCmd.Close acForm, frm.Name, acSaveNo
    
    where frm.Name is the name of the form, which is probably "CDfrmchecksort" in this case.
    

    To close the form after successfully finding a specific record, you need to add two things.

    A conditional to determine if the required record was found or not.

    A line of code to close the search form if the find was successful.

    In addition, you need to decide what else needs to happen when you don't find the record. Search again? Close the search anyway?

    What do you need to do when a record is found, in addition to just closing the search form?

    Was this answer helpful?

    0 comments No comments