Re: Importing an excel spreadsheet into and existing Access
here is the code I am using in Excel. This applies only to current week information. I will need to add additional code that will retrieve the information based upon the week date entered, if there has been a period close, then I need to pull that information into the PTD, QTD & YTD sheets. This is what I am trying to do now. If the current date is after the period close, I will need to select the PD close numbers as well as the intermitten weeks to accomodate QTD & YTD which is why I need every week.
Option Explicit
Sub FetchAllData(WeekNum As String)
FetchData "qrytransactions03", WeekNum, 20
FetchData "qrysdxlaborhrs03", WeekNum, 26
FetchData "qryVendorHrs03", WeekNum, 31
FetchData "qrySalesRevenues03", WeekNum, 57
FetchData "qryProductCost03", WeekNum, 62
FetchData "qryTotLaborCost03", WeekNum, 80
FetchData "qryControllables03", WeekNum, 85
FetchData "qryNonControl03", WeekNum, 90
FetchData "qryBgtTransactions04, weeknum, 16"
FetchData "qryBgtSdxLaborHrs04", WeekNum, 24
FetchData "qryBgtVendorHrs04", WeekNum, 29
FetchData "qryBgtSalesRevenues04", WeekNum, 55
FetchData "qryBgtProductCost04", WeekNum, 60
FetchData "qryBgtTotLaborCost04", WeekNum, 78
FetchData "qryBgtControllables04", WeekNum, 83
FetchData "qryBgtNonControl04", WeekNum, 87
End Sub
Sub FetchData(QueryName As String, WeekNum As String, RowNum As Integer)
Dim objAccess As Object
Dim objDB As Object
Dim rst As Object
Dim strSQL As String
Dim colnum As Integer
Dim foundit As Boolean
Sheets("Current Week").Select
'Opens Access
Set objAccess = CreateObject("access.application")
With objAccess
.opencurrentdatabase (ActiveWorkbook.Path & "\Flash FY05.mdb")
BeginLoop:
Set objDB = objAccess.currentdb()
With objDB
strSQL = "SELECT " & QueryName & ".UNIT, " & QueryName & ".[" & WeekNum & "] from " & QueryName & ""
Set rst = objDB.openrecordset(strSQL)
If Not rst.RecordCount = 0 Then
rst.movefirst
Sheets("current week").Select
Do
colnum = 1
foundit = False
' For Each fld In rst.fields
Do
If Cells(12, colnum).Value <> rst.fields("UNIT").Value Then
colnum = colnum + 1
Else
foundit = True
End If
Loop Until colnum > 223 Or foundit
If Cells(12, colnum).Value = rst.fields("UNIT").Value Then
Cells(RowNum, colnum).Value = rst.fields(WeekNum).Value
End If
colnum = colnum + 1
' Next fld
rst.movenext
Loop Until rst.EOF
End If
End With
End With
rst.Close
objAccess.Application.Quit
End Sub
I know it sounds crazy but there is a method to the madness