Message box Option Buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ezzz
    New Member
    • Jan 2010
    • 28

    Message box Option Buttons

    I have a search function in my database, see below:-

    Code:
    Private Sub cmdSearch_Click()
        Dim SerialNumberRef As String
        Dim Search As String
        
    'Check txtSearch for Null value or Nill Entry first.
    
        If IsNull(Me![txtsearch]) Or (Me![txtsearch]) = "" Then
            MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
            Me![txtsearch].SetFocus
        Exit Sub
    End If
    '---------------------------------------------------------------
            
    'Performs the search using value entered into txtSearch
    'and evaluates this against values in SerialNumber
            
    
        DoCmd.GoToControl ("SerialNumber")
        DoCmd.FindRecord Me!txtsearch
          
        SerialNumber.SetFocus
        SerialNumberRef = SerialNumber.Text
        txtsearch.SetFocus
        Search = txtsearch.Text
            
    'If matching record found sets focus in SerialNumber and shows msgbox
    'and clears search control
    
        If SerialNumberRef = Search Then
            MsgBox "Match Found For: " & Search, , "Congratulations!"
            SerialNumber.SetFocus
            txtsearch = ""
            
        'If value not found sets focus back to txtSearch and shows msgbox
            Else
               MsgBox "Match Not Found For: " & Search & " - Please Try Again.", _
                , "Invalid Search Criterion!"
                txtsearch.SetFocus
        End If
    End Sub
    When the search finds the serial number it displays a message box with "Ok"
    Because I have multiple entries with the same serial the search always only finds the first record.
    Is there a way to adapt the above code so it displays a message box with 2 option buttons, one that says "Ok" and one that says "Next" whereby I can then use the Find Next function to search for the remaining serial numbers?

    Ezzz
    Last edited by Jim Doherty; May 10 '10, 11:09 AM. Reason: Code tags
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Instead of using a Messagebox, you'll have to "roll your own," using a Popup/Modal form instead, with your own buttons. This is the standard workaround when you need a 'messagebox' that does things or looks differently from what an actual Messagebox can do or look.

    Linq ;0)>

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by Ezzz
      I have a search function in my database, see below:-

      Code:
      Private Sub cmdSearch_Click()
          Dim SerialNumberRef As String
          Dim Search As String
          
      'Check txtSearch for Null value or Nill Entry first.
      
          If IsNull(Me![txtsearch]) Or (Me![txtsearch]) = "" Then
              MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
              Me![txtsearch].SetFocus
          Exit Sub
      End If
      '---------------------------------------------------------------
              
      'Performs the search using value entered into txtSearch
      'and evaluates this against values in SerialNumber
              
      
          DoCmd.GoToControl ("SerialNumber")
          DoCmd.FindRecord Me!txtsearch
            
          SerialNumber.SetFocus
          SerialNumberRef = SerialNumber.Text
          txtsearch.SetFocus
          Search = txtsearch.Text
              
      'If matching record found sets focus in SerialNumber and shows msgbox
      'and clears search control
      
          If SerialNumberRef = Search Then
              MsgBox "Match Found For: " & Search, , "Congratulations!"
              SerialNumber.SetFocus
              txtsearch = ""
              
          'If value not found sets focus back to txtSearch and shows msgbox
              Else
                 MsgBox "Match Not Found For: " & Search & " - Please Try Again.", _
                  , "Invalid Search Criterion!"
                  txtsearch.SetFocus
          End If
      End Sub
      When the search finds the serial number it displays a message box with "Ok"
      Because I have multiple entries with the same serial the search always only finds the first record.
      Is there a way to adapt the above code so it displays a message box with 2 option buttons, one that says "Ok" and one that says "Next" whereby I can then use the Find Next function to search for the remaining serial numbers?

      Ezzz
      Is there a way to adapt the above code so it displays a message box with 2 option buttons, one that says "Ok" and one that says "Next" whereby I can then use the Find Next function to search for the remaining serial numbers?
      As linq explained, you would have to Roll Your Own, unless you wished to use a Message Box with Retry and Cancel Options, as in:
      Code:
      Dim intResponse As Integer
      
      intResponse = MsgBox("Okie Dooky", vbRetryCancel, "YaDa - Yada")
      
      If intResponse = vbRetry Then
        'Do your FindNext Operation
      Else    'Cancel or Close Button
        'Satisfied by initial Find
      End If

      Comment

      • Ezzz
        New Member
        • Jan 2010
        • 28

        #4
        Message box options buttons

        ADezii
        I did like your solution and tried to re-code my search formula but got bogged down with too many "If" statements. See below:-
        Code:
        'If matching record found sets focus in SerialNumber and shows msgbox
        'and clears search control
        
            If SerialNumberRef = Search Then
                MsgBox "Match Found For: " & Search, , vbRetryCancel, "Congratulations!"
                If Search = vbRetry Then
                DoCmd.FindNext
                'Do your FindNext Operation
                Else
                DoCmd.Close
                'Satisfied by initial Find
                SerialNumber.SetFocus
                txtsearch = ""
                
            'If value not found sets focus back to txtSearch and shows msgbox
                Else
                   MsgBox "Match Not Found For: " & Search & " - Please Try Again.", _
                    , "Invalid Search Criterion!"
                    txtsearch.SetFocus
        As you may see I am very new to Vb coding and most of what I am using has been gleened from examples found on the net, but I am trying.

        Ezzz
        Last edited by NeoPa; May 10 '10, 03:39 PM. Reason: Please use the [CODE] tags provided.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32654

          #5
          Ezzz, you have been told a number of times now to use the CODE tags when posting code. Please do so in future.

          -Administrator.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by Ezzz
            ADezii
            I did like your solution and tried to re-code my search formula but got bogged down with too many "If" statements. See below:-
            Code:
            'If matching record found sets focus in SerialNumber and shows msgbox
            'and clears search control
            
                If SerialNumberRef = Search Then
                    MsgBox "Match Found For: " & Search, , vbRetryCancel, "Congratulations!"
                    If Search = vbRetry Then
                    DoCmd.FindNext
                    'Do your FindNext Operation
                    Else
                    DoCmd.Close
                    'Satisfied by initial Find
                    SerialNumber.SetFocus
                    txtsearch = ""
                    
                'If value not found sets focus back to txtSearch and shows msgbox
                    Else
                       MsgBox "Match Not Found For: " & Search & " - Please Try Again.", _
                        , "Invalid Search Criterion!"
                        txtsearch.SetFocus
            As you may see I am very new to Vb coding and most of what I am using has been gleened from examples found on the net, but I am trying.

            Ezzz
            Syntax is all wrong, I'll rewrite and return later.

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              This is the General idea:
              Code:
              'If matching record found sets focus in SerialNumber and shows msgbox
              'and clears search control
              Dim intResponse As Integer
              
              'Hopefully, SerialNumberRef and Search are Declared/Initialized somewhere
              If SerialNumberRef = Search Then
                intResponse = MsgBox("Match Found For: " & Search, , vbRetryCancel, "Congratulations!")
                  If intResponse = vbRetry Then
                    'Do your FindNext Operation
                    DoCmd.FindNext
                  Else
                    'Satisfied by initial Find
                    DoCmd.Close   'Close the Form & Set Focus to a Control on it?
                    SerialNumber.SetFocus
                    txtsearch = ""
                  End If
              'If value not found sets focus back to txtSearch and shows msgbox
              Else
                MsgBox "Match Not Found For: " & Search & " - Please Try Again.", _
                        , "Invalid Search Criterion!"
                             txtsearch.SetFocus
              End If

              Comment

              • Ezzz
                New Member
                • Jan 2010
                • 28

                #8
                Message box Option Buttons

                Firstly apologies to the administrator re-the coding tags, hopefully I now understand where I was going wrong.

                Secondly ADezi thanks for the responce tried your code but couldn't get it to work.
                I have adapted it slightly (see below) and it nearly works!
                When it carries out the "DoCmd.FindNext " function the search does indeed find the next serial number but ONLY the next one. What I'd hoped was to be able to keep retrying until it finds the desired item. Is there a way to loop the function?
                Furthermore the close function is wrong. The "DoCmd.Clos e" actually wants to close the form and all I wanted it to do was stop the search. Also it doesnt like the SerialNumber.Se tFocus txtsearch = "" after the close, but when i remove them it throws up a debug problem and highlights the DoCmd.Close statement.
                Any ideas

                Ezzz

                Code:
                'If matching record found sets focus in SerialNumber and shows msgbox
                'and clears search control
                
                Dim intResponse As Integer
                   
                'Hopefully, SerialNumberRef and Search are Declared/Initialized somewhere
                    
                    If SerialNumberRef = Search Then
                        intResponse = MsgBox("Match Found For: " & Search, vbRetryCancel, "Congratulations!")
                        SerialNumber.SetFocus
                        txtsearch = ""
                    
                    If intResponse = vbRetry Then
                      'Do your FindNext Operation
                      DoCmd.FindNext
                    Else
                      'Satisfied by initial Find
                      DoCmd.Close   'Close the Form & Set Focus to a Control on it?
                      SerialNumber.SetFocus
                      txtsearch = ""
                    End If
                        
                    'If value not found sets focus back to txtSearch and shows msgbox
                        Else
                           MsgBox "Match Not Found For: " & Search & " - Please Try Again.", _
                            , "Invalid Search Criterion!"
                            txtsearch.SetFocus
                    End If
                End Sub

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32654

                  #9
                  Originally posted by Ezzz
                  Ezzz: Firstly apologies to the administrator re-the coding tags, hopefully I now understand where I was going wrong.
                  Good to hear Ezzz.

                  I can be PMed if you have any difficulties. I'm always happy to help members with the techniques of using the forum well and fully.

                  -NeoPa.

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    The basic concept would be:
                    Code:
                    Do
                      intResponse = MsgBox("Find Another Match?", vbRetryCancel, "Continue Find")
                        DoCmd.FindNext
                    Loop Until intResponse = vbCancel

                    Comment

                    • Ezzz
                      New Member
                      • Jan 2010
                      • 28

                      #11
                      Message box Option Buttons

                      Thanks ADezii
                      Almost works now, one minor flaw left, when i select the retry button the search does go to the next serial number in line and so on, great.
                      But when i find the asset i want and press the cancel button the search jumps one asset and displays the next in line not the one i stopped on?
                      See code below:-
                      Code:
                      'If matching record found sets focus in SerialNumber and shows msgbox
                      'and clears search control
                      
                      Dim intResponse As Integer
                         
                      'Hopefully, SerialNumberRef and Search are Declared/Initialized somewhere
                          
                          If SerialNumberRef = Search Then
                              intResponse = MsgBox("Match Found For: " & Search, vbRetryCancel, "Congratulations!")
                              SerialNumber.SetFocus
                              txtsearch = ""
                          
                          If intResponse = vbRetry Then
                            'Do your FindNext Operation
                            DoCmd.FindNext
                          'If this is not the asset you are looking for then try again
                          Do
                          intResponse = MsgBox("Find Another Match For: " & Search, vbRetryCancel, "Continue Find")
                          DoCmd.FindNext
                          Loop Until intResponse = vbCancel
                          
                          Else
                            'Satisfied by initial Find and close search
                      
                          End If

                      Comment

                      • ADezii
                        Recognized Expert Expert
                        • Apr 2006
                        • 8834

                        #12
                        Try:
                        Code:
                        Do
                          intResponse = MsgBox("Find Another Match For: " & Search, vbRetryCancel, "Continue Find")
                            If intResponse = vbRetry Then DoCmd.FindNext
                        Loop Until intResponse = vbCancel

                        Comment

                        • Ezzz
                          New Member
                          • Jan 2010
                          • 28

                          #13
                          Well done thats it you've cracked it.
                          Much appreciated.
                          Regards
                          Ezzz

                          Comment

                          Working...