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-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.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2025-04-11T22:19:36+00:00

    I had to make a couple changes but this worked

    Was this answer helpful?

    0 comments No comments
  3. 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 & """"
    

    Was this answer helpful?

    0 comments No comments
  4. 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.

    Was this answer helpful?

    0 comments No comments
  5. 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.

    Was this answer helpful?

    0 comments No comments