Running VBA code automatically after clicking a button with macro command (1 Viewer)

heavenbound4life

New member
Local time
Today, 13:41
Joined
Dec 14, 2012
Messages
22
Hello everyone, I have very little knowledge of VBA. Please, how can I make the code below run automatically after clicking a button with macro command? What I wanted is running the VBA code automatically after the macro must have completed its job. Thank you.

Code:
Private Sub cmdStrt_Click()

    With CodeContextObject
        On Error Resume Next
        DoCmd.GoToRecord , "", acNewRec
        If (.MacroError <> 0) Then
            Beep
            MsgBox .MacroError.Description, vbOKOnly, ""
        End If
    End With

    Me.InvoiceDate.Value = Format(Date, "dd-mmm-yyyy")
    Me.Employee.Value = Format([TempVars]![CurrentUser])
  
If Not IsNull(Me.InvoiceDate) Then
         Me![frmSubformSales].Form.ItemID.Enabled = True
         Me![frmSubformSales].Form.Quantity.Enabled = True
End If

End Sub
 
Last edited:

heavenbound4life

New member
Local time
Today, 13:41
Joined
Dec 14, 2012
Messages
22
i am a bit confused by your question.

Macros can be attached to click event of a form control or command button, but macros don't have a click event.
I'm sorry. What I wanted is running the VBA code automatically after the macro must have completed its job
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:41
Joined
Oct 29, 2018
Messages
21,358
Hi. Macros have a Run Code action, but I think they can only call functions and not subroutines. Also, it has to be public and not private.
 

oleronesoftwares

Passionate Learner
Local time
Today, 05:41
Joined
Sep 22, 2014
Messages
1,159
I'm sorry. What I wanted is running the VBA code automatically after the macro must have completed its job
You can follow @theDBguy's suggestion or

Add the vba code to run the macro , the code will be just before your current code, i.e the line before it e.g
application.DoCmd.RunMacro ............

Code:
Private Sub cmdStrt_Click()

    With CodeContextObject
        On Error Resume Next
        application.DoCmd.RunMacro .................
        DoCmd.GoToRecord , "", acNewRec
        If (.MacroError <> 0) Then
            Beep
            MsgBox .MacroError.Description, vbOKOnly, ""
        End If
    End With

    Me.InvoiceDate.Value = Format(Date, "dd-mmm-yyyy")
    Me.Employee.Value = Format([TempVars]![CurrentUser])
 
If Not IsNull(Me.InvoiceDate) Then
         Me![frmSubformSales].Form.ItemID.Enabled = True
         Me![frmSubformSales].Form.Quantity.Enabled = True
End If

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:41
Joined
Oct 29, 2018
Messages
21,358
Is it possible to convert an embedded macro to VBA code?
You can go to the Form's design view and click the Convert Form's Macros to Visual Basic button, but that will convert all embedded macros on that form. You can't do just one.
 

heavenbound4life

New member
Local time
Today, 13:41
Joined
Dec 14, 2012
Messages
22
You can follow @theDBguy's suggestion or

Add the vba code to run the macro , the code will be just before your current code, i.e the line before it e.g
application.DoCmd.RunMacro ............

Code:
Private Sub cmdStrt_Click()

    With CodeContextObject
        On Error Resume Next
        application.DoCmd.RunMacro .................
        DoCmd.GoToRecord , "", acNewRec
        If (.MacroError <> 0) Then
            Beep
            MsgBox .MacroError.Description, vbOKOnly, ""
        End If
    End With

    Me.InvoiceDate.Value = Format(Date, "dd-mmm-yyyy")
    Me.Employee.Value = Format([TempVars]![CurrentUser])

If Not IsNull(Me.InvoiceDate) Then
         Me![frmSubformSales].Form.ItemID.Enabled = True
         Me![frmSubformSales].Form.Quantity.Enabled = True
End If

End Sub
Thank you very much. It is an embedded macro. Can it work?
 

heavenbound4life

New member
Local time
Today, 13:41
Joined
Dec 14, 2012
Messages
22
You can go to the Form's design view and click the Convert Form's Macros to Visual Basic button, but that will convert all embedded macros on that form. You can't do just one.
I tried it but I didn't see any changes anywhere except the creation of backup and after that, it closes and opens the DB. I opened the embedded macro and saw the same Convert Form's Macros to Visual Basic button but this time it is disabled. I don't really know what is going on. Maybe my macros are not supported by the conversion process.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:41
Joined
Oct 29, 2018
Messages
21,358
I tried it but I didn't see any changes anywhere except the creation of backup and after that, it closes and opens the DB. I opened the embedded macro and saw the same Convert Form's Macros to Visual Basic button but this time it is disabled. I don't really know what is going on. Maybe my macros are not supported by the conversion process.
Assuming you had an embedded macro when you click a button, then after converting it to VBA, try clicking on that same button again to see if the code still work. If so, check it in design view to see if you're still using a macro or VBA. Since you said the ribbon button is now grayed out, it tells me you're using VBA now.
 
Last edited:

Users who are viewing this thread

Top Bottom