Search code examples
ms-accessvbacallmsgbox

VBA msgBox - vbYes keeps calling wrong sub


If I click on yes, VBA keeps going to the function Mandate? What did I do wrong?

Public AutoDate As Date
Public NewDate As String

Public Sub GetDate()  ' DATUM BEPALEN
    AutoDate = Date - 1
    MsgBox (AutoDate), (vbYesNo), ("Datum")
    Dim Response As VbMsgBoxResult
        If Response = vbYes Then
            NewDate = AutoDate
            Call DeleteDate
        Else    ' No
            Call ManDate
        End If
End Sub

Solution

  • You haven't assigned the result of MsgBox to Response.
    Not sure if VbMsgBoxResult is a valid data type in that instance either.

    Try either of these:

    Public Sub GetDate()  ' DATUM BEPALEN
    
        AutoDate = Date - 1
    
        If MsgBox(AutoDate, vbYesNo, "Data") = vbYes Then
            NewDate = AutoDate
            Call DeleteDate
        Else    ' No
            Call ManDate
        End If
    
    End Sub
    

    or

    Public Sub GetDate()  ' DATUM BEPALEN
    
            Dim Response As Long
    
            AutoDate = Date - 1
            Response = MsgBox(AutoDate, vbYesNo, "Data")
    
            If Response = vbYes Then
                NewDate = AutoDate
                Call DeleteDate
            Else    ' No
                Call ManDate
            End If
    
        End Sub