ACC2000: Sample Function to Open a Form to a Particular Record

ID: Q210436


The information in this article applies to:
  • Microsoft Access 2000

Advanced: Requires expert coding, interoperability, and multiuser skills.


SUMMARY

This article shows you how to create a sample Visual Basic for Applications function that uses the InputBox() function to open a form to a particular record.


MORE INFORMATION

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

  1. Open the sample database Northwind.mdb.


  2. Create a module and type the following line in the Declarations section if it is not already there:



  3. 
    Option Explicit 

  4. Type the following procedure:



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

  6. Open the Customers form in Design view.


  7. Add a command button to the form, and set the OnClick property as follows:


  8. =OpenFormWithInput()
  9. 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.


Additional query words: inf

Keywords : kbprg kbdta AccCon FmsHowto KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.