Display message box when search is not succesful.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crazyhouse
    New Member
    • Aug 2008
    • 31

    Display message box when search is not succesful.

    I am using the following code on a search page.

    myvalue = InputBox("Pleas e enter the Tracking Number.")

    stDocName = "Inspection_for m"
    stLinkCriteria = "[tracking_number]=" & "'" & [myvalue] & "'"

    DoCmd.OpenForm stDocName, , , stLinkCriteria, acReadOnly

    What i would like to know is how to open another form or msgbox if the [tracking_number] does not exist as a record.

    Thanks in advance.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Assuming you have a Field named [Tracking_Number] {STRING}, in a Table named tblOrders, here is a Template which you can use:
    Code:
    Dim strTrackingNum As String
    Dim blnTrackingNumExists As Boolean
    Dim strMsg As String
    
    strTrackingNum = InputBox$("Please enter the Tracking Number.")
    
    strMsg = "The Tracking Number [" & strTrackingNum & "] does not exist " & _
             "in tblOrders!"
    
    'blnTrackingNumExists returns True if the Tracking Number exists,
    'and False if it doesn't
    blnTrackingNumExists = DCount("*", "Employees", "[Tracking_Number] = '" & _
                                        strTrackingNum & "'")
    
    'User selected Cancel or OK with No Entry, bye - bye
    If Len(strTrackingNum) = 0 Then
      Exit Sub
    Else
      If blnTrackingNumExists Then      'Tracking Number does exist
        'Code if Tracking Number exists in tblOrders
      Else
        'Code if Tracking Number does not exist
        MsgBox strMsg, vbExclamation, "Tracking Number Not Found"
      End If
    End If

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      In Line #12 for Post #3, substitute tblOrders for Employees.

      Comment

      • crazyhouse
        New Member
        • Aug 2008
        • 31

        #4
        Thanks for the help, it worked great.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by crazyhouse
          Thanks for the help, it worked great.
          Glad it worked out for you.

          Comment

          • crazyhouse
            New Member
            • Aug 2008
            • 31

            #6
            I swear, i tried to change it but i couldn't figure it out. How do i make this work for a number field?

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by crazyhouse
              I swear, i tried to change it but i couldn't figure it out. How do i make this work for a number field?
              This would be the closest approach to what you already have:
              Code:
              Dim strTrackingNum As String
              Dim blnTrackingNumExists As Boolean
              Dim strMsg As String
                
              strTrackingNum = InputBox$("Please enter the Tracking Number.")
                
              strMsg = "The Tracking Number [" & strTrackingNum & "] does not exist " & _
                       "in tblOrders!"
                
              'blnTrackingNumExists returns True if the Tracking Number exists,
              'and False if it doesn't
              blnTrackingNumExists = DCount("*", "tblOrders", "[Tracking_Number] = " & _
                                     Val(strTrackingNum)) > 0
                
              'User selected Cancel or OK with No Entry or it is Not a Number
              If Len(strTrackingNum) = 0 Or Not IsNumeric(strTrackingNum) Then
                Exit Sub
              Else
                If blnTrackingNumExists Then      'Tracking Number does exist
                  'Code if Tracking Number exists in tblOrders
                  MsgBox "Yes it's alive!"
                Else
                  'Code if Tracking Number does not exist
                  MsgBox strMsg, vbExclamation, "Tracking Number Not Found"
                End If
              End If

              Comment

              • crazyhouse
                New Member
                • Aug 2008
                • 31

                #8
                Thanks again. You have really helped.

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  Originally posted by crazyhouse
                  Thanks again. You have really helped.
                  The pleasure is all mine.

                  Comment

                  Working...