Basically, I'm trying to find a (good) way of getting data from Access 2007 (3 tables, a total of 209 Columns) into Excel 2010 & have started by doing it from Excel. In the course of doing some other stuff I had used some code - that I got from somewhere - to create an Access file.
and it created a file:-
If I use that file to get data back to Excel using:-
it works just fine. But if I change the database to the one I created in Access 2007,
I get the above error.
I found a post that recommended installing Microsoft Office 12.0 Object Library. That wasn't in the list under Tools|References so I downloaded it from Microsoft.com but it fails to install.
I was going to get the data back one Table at a time & just exclude the Primary Key field ("Serial Number") from passes 2 & 3. I want to give my users the ability to download this data from Excel but I'm completely stuck. Any suggestions will be gratefully received.
Many thanks,
Des.
Code:
Sub NewDatabase()
Dim wspDefault As Workspace, dbs As Database
Dim tdf As TableDef, fld1 As Field, fld2 As Field
Dim idx As Index, fldIndex As Field
Set wspDefault = DBEngine.Workspaces(0)
' Create new, encrypted database.
Set dbs = wspDefault.CreateDatabase("Newdb.mdb", _
dbLangGeneral, dbEncrypt)
' Create new table with two fields.
Set tdf = dbs.CreateTableDef("Contacts")
Set fld1 = tdf.CreateField("ContactID", dbLong)
fld1.Attributes = fld1.Attributes + dbAutoIncrField
Set fld2 = tdf.CreateField("ContactName", dbText, 50)
' Append fields.
tdf.Fields.Append fld1
tdf.Fields.Append fld2
' Create primary key index.
Set idx = tdf.CreateIndex("PrimaryKey")
Set fldIndex = idx.CreateField("ContactID", dbLong)
' Append index fields.
idx.Fields.Append fldIndex
' Set Primary property.
idx.Primary = True
' Append index.
tdf.Indexes.Append idx
' Append TableDef object.
dbs.TableDefs.Append tdf
dbs.TableDefs.Refresh
Set dbs = Nothing
End Sub
and it created a file:-
Code:
"C:\Documents and Settings\des.lavender\My Documents\Newdb.mdb"
If I use that file to get data back to Excel using:-
Code:
Sub MYThursday()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim myRange As Range
Set ws = ActiveSheet
Set myRange = ws.Range("A2")
Set db = OpenDatabase("C:\Documents and Settings\des.lavender\My Documents\Newdb.mdb")
' open the database
Set rs = db.OpenRecordset("Contacts", dbOpenTable)
' get all records in a table
'Check for results
If (rs.EOF And rs.BOF) Then
Debug.Print "There is no data"
Else
'Write to value Excel Sheet
Call myRange.CopyFromRecordset(rs)
End If
'Clean up objects
If Not rs Is Nothing Then Set rs = Nothing
End Sub
it works just fine. But if I change the database to the one I created in Access 2007,
Code:
Set db = OpenDatabase("C:\Documents and Settings\des.lavender\Access\LBLTest1.accdb")
I found a post that recommended installing Microsoft Office 12.0 Object Library. That wasn't in the list under Tools|References so I downloaded it from Microsoft.com but it fails to install.
I was going to get the data back one Table at a time & just exclude the Primary Key field ("Serial Number") from passes 2 & 3. I want to give my users the ability to download this data from Excel but I'm completely stuck. Any suggestions will be gratefully received.
Many thanks,
Des.