Van T. Dinh MVP (Access) wrote ...
The mdb contains lots of objects in a format that only be
decoded by the right versions of Access. Without Access
software to decode, the mdb file is just a big heap of
bits.
Are you sure about that, did you test? Do the phrases 'Jet' and 'MDAC'
mean anything to you? Just in case you think I'm just being
argumentative:
Standard answer number 2: You don't need the MS Access application to
create and/or query a Jet database (.mdb file). You can do all this on
the fly using only Excel and ADOX (to create the .mdb file; also
create linked tables) and ADO (use DDL statements to create the schema
e.g. CREATE TABLE etc and SQL for queries).
You do need MDAC, free MS download and shipped with Excel, and Jet,
free MS download and shipped with early versions of MDAC, so it's
highly likely you already have the necessary components shipped with
Excel.
See the following links (in these articles, read 'Jet' for the word
'Access'
because the MS Access application is not actually used):
Creating an Access(sic) Database:
http://msdn.microsoft.com/library/d...s/odeopg/html/deovrcreatingaccessdatabase.asp
Running a Temporary Query:
http://msdn.microsoft.com/library/d...s/odeopg/html/deovrcreatingaccessdatabase.asp
Here's some Excel code (run from inside a new blank workbook) which
creates a .mdb, some tables and some sample data, queries the tables
with a join and copies the data to the workbook. You don't need to
uninstall MS Access (unless you remain unconvinced):
Option Explicit
Sub Test()
Dim Cat As Object
Dim Con As Object
Dim rs As Object
Dim strConJet As String
Dim strSql1 As String
Dim strSql2 As String
Dim strSql3 As String
Dim oTarget As Excel.Range
Dim lngCounter As Long
' Amend the following constants to suit
Const FILENAME_JET As String = "" & _
"New_Jet_DB2.mdb"
Const CONN_STRING_JET As String = "" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=<PATH><FILENAME>"
' Build connection string
strConJet = CONN_STRING_JET
strConJet = Replace(strConJet, "<PATH>", _
ThisWorkbook.PATH & Excel.Application.PathSeparator)
strConJet = Replace(strConJet, "<FILENAME>", FILENAME_JET)
' Build sql statements
strSql1 = ""
strSql1 = strSql1 & "CREATE TABLE CommsUsers ("
strSql1 = strSql1 & " ID INTEGER NOT NULL PRIMARY KEY,"
strSql1 = strSql1 & " lname VARCHAR(35) NOT NULL,"
strSql1 = strSql1 & " fname VARCHAR(35) NOT NULL,"
strSql1 = strSql1 & " mname VARCHAR(35) NOT NULL"
strSql1 = strSql1 & " DEFAULT '{{NA}}'"
strSql1 = strSql1 & ");"
strSql2 = ""
strSql2 = strSql2 & "CREATE TABLE Phones ("
strSql2 = strSql2 & " ID INTEGER NOT NULL"
strSql2 = strSql2 & " REFERENCES CommsUsers (ID),"
strSql2 = strSql2 & " Phone VARCHAR(15) NOT NULL"
strSql2 = strSql2 & ");"
strSql3 = ""
strSql3 = strSql3 & "SELECT cm.ID, cm.lname,"
strSql3 = strSql3 & " cm.fname, ph.Phone"
strSql3 = strSql3 & " FROM CommsUsers cm"
strSql3 = strSql3 & " LEFT JOIN Phones ph"
strSql3 = strSql3 & " ON cm.ID=ph.ID;"
' Create new Jet database
Set Cat = CreateObject("ADOX.Catalog")
Cat.CREATE strConJet
' 'inherit' the connection
Set Con = Cat.ActiveConnection
Set Cat = Nothing
With Con
' Create tables
.Execute strSql1
.Execute strSql2
' Create some sample data
.Execute "INSERT INTO CommsUsers (ID, lname, fname)" & _
" VALUES (1, 'Livehulas', 'A')"
.Execute "INSERT INTO CommsUsers (ID, lname, fname)" & _
" VALUES (2, 'Katewudes', 'B')"
.Execute "INSERT INTO CommsUsers (ID, lname, fname)" & _
" VALUES (3, 'Hevitoxic', 'C')"
.Execute "INSERT INTO CommsUsers (ID, lname, fname)" & _
" VALUES (4, 'Norarules', 'D')"
.Execute "INSERT INTO Phones (ID, Phone)" & _
" VALUES (1, '123')"
.Execute "INSERT INTO Phones (ID, Phone)" & _
" VALUES (1, '456')"
.Execute "INSERT INTO Phones (ID, Phone)" & _
" VALUES (2, '555')"
.Execute "INSERT INTO Phones (ID, Phone)" & _
" VALUES (2, '444')"
.Execute "INSERT INTO Phones (ID, Phone)" & _
" VALUES (4, '789')"
End With
' Open recordset
Set rs = Con.Execute(strSql3)
' Copy data to ThisWorkbook
Set oTarget = ThisWorkbook.Worksheets(1) _
.Range("A1")
With rs
For lngCounter = 1 To .fields.Count
oTarget(1, lngCounter).Value = _
.fields(lngCounter - 1).Name
Next
End With
oTarget(2, 1).CopyFromRecordset rs
Con.Close
End Sub
--