Sub RawLotInput()
Dim dbs As Database
Dim rs As Recordset
Dim Ws As Worksheet
Dim wb As Workbook
Dim Path As String
Dim strSQL As String
On Error GoTo ErrorHandler
ThisWorkbook.Activate
'' - - - - - - - - - - - - - - - - - - - - - - - - - - - -
''Open the database
Path = "P:\03_Construction\4.0 CONSTRUCTION AUTOMATION\4.1 Applications\QMD - PORT\QMD_prog_1.9.12.mdb"
Set dbs = workspaces(0).OpenDatabase(Path)
' SQL statement- Change Query parameters here
strSQL = "SELECT forRepStep2.ID AS [System Number], forRepStep2.CP AS Contract,forRepStep2.GivenID AS [QAN Number]," & _
"forRepStep2.MStatusID, forRepStep2.MonitoringType,IIf(forRepStep2.MonitoringType=""Surveillance"",""Not applicable"",forRepStep2.IRFNumBound)" & _
"AS [IRF Number], forRepStep2.DateTimeAct AS [Date Raised], forRepStep2.DateTimePlan AS" & _
"[Planned Closure Date], forRepStep2.ClosedDateTime, [Sched_ITP.ShortNum] & "" : """ & _
"& [Sched_ITP.Object] AS [ITP Number], forRepStep2.Desc AS Description, forRepStep2.StatusDesc" & _
" AS [Non Conformance Details], IRF.Loc AS Location, IRF.ClosingComment AS [Actione Taken]," & _
"forRepStep2.IsClosed AS Closed, ContractInfo.ContractDescription, ContractInfo.Contractor," & _
"ContractInfo.ResidentEngr, forRepStep2.StatusID2, DateDiff(""d"",forRepStep2.DateTimeAct,Date())" & _
"AS DaysOpen, forRepStep2.StatusID1, forRepStep2.StatusID3, IRF.EnteredBy, IRF.ByWhomID, IRF.ClosedBy " & _
"FROM ((forRepStep2 INNER JOIN IRF ON forRepStep2.ID = IRF.ID) INNER JOIN ContractInfo ON " & _
"forRepStep2.CP = ContractInfo.ContractPackage) INNER JOIN Sched_ITP ON forRepStep2.ITPNumShort" & _
"= Sched_ITP.ITPNum GROUP BY forRepStep2.ID, forRepStep2.CP, forRepStep2.GivenID," & _
"forRepStep2.MStatusID, forRepStep2.MonitoringType, IIf(forRepStep2.MonitoringType=""Surveillance""," & _
"""Not applicable"",forRepStep2.IRFNumBound), forRepStep2.DateTimeAct, forRepStep2.DateTimePlan," & _
"forRepStep2.ClosedDateTime, [Sched_ITP.ShortNum] & "" : "" & [Sched_ITP.Object], forRepStep2.Desc," & _
"forRepStep2.StatusDesc, IRF.Loc, IRF.ClosingComment, forRepStep2.IsClosed, ContractInfo.ContractDescription," & _
"ContractInfo.Contractor, ContractInfo.ResidentEngr, forRepStep2.StatusID2," & _
"DateDiff(""d"",forRepStep2.DateTimeAct,Date()), forRepStep2.StatusID1, forRepStep2.StatusID3," & _
"IRF.EnteredBy, IRF.ByWhomID, IRF.ClosedBy HAVING (((forRepStep2.StatusID1) = 2)) ORDER BY forRepStep2.CP," & _
"forRepStep2.MonitoringType;"
On Error Resume Next
Set rs = dbs.OpenRecordset(strSQL)
Set wb = ThisWorkbook
wb.Worksheets("Raw Data").Delete
'dont use set ws = activesheet as when you have a few
'copies of excel open at the same time weird things can happen
Set Ws = wb.Worksheets.Add
Ws.Name = "Raw Data"
'Clear cells first
'Cells.Select
'Selection.ClearContents
'you dont need this if you delete the sheet
'I would recommend deleting the sheet rather than clearing cells
'removed copyfromrecordset from your loop
For i = 0 To rs.Fields.Count - 1
Ws.Cells(1, i + 1) = rs.Fields(i).Name
Next
'Export data from the recordset to a worksheet (Sheet1).
Ws.Range("A2").CopyFromRecordset rs
'Auto-fit columns
'Select statements are usually unnecessary and where
'you have select followed selection you can combine this
Ws.Range("A1").CurrentRegion.Columns.AutoFit
'' - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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