Search code examples
vbams-access-2013

Show Message in text box when program ends successfuly or not


I have the below code that when it runs updates 14 tables, wondering how can I show a msg using the text boxes in the template below showing whether the subs end successfully or not.

[Private Sub Command0_Click()

   'Sub 1
    A_Forecast
    
    'Text box showing the msg OK or failed
    A_ForecastTxt "OK" or "Not"

   'Sub 2
    B_Forecast
    
    'Text box showing the msg OK or failed
    B_ForecastTxt "OK" or "Not"

   
End Sub][1]

enter image description here


Solution

  • Turn the subs into functions as these can return a value:

    Private Sub Command0_Click()
    
        Dim Success As Boolean
    
        ' Function 1
        Success = A_Forecast
        
        ' Text box showing the msg OK or failed
        MsgBox IIf(Success, "OK", "Failed")
    
        ' Function 2
        Success = B_Forecast
        
        ' Text box showing the msg OK or failed
        MsgBox IIf(Success, "OK", "Failed")
       
    End Sub
    

    Example:

    Public Function A_Forecast() As Currency
    
        Dim Result As Currency
        Dim Something As Boolean
    
        ' Your code:
        Something = True ' or False
    
        If Something = True Then
            Result = 100
        Else
            Result = 200
        End If
    
        A_Forecast = Result
    
    End Function