From Excel, I need to open an Access database and run one of the database's macros.
I'm using Excel and Access 2007. Here is my code in Excel:
Sub accessMacro()
Dim appAccess As New Access.Application
Set appAccess = Access.Application
appAccess.OpenCurrentDatabase "C:\blah.mdb"
appAccess.Visible = True
appAccess.DoCmd.RunMacro "RunQueries.RunQueries"
appAccess.CloseCurrentDatabase
End Sub
In the Access database, there is a procedure named RunQueries in a module named RunQueries.
I get:
Runtime error '2485':
Microsoft Access Office can't find the object 'RunQueries.'
I also tried
appAccess.DoCmd.RunMacro "RunQueries"
and I get the same errors message.
I argued against it, and I have to do it this way (meaning, I have to use Excel as a frontend to open several Access dbs and run their macros).
What about this syntax ?
appAccess.run "RunQueries.RunQueries"
By the way, I always avoid naming a module like a procedure. This is looking for trouble.
Sub accessMacro()
Dim appAccess As Access.Application
Set appAccess = New Access.Application
appAccess.OpenCurrentDatabase "C:\blah.mdb"
appAccess.Visible = True
appAccess.DoCmd.RunMacro "Macro Name" '<-- As it appears in the Macro Group in the Access Interface.
appAccess.CloseCurrentDatabase
End Sub
How about this:
appAccess.Modules.Application.Run "macro_name"
The macro name doesn't need the Module name to function for me.
The msdn site didn't shed too much light, but I have a feeling that their disclaimer applies here. Here's what they mentioned:
If you run Visual Basic code containing the RunMacro method in a
library database, Microsoft Access looks for the macro with this name
in the library database and doesn't look for it in the current
database.
Of course they don't mention how exactly to remedy this issue! But after reviewing the answers above I think it would be helpful to post a full answer:
Sub accessMacro()
Dim appAccess As New Access.Application
Set appAccess = Access.Application
appAccess.OpenCurrentDatabase "C:\blah.mdb"
appAccess.Visible = True
appAccess.Run "RunQueries"
appAccess.CloseCurrentDatabase
End Sub
This worked when I ran it. Good luck! :D
-Reverus
Try this:
Sub accessMacro()
Dim appAccess
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase "C:\blah.mdb"
appAccess.Visible = True
appAccess.DoCmd.RunMacro "RunQueries.RunQueries"
appAccess.CloseCurrentDatabase
End Sub
This doesn't specifically address the "RunQueries" version, but this works in Access 2019.
Note that the Application object has to be created and initialized a bit differently than in the previous examples (and this ends with Set [object] = Nothing).
Although not mentioned, TXE_DEN.accdb has a tie-in to a separate library database MLO_Library.accdb and a lot of the subroutines in DEN access routines in Library. The macro in the example is in the TXE_DEN database, not the Library. If it were in the Library, I don't know whether it could be accessed through the TXE_DEN database as shown.
Also, in the Navigation Pane the example macro shows up in "Unrelated Objects". So in other cases--e.g., RunQueries--it might be necessary to include a module name in the identifier.
And just to avoid confusion--this macro does not do anything to anything in Excel. It's just "Well, I'm running THIS EXCEL stuff and I also need to run THAT ACCESS stuff, so I'll digress to Access and run that and then continue with my EXCEL stuff."
Sub Run_Access_Macro()
Dim appAccess As Object
Set appAccess = CreateObject("Access.Application")
Dim AccessDB As String
AccessDB = "F:\PATH WITH SPACES\TDN\TXE_DEN.accdb"
' format: module.macro
Dim AccessMacro As String
AccessMacro = "0 - Import TDN"
appAccess.OpenCurrentDatabase AccessDB
appAccess.Visible = True
appAccess.DoCmd.RunMacro AccessMacro
appAccess.CloseCurrentDatabase
Set appAccess = Nothing
End Sub
Related
I have a code that open access from excel:
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
objShell.Open "\\acrtnd\share$\PUBLIC\!tools\preportDST.accdb"
Set objShell = Nothing
I would like to little automatized that and add to it code that will paste value in to field in access and run macro in access that start calculation, but I have not knowidge about manageing other office parts from inside excel. The text box name is "txt_PasteField" and VBA macro in access is name "run_calculation", I try something like this below but objShell do not allow such actions.
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
objShell.Open "\\acrtnd\share$\PUBLIC\!tools\preportDST.accdb"
objShell.txt_PasteField.Value = Sheets("dashboard").Cells(5, 1).Value
call objShell.run_calculation
Set objShell = Nothing
Can someone provide be way to do that or navigate me to needed command?
Thanks in advance for helping.
I'll try to help you, but I'm still a little unclear on the details. Since a text field has to be either on a form or a report, you have to open it first. Also, if you want to influence the Access application after opening it, you will need to use OLE automation.
Dim appAccess As Object
Set appAccess = CreateObject("Access.Application")
With appAccess
.OpenCurrentDatabase "\\acrtnd\share$\PUBLIC\!tools\preportDST.accdb"
.UserControl = True
.DoCmd.OpenForm "MyForm"
.Forms!MyForm.txt_PasteField.Value = Sheets("dashboard").Cells(5, 1).Value
.Run "run_calculation"
End With
I have a project written in Vb .NET in which I take input from the user, open a Excel template and run a macro in it(vba) with input from main form.
I can do all of this without a problem using a path to the template but I need it to be part of the project when I publish it.
This is my path version code(excess code deleted):
Imports Excel = Microsoft.Office.Interop.Excel
private sub OpenExcel()
Dim objApp As Object
Me.Hide()
objApp = CreateObject("Excel.Application")
objApp.WorkBooks.Open("ExampleWorkBook.xlsm")
objApp.visible = True
objApp.Run("MacroName", Var1, Var2)
Me.Close()
End Sub
I have found this post but it doesn't work, maybe because my templates are in a folder(which is located in same location as form1.vb etc).
My templates must be separate from other files so they are easy to find.
If anyone could provide me with a solution/modify code from other post in such a way it works I would be really thankful
I'm using Visual Studio 2017 and Excel 2010
Code from linked post:
Dim filename as String = My.Application.Info.DirectoryPath & System.IO.Path.DirectorySeparatorChar & "WorkbookName.xlsx"
Process.Start(filename)
I have managed to find a solution but then I have run into another problem: I would get an error saying the path is invalid, object is missing. I was sure it was a problem with path but it was related to resource properties(type:content instead of none and copy always so it is in the folder with the application).
Code for opening the excel template:
Imports Excel = Microsoft.Office.Interop.Excel
private sub OpenExcel()
Dim objApp As Object
Me.Hide()
'This path is independent of where the program is installed as it refers to program itself
Dim ResourcePath As String = My.Application.Info.DirectoryPath &
System.IO.Path.DirectorySeparatorChar & "Templates\Rate_Reach_ATT finished.xlsm"
objApp = CreateObject("Excel.Application")
objApp.WorkBooks.Open(ResourcePath)
objApp.visible = True
objApp.Run("MacroName", Var1, Var2)
Me.Close()
End Sub
As for resource itself:
Project>Add existing item>browse>add
Then find the item in solution explorer, open it's properties and change:
Type:Content
Copy to Output Directory: Copy Always
The issue has been fixed so I close this thread
From VBA in Excel (2013), I need to create a standard module in a MS Access (2013) database. I am open to any and all ideas - Thanks!
OK, I have it working, but I am sure there are aspects that could/should be done differently. As a reminder, the code below is in MS Excel. Here is the code:
'REQUIRES: MS Access 12.0 Object Library
Dim ObjAccess As Access.Application
Dim strPath As String
strPath = "C:\Temp\MyDB_DEV_1.MDB"
Set ObjAccess = New Access.Application
With ObjAccess
.OpenCurrentDatabase strPath, True
.DoCmd.RunCommand acCmdNewObjectModule
.DoCmd.Save acModule, "Module1"
.DoCmd.Rename "MyCodedModule", acModule, "Module1"
.CloseCurrentDatabase
.Quit
End With
Set ObjAccess = Nothing`
The natural evolution of the previous solution is to want to add code to the new module. I decided to take an alternate route and "import" an existing standard module into the remote MS Access db. A final step in the evolution is to remotely run a procedure that is within the standard module that was imported into the remote db. Again, this code is located in Excel VBA. The code that accomplishes these two tasks are as follows:
'FROM EXCEL, REMOTELY INSTRUCT A MS ACCESS db
'IMPORTS .BAS FILE INTO db
'EXECUTE THE 'RoutineWithinModule1' THAT IS WITHIN MODULE1 OF THE REMOTE db
Dim appAccess As Access.Application
Set appAccess = New Access.Application
With appAccess
.OpenCurrentDatabase "C:\Temp\MyDB_DEV_1.mdb"
.VBE.ActiveVBProject.VBComponents.Import "C:\Module1.bas"
.DoCmd.Save acModule, "Module1"
.Run "RoutineWithinModule1"
.CloseCurrentDatabase
.Quit
End With
Set appAccess = Nothing
I'm following the instructions here http://software-solutions-online.com/excel-vba-export-worksheet-to-existing-access-table/ to transfer data from an Excel spreadsheet to an Access database. The script I have adapted is:
Sub MailMerge2()
Dim strPath As String
Dim objAccess As Access.Application
Dim strExcelPath As String
strPath = "C:...Documents\MailMerge2"
strExcelPath = Application.ActiveWorkbook.FullName
Set objAccess = New Access.Application
Call objAccess.OpenCurrentDatabase(strPath)
objAccess.Visible = True
Call objAccess.DoCmd.TransferSpreadsheet(acImport, _
acSpreadsheetTypeExcel8, "MyTable1", strExcelPath, _
True, "A1:D11")
End Sub
However, running this gives me an error saying:
Run-time error: 7866, Microsoft Access can't open the database
because it is missing, or opened exclusively by another user, or it is
not an ADP file.
Any suggestions on which of these the problem is? I'm fairly new to Access, and haven't quite got the hang of the terminology yet.
Found the problem. I left out .accdb in my Access db file names.
The first part is now working [
I have the following which just seems to hang; the part that adds/deletes the module works when running in VBA
I note that I'm prompted with a dialog saying 'this workbook contains links to other data sources' which I ok to, then it hangs
So I tried setting the second argument to 0 and also tried 2 but still it hangs
(2nd arg is UpdateLinks as can be found here )
]
dim objExcel
dim objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open( "H:\M\X\C.xls", 0 , , ,"PASSWORD!" )
Const modpath = "H:\M\V\"
Const modtest = "TEST.cls"
Const modname = "TEST"
On Error Resume Next
Dim vbcomp
Set vbcomp = ActiveWorkbook.VBProject.VBComponents(modname)
objWorkbook.VBProject.VBComponents.Remove vbcomp
objWorkbook.VBProject.VBComponents.Import modpath & modtest
objWorkbook.Save
objWorkbool.Close
set vbcomp = nothing
set objworkbook = nothing
set objExcel = nothing
edited again 14/04/2009
I have now also allowed the 'tools - macro - security - vbproject access'
The script now finishes, however, when trying to open the xls to see if the changes have been made, I get a message informing me that the sheet is locked by "account used to run script"; open 'read only'/notify
Why isn't it releasing control correctly**?**
First thought: Does your workbook already contain a reference (within VBA) to the "Microsoft Visual Basic for Applications Extensibility" library? You'll need it to be able to talk to the VBProject object. Actually, you probably do have the reference if your code works in VBA.
Second thought: ActiveWorkbook is probably not defined outside of an actual workbook. Try replacing it with your objWorkbook object.
Here's a third thought. Did you try setting the Application's DisplayAlerts property to FALSE before you include the module?
The edited script works.
The problem was caused by the fact that I was supplying the password at the workbook level and not at the VBA project level.
A quick search on the web reveals that it is not possible to do this anyway (sendkeys etc) so after manually removing the password on the project, the problem is solved