Results 1 to 3 of 3
  1. #1
    Ecologist_Guy is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2017
    Posts
    5

    Use Message Box that informs user of error if there is no match when using OpenArgs to open a form

    I created a form that allows users to enter a record name (which also happens to be the primary key) in a text box and then click a button which opens a data entry form and automatically goes to the record that the user entered. I used the following code that takes advantage of the DoCmd.open form OpenArgs property:



    Private Sub btnFind_Nest_Click()
    Dim recordtolocate As String
    recordtolocate = [Forms]![findNest]![full_ID]
    DoCmd.OpenForm "frmHatch_Success", , , "Full_Nest_ID= '" & recordtolocate & "'"
    DoCmd.Close acForm, "FindNest", acSaveNo
    End Sub

    I am wondering if there is something I can do so a message box will appear if there is no record match (e.g. the user enters a typo). Currently, if there is no match the form will open to a new record.

    I am also open to using a different method to open the form if it facilitates incorporating an error message.

    Thanks for your help.

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You could use DLookUp to find out if that key exists on the record source of the form you are opening. If it is successful then open the form, otherwise give an error message.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    For the record, you aren't using OpenArgs, you're using the Wherecondition argument. You can test first:

    Code:
    If DCount("*", "TableName", "Full_Nest_ID= '" & recordtolocate & "'") > 0 Then
      DoCmd.OpenForm "frmHatch_Success", , , "Full_Nest_ID= '" & recordtolocate & "'"
    Else
      Msgbox "No such record"
    End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 232
    Last Post: 04-17-2017, 01:08 PM
  2. Replies: 12
    Last Post: 08-19-2016, 11:23 AM
  3. Replies: 3
    Last Post: 02-06-2015, 03:22 PM
  4. Replies: 1
    Last Post: 08-01-2011, 04:17 PM
  5. Open a second form using the OpenArgs
    By blueraincoat in forum Forms
    Replies: 6
    Last Post: 06-02-2011, 06:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums