ACC1x: Sample Function to Check Whether an Entry Exists

Last reviewed: June 8, 1997
Article ID: Q109374
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1

SUMMARY

This article demonstrates a sample function called CheckPrimaryKey() that you can use to check whether an entry in a text box already exists in the form's underlying table.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual.

MORE INFORMATION

In the following example, the CheckPrimaryKey() function checks whether the entry in the Customer ID field on the Customers form already exists in the Customers table. The function is attached to the BeforeUpdate event of the Customer ID text box on the form.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re- creating this code in Access Basic.

  1. Open the sample database NWIND.MDB. Create a new module.

  2. In the Declarations section, type the following:

          Option Explicit
    

  3. Enter the following code in the module:

          Function CheckPrimaryKey(MyCustID)
    

             Dim MyDB As Database, MyDyna As Dynaset
    
          ' Create a dynaset of Customer IDs from the Customers table. The
          ' function may encounter an empty dynaset. If so, the function will
          ' fail attempting to move to the first record of the dynaset, and
          ' execute the error handling routine labeled MyCustErrTrap.
    
             Set MyDB = CurrentDB()
             Set MyDyna = MyDB.CreateDynaset("SELECT [Customer ID] FROM _
                Customers WHERE [Customer ID] = '" & MyCustID & "';")
    
             On Error GoTo MyCustErrTrap:
             MyDyna.MoveFirst
    
          ' The Customer ID entered in the form is compared to the Customer IDs
          ' in the dynaset. If there is a match, display a message saying so,
          ' then cancel the event of updating the text box, and send the Escape
          ' (ESC) keystroke to undo the data entry. If there is no match, leave
          ' the Customer ID in the text box and select the next control on the
          ' form.
    
             If MyDyna![Customer ID] = MyCustID Then
                MsgBox "You've entered an existing Customer ID, please enter _
                   a new one"
                DoCmd CancelEvent
                SendKeys "{ESC}"
             End If
    
          ' This is the Error trap routine.
          MyCustErrTrap:
    
             Exit Function
    
          End Function
    
    

  4. Save the module as Customer ID Test Module.

  5. Open the Customers form in Design view, and select the Customer ID text box.

  6. Add the following expression to the BeforeUpdate event:

          =CheckPrimaryKey([Customer ID])
    

  7. Switch to Form view and choose Go To from the Records Menu, then choose New.

  8. Type "ALWAO" (without quotation marks) in the Customer ID text box. The message box will appear, and when you select OK your data entry will be undone.

  9. Type "ZZZZZ" (without quotation marks) in the Customer ID text box. The message box will not appear, and Microsoft Access will accept the data entry.

REFERENCES

Microsoft Access "Language Reference," version 1.0, pages 88, 311, and 432


Keywords : kbprg PgmOthr
Version : 1.0 1.1
Platform : WINDOWS
Hardware : X86
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.