Tyrone
Tyrone

Reputation: 77

Converting Macros to VBA from Outside Current Database

I'd like a way to programatically convert Access macros to VBA modules. I know how to do this manually. I was working with some code to do this within the current database and I got it to work. However, I would like to be able to do this outside of the current database. I plan to have this program housed with other utility type programs in the same database. Below is my code. I am getting a message that says, "Microsoft Access cannot find the test1 you referenced in the Object Name arguement. Do you have any suggestions on how to acheive what I want to acheive?

Private Sub Command2_Click()
MsgBox "start here"
Dim strDB As String
Dim appAccess As Access.Application
Dim m As Object

strDB = "C:\Users\me\Desktop\testme.accdb"

Set appAccess = New Access.Application
appAccess.OpenCurrentDatabase strDB

For Each m In appAccess.CurrentProject.AllMacros

    Debug.Print m.Name
    DoCmd.SelectObject acMacro, m.Name, True
    DoCmd.RunCommand acCmdConvertMacrosToVisualBasic

Next

Set appAccess = Nothing

End Sub

Upvotes: 1

Views: 185

Answers (1)

HansUp
HansUp

Reputation: 97131

DoCmd is a member of the Application object. Ask those commands to work from the new Application object instance you created (appAccess) instead of the Application instance which is running your Command2_Click() procedure.

appAccess.DoCmd.SelectObject acMacro, m.Name, True
appAccess.DoCmd.RunCommand acCmdConvertMacrosToVisualBasic

In other words, basically do the same for DoCmd as you did with appAccess.OpenCurrentDatabase and appAccess.CurrentProject ... you associated them to the specific Application instance.

Upvotes: 1

Related Questions