Option Explicit
Private Sub Workbook_Open()
'==================================================
'Grabs material list from master materials index using ActiveX Data Objects
'Var Purpose
'a1 Connection object
'a2 Recordset object
'b1 SQL connection string
'b2 SQL recordset "Source" parameter
'b3 Source workbook path
'b4 Source workbook name
'==================================================
On Error GoTo Catch
Dim a1 As Connection
Dim a2 As Recordset
Dim b1, _
b2, _
b3, _
b4 As String
'==================================================
'Media
'==================================================
'Assigns inventory path and filename to "b3" and "b4"
b3 = "internal link"
b4 = "\Inventory Updater.xls"
'Assigns connection string to "b1"
b1 = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & b3 & b4 & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
'Assigns SQL select statement to "b2"
b2 = "SELECT * FROM [" & "Media$A:B" & "];"
'Sets connection and recordset object variables
Set a1 = CreateObject("ADODB.Connection")
Set a2 = CreateObject("ADODB.Recordset")
'Opens the connection and the recordset
a1.Open b1
a2.Open b2, b1
'Copies from the recordset
With ThisWorkbook.Sheets("Media")
.Cells.ClearContents
.Range("A1").CopyFromRecordset a2
End With
'Closes connection and recordset so that they can be reopened with a tweaked SQL statement
a1.Close
a2.Close
'==================================================
'Leads and employees
'==================================================
'Assigns SQL select statement to "b2"
b2 = "SELECT * FROM [" & "Names$A:D" & "];"
'Opens the connection and the recordset
a1.Open b1
a2.Open b2, b1
'Copies from the recordset
With ThisWorkbook.Sheets("Names")
.Cells.ClearContents
.Range("A1").CopyFromRecordset a2
End With
'Closes the connection and the recordset
a1.Close
a2.Close
'==================================================
'Error Handler
'==================================================
Catch:
On Error Resume Next
'Ensures connection and recordset are closed
a1.Close
a2.Close
'Cleanup
Set a1 = Nothing
Set a2 = Nothing
'If the event errors out, then a caption appears which reads "Lists outdated"
With ThisWorkbook.Sheets("Media Electronic")
Select Case Err.Number
Case Not 0, Not 3704
.Range("G43") = "Lists outdated"
Case Else
.Range("G43") = "Lists current"
End Select
End With
End Sub