The OpenFormWithInput() function opens the form to the record where the CustomerID matches the value that you enter in the input box.
If you change "=" to ">=" on the
OpenForm line in the function, the form opens to the record that you want and displays subsequent records as well. For example, if you type
CACTU as the CustomerID in the input box, CustomerID "AROUT" is not part of the form's record set, but "THEBI" is.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
- Open the sample database Northwind.mdb.
- Create a module and type the following line in the Declarations section if it is not already there:
- Type the following procedure:
Function OpenFormWithInput()
Dim Msg As String
Dim Title As String
Dim Defvalue As String
Dim Answer As String
Msg = "Enter a Customer ID (AROUT, CACTU, THEBI, etc.)."
Title = "OPEN CUSTOMERS FORM"
Defvalue = "CACTU"
Answer = InputBox(Msg, Title, Defvalue)
If Answer <> "" Then
DoCmd.OpenForm "Customers", , , "[CustomerID]='" & Answer & "' "
Else
DoCmd.OpenForm "Customers"
End If
End Function
- Open the Customers form in Design view.
- Add a command button to the form, and set the OnClick property as follows:
=OpenFormWithInput()
- Open the form in Form view and click the command button. Accept the default value in the input box (or type any valid CustomerID) and click OK. Note that the form opens to the designated customer.
NOTE: If you type an invalid CustomerID in the input box, Microsoft Access displays a blank form.