I have the below code pasted into the ThisWorkbook object. However everytime I open the workbook it does nothing, as soon as I go into VBA and manually step through it, it works and the combobox get populated? I’m sure its a setting or something simple rather than anything complicated with the code but I’m all out of ideas as to why this is failing.
Any help would be much appreciated
Option Explicit
Public bClear As Boolean
Private Sub Open_Workbook()
With Application
.Calculation = xlManual
.EnableEvents = False
.ScreenUpdating = False
End With
Dim stSQL As String, rst As ADODB.Recordset, k As Integer, vaData As Variant
Dim objConn As ADODB.Connection
Dim ConnectionString As String
ConnectionString = "Provider=sqloledb.1;data source=SQL-SERVER;Initial catalog=SQL-DB;Integrated Security = SSPI;"
Set objConn = New ADODB.Connection
stSQL = "EXEC('SELECT NAME FROM TABLE1')"
With objConn
.CursorLocation = adUseClient
.Open ConnectionString
Set rst = .Execute(stSQL)
End With
With rst
Set .ActiveConnection = Nothing
k = .Fields.Count
vaData = .GetRows
End With
Sheets("Menu").CB_Book.List = Application.Transpose(vaData)
objConn.Close
Set rst = Nothing
Set objConn = Nothing
bClear = True
With Application
.Calculation = xlAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Try changing this:
Private Sub Open_Workbook()
To this:
Private Sub Workbook_Open()
Tags: sqlsql