Infobox cancel button

shafara7

Registered User.
Local time
Today, 10:52
Joined
May 8, 2017
Messages
118
I have a button, where if I click it, it will promt a MsgBox and InputBox before the action is done.

Code:
Private Sub btnWorkDone_Click()
    Dim WorkHours As Integer
    On Error Resume Next

    If Not (IsNull([datTransferIs]) Or IsNull([datTransferShould])) Then
            WorkHours = InputBox("Actual working hours:", "Update Working Hours")
            CurrentDb.Execute "UPDATE tblWork SET datActualTransfer = " & WorkHours & " WHERE lngTransferNr = " & [lngTransferNr] & ";"


            If MsgBox("Are you sure you want to send this bill?", vbYesNo, "Billtransfer confirmation?") = vbYes Then
                CurrentDb.Execute "UPDATE tblWork SET datMeasurementIs = '" & Date & "' WHERE lngTransferNr = " & [lngTransferNr] & ";"
                Me.Requery 
            End If

    Else: MsgBox "This Bill cannot be sent yet", vbCritical, "Error"

    End If
End Sub


How do I stop the MsgBox from executing if I the InputBox is left blank or I click cancel at the InputBox?
Or is there a code that will only show the OK button?
 
Last edited:
Various ways of doing this.
For example, do one of these:

1. After the line "WorkHours = InputBox ...", add this:
Code:
If Nz(WorkHours,"")="" Then Exit Sub

2. Add error handling - depending on your other code, you may get error 5 or 13 or 2501 if user clicks Cancel.
If so, add something like this:

Code:
...
Err_Handler:
      If Err = 13 Then Resume Next

End Sub
 
....
Workhours= inputBox(....
If WorkHours=0 then exit sub


Note: this means you cannot set hrs =0. If that is ok ,then use code above.
Otherwise make WorkHours a variant then test for : IsNull(workHours)
 
Thank you for your replies :)
That will only stop the InputBox from making any changes. After that, the MsgBox appears.
How do I stop the MsgBox from executing, when I the Inputbox is canceled?
 
If you use my first bit of code it will exit without calling the MsgBox

If you use the second bit of code, modify the "Resume Next" to "Exit Sub"
 
I did but somehow it still appears.
But then I changed it to
Code:
If Messdauer = False Then Exit Sub
Then it works.

Thank you for your idea :)
 

Users who are viewing this thread

Back
Top Bottom