In VBA, cannot use Access.Application object - ms-access

This does NOT work:
Sub X()
Dim A As Access.Application
Set A = CreateObject("Access.Application")
'Do Stuff
End Sub
However, this DOES work:
Sub X()
Dim A As Object
Set A = CreateObject("Access.Application")
'Do Stuff
End Sub
I know they do virtually the same thing, but can anyone tell me how to make an access.application object? I should add that I have Crystal Reports 11 and on my last upgrade, it may have 'unregistered' some VBA DLLs.
(Update 2009-06-29)
In response to the first 2 questions, I am using MS Access VBA to control some other Access & Excel files. Since this will only ever run on my local machine, I can guarantee that Access will always be installed. I have also referenced the "Microsoft Access 11.0 Object Library" (MSACC.OLB).
I know there's ways around this, i.e. use early binding when coding, and switch to late binding when running it, I just don't understand why the early binding method doesn't work at all on my machine (Of course, the code works fine on another machine with Access).

If you are writing this in Access there is no need to do that as the Application object is already there for you. If you are writing this in Excel or Word then you need to add a reference to the Access Library. Go to Tools/References and look for Microsoft Access XX Object Library

Hello,The code that you say is not working is legal syntax. What error are you getting? When does it occur? Do you know the line of code it happens at?
Just as a side note, this is legal syntax as well: Dim accApp As Access.Application
Set accApp = New Access.Application
But to be clear, the CreateObject Syntax is legal and not the source of the problem.

Try Detect And Repair from the Help menu in MS Access. Worked perfect for me.

Related

Access reference library goes missing after opened from newer version

I have built an Access database that is shared on our company network. Most users on the network have Access 2010 installed but some have Access 2016. When the database has been opened by someone with Access 2016 and then subsequently opened by someone with Access 2010 they are getting an error message as the Microsoft Excel 16.0 Object Library is marked "Missing".
I can fix this temporarily by selecting the '14.0 library' but the error recurs as soon as it is opened in the later version again. Is there anything I can do to stop this from happening?
Thanks
You can use late binding, which helps you avoid the issue. Code with late binding works with any appropriate object library, whichever is available.
You need to change the object initializations:
Dim excelApp As Excel.Application
Set excelApp As New Excel.Application
To
Dim excelApp As Object
Set excelApp = CreateObject("Excel.Application")
Note that if you use late binding, intellisense will be unavailable. My usual practice is to develop using early binding, and change it to late binding as soon as it's finished.
Are you sharing an unsplit database or sharing the front end of a split database?
Access is inherently multi user, but one must distribute it correctly. The database must be split into 2 files (this is a feature in the ribbon) and then each user has their own front end file which links to the common single back end file.
Then there should be no changing occurring to references once they are set up for a user's environment.

MS Access Reference to LATEST Excel/Word/etc

