[FONT=Courier New][SIZE=1]Option Explicit[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Public Sub CopyFromRecordset()[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1] Const SheetName As String = "[COLOR=red][B]Sheet1[/B][/COLOR]"
Dim dbData As DAO.Database
Dim rsData As DAO.Recordset
Dim iField As Integer
Dim iPtr As Integer
Set dbData = DAO.OpenDatabase("[COLOR=red][B]Database.mdb[/B][/COLOR]")
Set rsData = dbData.OpenRecordset("[B][COLOR=red]tblWhatever[/COLOR][/B]")[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1] For iField = 0 To rsData.Fields.Count - 1
Sheets(SheetName).Cells(1, iField + 1) = rsData.Fields(iField).Name
Next iField
Sheets(SheetName).Range("A2").CopyFromRecordset rsData
rsData.Close[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT]
[FONT=Courier New][SIZE=1]End Sub[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Set rsData = dbData.OpenRecordset("SELECT * FROM " & TableName & " WHERE Surname='Smith';")[/SIZE][/FONT]
[FONT=Courier New][SIZE=1] Debug.Print rsData.RecordCount & " records"[/SIZE][/FONT]
[FONT=Courier New][SIZE=1] rPtr = 1
Do Until rsData.EOF
rPtr = rPtr + 1
Debug.Print rsData!Surname [COLOR=green]' use ! to refer to the field by its name
[/COLOR] Debug.Print rsData.Fields(0) [COLOR=green]' use . to refer to the field by its position
[/COLOR] rsData.MoveNext
Loop
rsData.Close
[/SIZE][/FONT]
Option Explicit
Sub GetSQLQueryResults()
Dim iChannel1 As Integer
Dim iChannel2 As Integer
Dim sSQL As String
Dim vData As Variant
Dim rPtr As Long
Dim cPtr As Long
Sheets("Sheet1").Cells.ClearContents
Shell "MSAccess " & "c:\folder\database1.mdb", vbMinimizedNoFocus
iChannel1 = DDEInitiate("MSAccess", "System")
DDEExecute iChannel1, "[OpenDatabase database1]"
sSQL = "SELECT * FROM tblProducts " _
& "WHERE Colour='Silver' ORDER BY Price DESC;"
iChannel2 = DDEInitiate("MSAccess", "database1;SQL " & sSQL)
vData = DDERequest(iChannel2, "All")
DDETerminate iChannel2
DDEExecute iChannel1, "[CloseDatabase]"
DDEExecute iChannel1, "[Quit]"
DDETerminate iChannel1
If Not IsArray(vData) Then
MsgBox "Failed: Access returned " & CStr(vData) & "!" & Space(15), vbOKOnly + vbExclamation
DDETerminateAll
Exit Sub
End If
For rPtr = LBound(vData, 1) To UBound(vData, 1)
For cPtr = LBound(vData, 2) To UBound(vData, 2)
Sheets("Sheet1").Cells(rPtr, cPtr) = vData(rPtr, cPtr)
Next cPtr
Next rPtr
End Sub