hi Dave,
i opened up the Comptoir.mdb file and i opened the table "Clients" for editing.
and on excel i have executed the macro "test" with no problem.
i also did a test when Comptoir.mdb have being closed, and there is no problem.
Sub test()
DAOCopyFromRecordSet "C:\Program Files\Microsoft Office\Office10\Samples\Comptoir.mdb", _
"Clients", "Fonction", " = 'Propriétaire'", Range("A1")
End Sub
Sub DAOCopyFromRecordSet(DBFullName As String, TableName As String, _
FieldName As String, Criteria As String, TargetRange As Range)
Dim db As Database, rs As Recordset
Dim intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
Set db = OpenDatabase(DBFullName)
' Set rs = db.OpenRecordset(TableName, dbOpenTable) ' all records
Set rs = db.OpenRecordset("SELECT * FROM " & TableName & _
" WHERE " & FieldName & Criteria, dbReadOnly) ' filterrecords
' 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 Sub
Tried your example (full path is same as what works now regardless of
tbl being open, just not shown here)
Sub test()
DAOCopyFromRecordSet "\\...dbname.mdb", _
"tblBalance", "*", " = '*'", Range("A2")
End Sub
Sub DAOCopyFromRecordSet(DBFullName As String, TableName As String, _
FieldName As String, Criteria As String, TargetRange As Range)
Dim db As Database, rs As Recordset
Dim intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
Set db = OpenDatabase(DBFullName)
* Set rs = db.OpenRecordset(TableName, dbOpenTable) ' all records
'Set rs = db.OpenRecordset("SELECT * FROM " & TableName & _
" WHERE " & FieldName & Criteria, 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 Sub
I get an error (3219) "Application-defined or object-defined" message
at the line I marked with *. I noticed you do not dim the db or rs
specifically as DAO. I tried specific references and got the same
error. I do have a reference to DAO 3.6 and VBA 5.3. So why would this
work for you and not me?