New to VBA - Macro to VBA Conversion and the results changed!

aabrazil

New member
Local time
Today, 15:38
Joined
Jan 27, 2020
Messages
1
I'm brand new to VBA and I really appreciate any insight!

The following code was generated by converting from Macros to VBA using the design tab in Access 2016. Why did the action change after the conversion to VBA?

Prior to the conversion and minor edit below in bold, the If MacroError message box would appear if any required field was empty and the save button was pressed.

After the conversion and minor edit, the If macroError message is skipped and the Else "Your number is ... Record is saved" message box always displays. The Else "You number is ... Record is saved" now displays even when required fields are skipped.



----------------Beginning Code----------------------------
Private Sub Save_Record_and_Clear_Form_Click()
On Error GoTo Save_Record_and_Clear_Form_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Else
MsgBox "Your record number is " & Me.ID & ". In the event of any changes request to this form, please provide this record number.", vbOKOnly, "Record is Saved – Thank you"
End If
Save_Record_and_Clear_Form_Click_Exit:
Exit Sub
Save_Record_and_Clear_Form_Click_Err:
MsgBox Error$
Resume Save_Record_and_Clear_Form_Click_Exit
End Sub
----------------- Ending Code --------------------------


Thank you for your help!
 
In future, please post code between CODE tags to retain indentation and readability.

Do you have Option Explicit at top of every module header?

I suspect the MacroError object no longer has relevance. Step debug code, what does MacroError show as value?

I don't use macros but every time I've seen a conversion, have to do more than just a 'minor edit'. For VBA error handling review http://allenbrowne.com/ser-23a.html.
 
Last edited:
turn off both of these lines
On Error GoTo Save_Record_and_Clear_Form_Click_Err
On Error Resume Next
and step through the code. You will probably find that macroerror is not 0, therefore that block won't run. Closing the form and reopening to test further may resolve a situation where the value in that property is retained. MacroError is recognized by VBA, so Option Explicit really won't apply although I don't know its relevance here. If you are trying to validate form controls have data, I'd abandon this altogether. BTW, to direct flow with one OnError line only to redirect it in the very next line makes no sense. If you research, you'll find plenty of code for validating forms.
 

Users who are viewing this thread

Back
Top Bottom