access to excel

P

probitas

i'm trying to view some data from access in excel. i'm using code found
on this site written in excel as shown below but excel seems to fail to
recognise the datatype "Database". Any ideas?

Sub DAOCopyFromRecordSet() '"C:\db1.mdb" As _ String,
"dbt_Pruefbericht" As String, "dbf_MessDaten" As _ String, Range("A1")
As Range)
' Example: DAOCopyFromRecordSet "C:\FolderName\db1.mdb", "TableName",
"FieldName", Range("C1")

Dim db As Database
Dim rs As Recordset
Dim intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
Set db = OpenDatabase("C:\db1.mdb")
Set rs = db.OpenRecordset("dbt_Pruefbericht", dbOpenTable) _ ' all
records
'Set rs = db.OpenRecordset("SELECT * FROM " & TableName _ & " WHERE
" & FieldName & " = 'MyCriteria'", dbReadOnly) ' filter _ records
' write field names
For intColIndex = 0 To rs.Fields.Count - 1
TargetRange.Offset(0, intColIndex).Value =
rs.Fields(intColIndex).Name
Next
' write recordset
TargetRange.Offset(1, 0).CopyFromRecordset rs
Set rs = Nothing
db.Close
Set db = Nothing
End SubDatabase
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top