I haven't seen this exact question yet:
I have multiple installations of MS Office (don't ask, just need it!). Anyway, I've developed a database that will create and interact with Excel sheets and Word documents. Currently, when creating these files, I'm using something like Excel.14 or Word.14 to specify Office 2010 documents. We are upgrading to Office 2013, and though I will still have Office 2010 installed, I want VBA to point to Word.15 but I don't want to hard code it.
I don't want to edit the code each time we upgrade. Is there a way to ensure that these references will ALWAYS pick the latest version of installed software?
Thanks for the help!
...Scotty
You might be able to use late binding to achieve this. Instead of setting a Reference in the project and using...
Dim objExcel As New Excel.Application
Dim objWord As New Word.Application
...you could try omitting the project Reference and use
Dim objExcel As Object, objWord As Object
Set objExcel = CreateObject("Excel.Application")
Set objWord = CreateObject("Word.Application")

Opening Access from Excel VBA

Edit: The answer to this question can be found within the comments of the accepted answer.
I am attempting to open an Access database from a button click within my excel file. I currently have this code:
Private Sub bttnToAccess_Click()
Dim db As Access.Application
Set db = New Access.Application
db.Application.Visible = True
db.OpenCurrentDatabase "C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb"
End Sub
This seems to work briefly and then Access shuts down almost immediately. If it matters, the Access file has an AutoExec macro that runs through a few tests itself on open.
Don't try to open the Access application then; just create a connection object using one of the Data Access technologies:
- OLE-DB or
- ODBC.
Google "ODBC Connection strings" or "OLE-DB Connection Strings" to get details depending on your particular configuration (and Access filetype).
Probably ADODB is the easiest current library to use for your data access.
Update:
Try Importing the data from Access then using the Data -> From Access wizard. Yu can always use the Macro recoding facility to automatically generate some VBA code for you, that will create some infrastructure for you; I use this regularly when exploring new portions of the VBA object model.
Update - Final resolution of problem, from comments below
That may be because the variable goes out of scope; move the declaration of db outside the function, to module level
The code started Access by creating an application instance assigned to an object variable. At the end of the procedure, the variable went out of scope so Access shut down.
You accepted an answer to use a module-level variable for the Access application instance. In that case, Access remains running after the procedure ends. However if the user exits Excel, Access will close down too.
If the goal is to start Access and leave it running until the user decides to close it, just start Access directly without assigning the application instance to an object variable (Set db = New Access.Application). That db variable would be useful if your Excel code needed it for other purposes. However, it's actually only used to open the db file.
You can use the Run method of WScript.Shell to open your db file in an Access session.
Private Sub bttnToAccess_Click()
Const cstrDbFile As String = "C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb"
Dim objShell As Object
Set objShell = CreateObject("WScript.Shell")
objShell.Run cstrDbFile
Set objShell = Nothing
End Sub
I know this is an old thread, but you will get this error in Excel VBA if you are trying to open an Access database, but you do not have two specific References clicked. (Tools, References on the VBA Editor screen). You need to click 'Microsoft Access 15.0 Object Library' and 'Microsoft ActiveX Data Objects 6.1 Library'.
Remove the New declaration then it works
Actually it is pretty straightforward:
Private Sub bttnToAccess_Click()
db = DBEngine.OpenDatabase("C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb")
End Sub
For this to work you need to declare db as Database at the Module level.
Dim db As Database 'Requires reference to the Microsoft
'Access Database Engine Object Library

“User-defined type not defined” error in VB in Access 2007

I'm receiving a compile error on the following line of code:
Dim oXL As Excel.Application
The code is in VB in MS Access 2007. The line above is the beginning of a segment to generate an MS Excel file. The obvious answer to me was to ensure that the "Microsoft Office 12.0 Object Library" is checked under Tools > References. I've done this but the error persists. Does Excel need to be installed side-by-side for this to work? What have I done wrong? Thanks in advance.
You need to reference Microsoft Excel 12.0 Object Library or use late binding. Late binding is almost always necessary if you will be sharing your project with users who may have different versions of Excel installed.
For late binding, you would instead do:
Dim oXL as object
Set oXL = CreateObject("Excel.Application")
Then your code should work as expected, without the need to make the reference... assuming you aren't using any other Excel specific values or objects.

How to Solve DLL error in access

I am developing an Excel project in Access using VBA. My project is working in Access 2007, but it does not work in Access 2003. If I try to run the file, it shows the DLL error. Anyone know how to debug this error?
It's pretty difficult to know what's causing your problem without knowing what error message you are getting and where it's occurring.
My best guess says that you are have a problem with a reference. Usually the best way to fix this is to change your code to use late binding if the version of Excel will not always be the same on every machine.
Consider the following 3 examples:
'This is early binding.
Dim oXLS As Excel.Application
Set oXLS = New Excel.Application
'Create a new instance of an Excel Object using late binding
Dim oXLS As Object
Set oXLS = CreateObject("Excel.Application")
'Set your object to an instance of Excel that is already open using late binding
Dim oXLS As Object
Set oXLS = GetObject(, "Excel.Application")
It's easier to program using early binding because you can use intellisense. However, if your target machines might have differing versions of Excel you should consider using late binding in your production/deployment version for better compatibility.