1

我将以下代码粘贴到 ThisWorkbook 对象中。但是,每次我打开工作簿时它什么都不做,只要我进入 VBA 并手动单步执行它,它就可以工作并且组合框会被填充?我确定它是一个设置或一些简单的东西,而不是任何复杂的代码,但我完全不知道为什么会失败。

任何帮助将非常感激

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
4

1 回答 1

3

尝试改变这个:

Private Sub Open_Workbook()

对此:

Private Sub Workbook_Open()
于 2013-10-14T16:20:54.007 回答