How to back to the form after MsgBox to select a value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • osman7king
    New Member
    • Sep 2010
    • 61

    How to back to the form after MsgBox to select a value?

    Hi dears,

    When the user press the Save button, the program should insure that a combo box has a value select.
    If not selected, a message box with single OK button should be opened. but when I press this button, the program is going to complete.

    I want to reinforce the program to back to the form after pressing OK button to select from a specific combo box.

    what is the best way?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    We need to see your code.

    Comment

    • jimatqsi
      Moderator Top Contributor
      • Oct 2006
      • 1292

      #3
      I think this is what you want. Put this in the click event of the Save button prior to the code that actually does the save. Of course "xyz" needs to be changed to whatever is appropriate.

      Code:
      If MsgBox("You have not selected anything for xyz", vbOKCancel) = vbCancel Then
          DoCmd.CancelEvent
          Exit Sub
      End If

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        osman7king

        Before you try any changes to your code, I would highly advise that you follow Rabbit's advise and post your calling code.
        Before you post, please follow the general instructions in this link: >> Before Posting (VBA or SQL) Code.

        Comment

        • osman7king
          New Member
          • Sep 2010
          • 61

          #5
          Ok Dears,
          this is my code:
          Code:
          Private Sub save_Click()
          On Error GoTo Err_save_Click
          Dim v_balance As Double
          Dim v_final_balance As Double
          Dim v_move_type As String
          Dim strSQL As String
          Dim db As DAO.Database
          
          Set db = CurrentDb
          
          DoCmd.RunCommand acCmdSaveRecord
          
          
          If Forms![cash].employees_combo.Value = 0 And Forms![cash].cash_out_type_combo = 2 Then
              MsgBox("Please, Select an employee", vbOKOnly, "NO Employee Selected")
          
          
          If Forms![cash].currency_combo.Value = "USD" And Forms![cash].[move_type] = 1 Then
                      DoCmd.OpenQuery "Q_get_balance_usd"
                      v_balance = DLookup("colsing_balance_usd", "Q_get_balance_usd")
                      
                      v_final_balance = v_balance + Forms![cash].[cash_in_usd] * Forms![cash].[move_type]
                               
                      
                      strSQL = "UPDATE balanceT SET colsing_balance_usd = " & v_final_balance
                      db.Execute strSQL, dbFailOnError
                      
                      
                      strSQL = "UPDATE cashT SET balance_usd = " & v_final_balance & " WHERE cash_id = " & cash_id
                      db.Execute strSQL, dbFailOnError      
                       
                                
                  ElseIf Forms![cash].currency_combo.Value = "IQD" And Forms![cash].[move_type] = 1 Then
                          DoCmd.OpenQuery "Q_get_balance_iqd"
                          v_balance = DLookup("closing_balance_iqd", "Q_get_balance_iqd")
                          
                          v_final_balance = v_balance + Forms![cash].[cash_in_iqd] * Forms![cash].[move_type]
                          
                          strSQL = "UPDATE balanceT SET closing_balance_iqd = " & v_final_balance
                          db.Execute strSQL, dbFailOnError
                      
                    
                      
                          strSQL = "UPDATE cashT SET balance_iqd = " & v_final_balance & " WHERE cash_id = " & cash_id
                          db.Execute strSQL, dbFailOnError
                          
                          
                          
                          
                  ElseIf Forms![cash].currency_combo.Value = "USD" And Forms![cash].[move_type] = -1 Then
                          DoCmd.OpenQuery "Q_get_balance_usd"
                          v_balance = DLookup("colsing_balance_usd", "Q_get_balance_usd")
                      
                          v_final_balance = v_balance + Forms![cash].[cash_out_usd] * Forms![cash].[move_type]
                          
                          strSQL = "UPDATE balanceT SET colsing_balance_usd = " & v_final_balance
                          db.Execute strSQL, dbFailOnError
                      
                      
                          strSQL = "UPDATE cashT SET balance_usd = " & v_final_balance & " WHERE cash_id = " & cash_id
                          db.Execute strSQL, dbFailOnError
                                            
                          
                  ElseIf Forms![cash].currency_combo.Value = "IQD" And Forms![cash].[move_type] = -1 Then
                          DoCmd.OpenQuery "Q_get_balance_iqd"
                          v_balance = DLookup("closing_balance_iqd", "Q_get_balance_iqd")
                          
                          v_final_balance = v_balance + Forms![cash].[cash_out_iqd] * Forms![cash].[move_type]
                          
                          strSQL = "UPDATE balanceT SET closing_balance_iqd = " & v_final_balance
                          db.Execute strSQL, dbFailOnError
                      
                      
                          strSQL = "UPDATE cashT SET balance_iqd = " & v_final_balance & " WHERE cash_id = " & cash_id
                          db.Execute strSQL, dbFailOnError
                   End If
                          
          Set db = Nothing
          
          'To Load the new Balances
           balance_usd = DLookup("colsing_balance_usd", "balanceT")
           balance_iqd = DLookup("closing_balance_iqd", "balanceT")
          
          
          'To clear the window...
           DoCmd.GoToRecord , , acNewRec
           
           'To hide employee list
           Forms![cash].employees_combo.Visible = False
          
          Exit_save_Click:
              Exit Sub
          
          Err_save_Click:
              MsgBox Err.Description
              Resume Exit_save_Click
              
          End Sub
          Thanks.

          Comment

          • jimatqsi
            Moderator Top Contributor
            • Oct 2006
            • 1292

            #6
            Change
            Code:
            If Forms![cash].employees_combo.Value = 0 And Forms![cash].cash_out_type_combo = 2 Then
                MsgBox("Please, Select an employee", vbOKOnly, "NO Employee Selected")
            to
            Code:
            If Forms![cash].employees_combo.Value = 0 And Forms![cash].cash_out_type_combo = 2 Then
            
                MsgBox("Please, Select an employee", vbOKOnly, "NO Employee Selected")
            docmd.CancelEvent
            End If
            In line 11 you are saving the record before doing any of the combo box checking. Move line 11 down past that If Then test.

            Also, what is the reason for opening all those queries that are followed by DLookup? You do not have to open a query before using it in a DLookup.

            Jim
            Last edited by jimatqsi; Aug 18 '13, 05:11 PM. Reason: typo and copy/paste error

            Comment

            • jimatqsi
              Moderator Top Contributor
              • Oct 2006
              • 1292

              #7
              I believe in this case "exit sub" would work as well as the CancelEvent at line 4 of my code.

              Comment

              Working...