Hi All-
I would appreciate any help on this. The attached code worked initially but it did not pull all records from the recordset, rs; only 180 of the 250 records was output to the excel sheet. Currently, when I run the macro, all it does is output only the very first field name and nothing else. I didn't change anything since I ran it last time. I get this error too: "Data and table creation error"
Is there a better way to output all records of the recordset, rs?
Sub RawLotInput()
Dim dbs As Database
Dim rs As Recordset
Dim Ws As Worksheet
Dim Path As String
Dim strSQL As String
On Error GoTo ErrorHandler
ThisWorkbook.Activate
'' - - - - - - - - - - - - - - - - - - - - - - - - - - - -
''Open the database
Path = "C:\Documents and Settings\ofomaiu\Desktop\PP database.mdb"
Set dbs = OpenDatabase(Path)
' SQL statement- Change Query parameters here
strSQL = "SELECT [Access Compatible].*" & _
" FROM [Access Compatible]" & _
" WHERE ((([Access Compatible].[Firing Lot No#])=80554) AND (([Access Compatible].[Run No#])='21'));"
Set rs = dbs.OpenRecordset(strSQL)
Set Ws = ActiveSheet
'Clear cells first
Cells.Select
Selection.ClearContents
For i = 0 To rs.Fields.Count - 1
Ws.Cells(1, i + 1) = rs.Fields(i).Name
'Export data from the recordset to a worksheet (Sheet1).
Ws.Range("A2").CopyFromRecordset rs
Next
'Auto-fit columns
Sheets("Raw Data").Select
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Columns.AutoFit
Range("A1").Select
'' - - - - - - - - - - - - - - - - - - - - - - - - - - - -
lbTidy:
dbs.Close
Set dbs = Nothing
Set rs = Nothing
Exit Sub
'' - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ErrorHandler:
vtMessage = "Table and data creation error"
vtMessage = vtMessage & _
Chr(10) & _
Chr(10) & "Error Number: " & Err & _
Chr(10) & "Error Description: " & Error()
MsgBox vtMessage, strSQL, vbInformation, ctByg
Resume lbTidy
End Sub