create an DAO Access file in Excel VBA - excel

I am unable to create a file using following commands, could anyone help me in this regard. The error it says is invalid argument.
I got office 2010.
My ref is :MS office 14.0 ACCESS DATABASE OBJECT LIBRARY
Sub DBcreate()
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Set DB = DAO.Workspaces(0).CreateDatabase("D:\tblImport11.accdb", DB_LANG_GENERAL)
DB.Close
'Set RS = Nothing
Set DB = Nothing
End Sub
i also tried:
Set DB = DBEngine.CreateDatabase("D:\tblImport11.accdb", DB_LANG_GENERAL)
This also gives same error

The DB_LANG_GENERAL is the wrong parameter. Use:
Set DB = DBEngine.CreateDatabase("D:\tblImport11.accdb", dbLangGeneral)

Related

Word / Excel crashing when looping through Access database QueryDefs

I have a VBA procedure in Word that checks to see if a QueryDef exists in an Access database. It was working fine until a few days ago, when it started crashing Word.
The issue seems to be this line: For Each qdf In db.QueryDefs. This is the line on which Word hangs and then crashes. I don't think it's my code because I have created a very basic test procedure in a new Word document that is just a loop with nothing in it (see below) and created a new Access database and it still crashes.
A connection to the database is successfully made as I get the locked database file. And this code will successfully execute and print to immediate window: Debug.Print db.QueryDefs.Count
I also tested this basic procedure with the empty loop in Excel and it crashes there too. I also updated Office (I'm on Office 365, desktop version of Office). Is there something I'm missing?
Reference to: Microsoft Office 16.0 Access Database Engine Object Library
Option Explicit
Sub Test()
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Set db = OpenDatabase("DatabasePathAndFileName")
Debug.Print db.QueryDefs.Count
For Each qdf In db.QueryDefs
Next qdf
End Sub
Hit F9 as you step through the code and post back with the exact error. Also, have you restarted the machine recently? I have seen CTRL-Breaks build up over time, and cause all kinds of weirdness, but a restart usually fixes everything. Finally, what changed since it was working last??!!
Try this. What happens?
Public Sub ExecParameterQuery()
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("myActionQuery")
'Set the value of the QueryDef's parameter
qdf.Parameters("Organization").Value = "Microsoft"
'Execute the query
qdf.Execute dbFailOnError
'Clean up
qdf.Close
Set qdf = Nothing
Set dbs = Nothing
End Sub
Try stepping through the collection to see if it is a specific query that causes the problem:
Sub Test()
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Set db = OpenDatabase("DatabasePathAndFileName")
If db.QueryDefs.Count > 0 Then
For Each qdf In db.QueryDefs
MsgBox qdf.Name
Next qdf
End If
End Sub

How to send parameters to microsoft access query so that I can import an access parameter query to excel?

I need to import a microsoft access query that has popup input parameters into excel. I tried the code below but it does not work. I receive error 93 that tells me that object or object variable is not set.
I would like to be able to reference two cells in excel that contain the values of the current and previous month and then send these values as inputs to the access query, but for now I entered them in VBA to keep it simple.
Any help would be greatly appreciated!
Thank you!
Sub Acess_Connection()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim i As Long
Dim wsh As Worksheet
Set dbs = DBEngine.OpenDatabase("filepath")
Set qdf = dbs.QueryDefs("parameter_query")
qdf.Parameters("Date_PreviousMonth") = "31.12.2018"
qdf.Parameters("Date_CurrentMonth") = "31.01.2019"
Set rst = qdf.OpenRecordset("parameter_query")
Set wsh = Worksheets("Sheet1")
For i = 0 To rst.Fields.Count - 1
wsh.Cells(1, i + 1).Value = rst.Fields(i).Name
Next
wsh.Range("A1").Resize(ColumnSize:=rst.Fields.Count).Font.Bold = True
wsh.Range("A2").CopyFromRecordset rst
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
End Sub
I tested setting query parameters via VBA with a very simple query and it works with following adjustments.
query object must have PARAMETERS clause and parameters under appropriate field(s)
use # delimiters for date criteria #12/31/2018#
Set rst = qdf line does not use query name as argument, the variable qdf provides the name so correct to
Set rst = qdf.OpenRecordset() which will use the default recordset type.
for early binding, select Microsoft Office 14.0 Access Database Engine Object Library in VBA editor, at least for more recent versions of Excel
You may have to pass valid date values to the parameters:
qdf.Parameters("Date_PreviousMonth").Value = #12/31/2018#
qdf.Parameters("Date_CurrentMonth").Value = #01/31/2019#
Try adding square brackets around your parameters name so:
qdf.Parameters("[" & "Date_PreviousMonth" & "]") = "31.12.2018"

Variable Not Defined error in VBA for CurrentDb function for access DB

I am using currentDb method to get instance of database in VBA excel macro. I have added reference as well but CurrentDb method is giving error "Variable Not Defined"
Below is code
Dim Db As DAO.Database
Db = CurrentDb
Could you please help me on this
CurrentDb is an Access-specific global variable which is not defined in Excel.
You need to open a database instead:
Dim Db As DAO.Database
Set Db = OpenDatabase("C:\Users\Fred\Databases\Foo.mdb")
OpenDatabase is a member of the global object DBEngine from the Microsoft DAO 3.6 Object Library (which I assume you have referenced because your code has the DAO.Database type).

Trying to Run Access Macro from

I am trying to run an Access Macro from Excel. I set a reference to Microsoft Office 14.0 Access database engine Object Library. Now, I'm trying to run a small script like this.
Sub RunAccessMacro()
Dim strDatabasePath As String
Dim PathToDB As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
PathOfDatabase = ThisWorkbook.Worksheets("Updates").Range("PathToAccess")
Set db = DAO.DBEngine.OpenDatabase(PathOfDatabase)
Set qry = db.Execute("Macro_Run_Key")
qry.Close
db.Close
MsgBox "Done! All processes complete!!"
End Sub
The problem is, the db.Execute won't execute the Macro. I don't even see anything like RunMacro, or whatever it's called.
There must be a way to do this, right.
The database engine only does database things (anything with tables and queries). If you want more than that, you will have to use the Access application through VBA:
Sub RunAccessMacro()
Dim strDatabasePath As String
Dim PathToDB As String
Dim accApp As Access.Application
Set accApp = New Access.Application
PathOfDatabase = ThisWorkbook.Worksheets("Updates").Range("PathToAccess")
accApp.OpenCurrentDatabase PathOfDatabase
accApp.DoCmd.RunMacro "Macro_Run_Key"
accApp.Quit
Set accApp = Nothing
MsgBox "Done! All processes complete!!"
End Sub
Also, you will need to add a reference to the Microsoft Access Object Library, or you can adapt this code to use late bindings.
Setting a reference to the Access Object Library is useless unless you actually use it :) Seriously, the code written above uses DAO which is a different animal than calling Access directly through the Object Libary. DAO is strictly a database engine (like ADO) and does not know about macros and modules and such as defined in Office Apps.
Here is the code to use when using early binding:
Sub RunAccessMacro()
Dim PathOfDatabase As String
PathOfDatabase = ThisWorkbook.Worksheets("Updates").Range("PathToAccess")
Dim accApp As Access.Application
Set accApp = New Access.Application
With accApp
.OpenCurrentDatabase PathOfDatabase
.DoCmd.RunMacro "Macro_Run_Key"
.Quit
End With
MsgBox "Done! All processes complete!!"
End Sub
If the macro is an Access Macro, you can actually trigger the macro with a single command rather than having to go around the houses. The /X command line switch will help - check this link: How can I schedule a Macro to run automatically in Access 2007

Error `object required` while trying to run MS Access queries in Excel VBA

I'm trying to run Access DB queries in Excel, but facing problem.
Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Execute "DELETE FROM tblMyTable WHERE Bad", dbFailOnError
here it's getting
run time error 424 object required
exactly on the 2ndline
set dbs = CurrentDb
I already added reference DAO 3.6 object library. what to keep in place of CurrentDB. My MsAccess DB is in local disk.
CurrentDb isn't recognized by Excel, so it's being treated as an empty variant, since you haven't assigned it anything. Using Option Explicit will prevent this sort of problem in the future.
If your DB is already open, try something like: Set dbs = GetObject(,"Access.Application").CurrentDb
Option Explicit '### This goes at the VERY TOP of ALL MODULES
Sub foo()
Dim dbs As DAO.Database
Dim appAccess as Object '# Access.Application
Set appAccess = GetObject(,"Access.Application") '# Get a handle on Access
Set dbs = appAccess.CurrentDb
dbs.Execute "DELETE FROM tblMyTable WHERE Bad", dbFailOnError
End Sub

Resources