A family of Microsoft relational database management systems designed for ease of use.
This form searches for a check number in another form. If it exists, it opens that form.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
A family of Microsoft relational database management systems designed for ease of use.
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.
This form searches for a check number in another form. If it exists, it opens that form.
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.
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.
> DoCmd.FindRecord "CheckNr"
This looks fishy. Comment it out.
Your OpenForm line already opens the form to the given check number.
" 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?