Input Box and CancelEvent

Denmla

Registered User.
Local time
Today, 16:55
Joined
Aug 9, 2012
Messages
13
Hello all,

I need help with the Input Box and CancelEvent.
The following is a function that I made.
The point of all is that over the function Input Box it is import into a Sken table.
At the same time, I need help how to stop import before if validation fails.
I put the command CancelEvent but not work.

So we need to suspend import if you do not pass validation.
If the validation does not pass puts the message "Do you want to delete the scanned barcode"

Private Sub Command0_Click()
DoCmd.SetWarnings False
Dim Barkod As String
Barkod = "?"
While Barkod = "?"
DoCmd.Requery
Barkod = InputBox("Unesi barkod", "Barkod", , 1, 15)
If Barkod <> "" Then
CurrentDb.Execute "INSERT INTO Sken VALUES ('" & Barkod & "');"
Me.Refresh
Barkod = "?"
If IsNull(Forms![FrmZaprimanje2]![SubZaprimanje].Form![Artikl]) Then
Dim MyAnswer As Integer
MyAnswer = vbCancel
Do While MyAnswer = vbCancel
MyAnswer = MsgBox("Scanned barcode is not in database", vbOKCancel + vbInformation + vbDefaultButton2, "Information")
Loop
If MsgBox("Do you want to delete the scanned barcode?", vbOKCancel + vbCritical + vbDefaultButton1, "Delete article") = vbOK Then
DoCmd.CancelEvent
DoCmd.Requery
Me.Refresh

End If
End If
End If
Wend

End Sub
 
I don't see how testing a form control tells you if the InputBox result is valid. I'd probably use a DLookup() or recordset to test the variable, and only do the append if the validation passed.
 
First things first. Beware of nomenclature here because it could lead you to look up the wrong thing. "Cancel" of an event is a real concept in VBA/Access, but not all events can be canceled. The bad thing about DoCmd.CancelEvent is that it is misleading. If the event entry point did not include an argument "Cancel as Integer" then .CancelEvent won't work.

For example, Form_Open can be canceled (it has the Cancel argument) but button-clicks cannot be canceled. Button clicks CAN be dismissed (or undone and dismissed), which is what you are doing.

What you have to do is put your validation code into the top of the click-event code and put a label at the bottom where you exit from the event. Put another label on a code segment that does your "Undo" type of record operation. Put another label on the code that implements your operation because it was allowed.

Now do your click-event. Test your validation conditions. Jump to either the "Undo" code or the "Do-it" code. In the Undo code, if you had to do something that needs to be undone, that is where you ask your question. I can't tell you what is right if your user says "Don't delete the record" - that is your decision because it is specific to your problem. But eventually, all paths have to end up going through the click-exit code.

By the time you get to that click-exit code, everything has to be done, either to accept, reject, or leave an entry dangling, whatever is right for your situation. (We won't be able to counsel you on that question.) But here is the thing to understand that makes this easier to understand.

If you click a button, determine by test that it is invalid to do so, and don't leave your code in an impossible state, the user could go fix the problem and come back to do the button click again. Maybe this time, it would work if the previous disqualifying condition has now been remedied! You can't cancel the click - but you can choose to ignore it and dismiss it.

Hope that made sense and was applicable to your situation.
 
Thank you for detail explain.
Can you tell me how can delete last record if get message:


Code:
If MsgBox("Do you want to delete the scanned barcode?", vbOKCancel +  vbCritical + vbDefaultButton1, "Delete article") = vbOK Then
 

Users who are viewing this thread

Back
Top Bottom