Code I'm using is....
Expand|Select|Wrap|Line Numbers
- Private Sub Command4_Click()
- Dim lngColumn As Long
- Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
- Dim dbs As DAO.Database
- Dim rst As DAO.Recordset
- Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
- blnEXCEL = False
- blnHeaderRow = True
- On Error Resume Next
- Set xlx = GetObject(, "Excel.Application")
- If Err.Number <> 0 Then
- Set xlx = CreateObject("Excel.Application")
- blnEXCEL = True
- End If
- Err.Clear
- On Error GoTo 0
- xlx.Visible = True
- Set xlw = xlx.Workbooks.Open("V:\Ops Timing Collection\OO Database\Risk Registers\Solutions Delivery\RR Examplev2.xlsx")
- Set xls = xlw.Worksheets("Risks")
- Set xlc = xls.Range("A1") ' this is the first cell into which data go
- Set dbs = CurrentDb()
- Set rst = dbs.OpenRecordset("q_RiskRegisterOutputRisks", dbOpenDynaset, dbReadOnly)
- If rst.EOF = False And rst.BOF = False Then
- rst.MoveFirst
- If blnHeaderRow = True Then
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
- Next lngColumn
- Set xlc = xlc.Offset(1, 0)
- End If
- Do While rst.EOF = False
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value
- Next lngColumn
- rst.MoveNext
- Set xlc = xlc.Offset(1, 0)
- Loop
- End If
- Set xls = xlw.Worksheets("Controls")
- Set xlc = xls.Range("A1") ' this is the first cell into which data go
- Set dbs = CurrentDb()
- Set rst = dbs.OpenRecordset("q_RiskRegisterOutputControls", dbOpenDynaset, dbReadOnly)
- If rst.EOF = False And rst.BOF = False Then
- rst.MoveFirst
- If blnHeaderRow = True Then
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
- Next lngColumn
- Set xlc = xlc.Offset(1, 0)
- End If
- Do While rst.EOF = False
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value
- Next lngColumn
- rst.MoveNext
- Set xlc = xlc.Offset(1, 0)
- Loop
- End If
- Set xls = xlw.Worksheets("Actions")
- Set xlc = xls.Range("A1") ' this is the first cell into which data go
- Set dbs = CurrentDb()
- Set rst = dbs.OpenRecordset("q_RiskRegisterOutputActions", dbOpenDynaset, dbReadOnly)
- If rst.EOF = False And rst.BOF = False Then
- rst.MoveFirst
- If blnHeaderRow = True Then
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
- Next lngColumn
- Set xlc = xlc.Offset(1, 0)
- End If
- Do While rst.EOF = False
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value
- Next lngColumn
- rst.MoveNext
- Set xlc = xlc.Offset(1, 0)
- Loop
- End If
- Set xls = xlw.Worksheets("Action Updates")
- Set xlc = xls.Range("A1") ' this is the first cell into which data go
- Set dbs = CurrentDb()
- Set rst = dbs.OpenRecordset("q_RiskRegisterOutputActionUpdates", dbOpenDynaset, dbReadOnly)
- If rst.EOF = False And rst.BOF = False Then
- rst.MoveFirst
- If blnHeaderRow = True Then
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
- Next lngColumn
- Set xlc = xlc.Offset(1, 0)
- End If
- Do While rst.EOF = False
- For lngColumn = 0 To rst.Fields.Count - 1
- xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Value
- Next lngColumn
- rst.MoveNext
- Set xlc = xlc.Offset(1, 0)
- Loop
- End If
- rst.Close
- Set rst = Nothing
- dbs.Close
- Set dbs = Nothing
- Set xlc = Nothing
- Set xls = Nothing
- xlw.Close True ' close the EXCEL file and save the new data
- Set xlw = Nothing
- If blnEXCEL = True Then xlx.Quit
- Set xlx = Nothing
- End Sub
Craig