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. Anonymous
    2025-04-11T16:51:29+00:00

    The simplest solution would be to use a combo box, whose RowSource is a query which returns all cheque numbers from the relevant table, as the control in the search form.  That way you can guarantee that the cheque exists.  The code for the combo box’s AfterUpdate event procedure would be like this:

        Dim strCriteria As String
    
        strCriteria = “CheckNr =" &  Me.ActiveControl
    
        If Not IsNull(Me.ActiveControl) Then
    
            DoCmd.OpenForm "CDfrmchecksort", WhereCondition:=strCriteria
    
            DoCmd.Close acForm, Me.Name
        End If
    

    The above assumes that the CheckNr column is of a number data type.  If it’s of text data type amend the code as follows:

        strCriteria = "CheckNr = """ &  Me.ActiveControl & """"
    
    0 comments No comments
  2. Anonymous
    2025-04-11T22:19:36+00:00

    I had to make a couple changes but this worked

    0 comments No comments
  3. ScottGem 68,755 Reputation points Volunteer Moderator
    2025-04-12T01:43:47+00:00

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

    Forms DISPLAY data they don't hold data. So, if you want to search for the existence of data you search through a table, not a form. If the form uses a filtered Recordsource, then you search through that Recordsource.

    I see Ken suggested some better coding that you got to work. But you do need to understand how it works.

    0 comments No comments