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
{count} votes

8 answers

Sort by: Most helpful
  1. George Hepworth 21,805 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?

    0 comments No comments
  2. Tom van Stiphout 39,986 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.

    0 comments No comments
  3. ScottGem 68,755 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.

    0 comments No comments
  4. Anonymous
    2025-04-09T21:39:41+00:00

    Assuming the CheckNr column is of a number data type try this:

    Private Sub cmdChecknr_Click()
    
        Const MESSAGE_TEXT = “No matching record found.”
    
        DoCmd.OpenForm "CDfrmchecksort",WhereCondition:="CheckNr = " & CheckNr
    
       If  Not IsNull(Forms(“CDfrmchecksort”).CheckNr) Then
    
           DoCmd.Close acForm, Me.Name
    
       Else
    
            DoCmd.Close acForm, “CDfrmchecksort”
    
            MsgBox MESSAGE_TEXT, vbInformation, “Warning”
    
        End If
    
    Exit_cmdChecknr_Click:
        Exit Sub
    
    Err_cmdChecknr_Click:
        MsgBox Err.Description
        Resume Exit_cmdChecknr_Click
    
    End Sub
    

    The above assumes that the CDfrmchecksort form’s recordset is updatable, so that the form would go to an empty new record if no match is found.  If you want the CheckNr control to receive focus when the form opens just make it the first control in the form’s detail section’s tab order.

    0 comments No comments
  5. Anonymous
    2025-04-11T15:59:34+00:00

    This form searches for a check number in another form. If it exists, it opens that form.

    0 comments No comments