我在 Outlook 中有一些運行 SQL 查詢并填充我設計的用戶表單的 VBA 代碼。這段代碼以前在一臺 PC 上運行良好,然后我得到了一臺新筆記本電腦,我直接復制了 Outlook VBA 模塊。
當我嘗試運行時,我只是得到一個錯誤“編譯錯誤:找不到專案或庫”,并且 RS.open 陳述句中的文本“adOpenStatic”被突出顯示。以前,如果我的 SQL 查詢錯誤或驅動程式丟失,我會看到相同的錯誤,但我仔細檢查了兩者。我將 EXACT 代碼復制到一個 excel VBA 模塊中,它按預期運行。
我能看到的唯一其他區別是我從 Outlook 2019 轉到了 Office 365 for Enterprise。
Dim server, database, login_user, password, port As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
server = "dummy"
database = "dummy"
login_user = "dummy"
password = "dummy"
port = "dummy"
Set cn = New ADODB.Connection
cn.ConnectionString = "DRIVER={MySQL ODBC 8.0 ANSI Driver};Provider=MSDASQL;" & "SERVER=" & server & ";" & " DATABASE=" & database & ";" & "UID=" & login_user & ";PWD=" & password & "; OPTION=3; PORT=" & port & ";Connect Timeout=20;"
'open the connection
cn.Open
Set rs = New ADODB.Recordset
strSQL = "SELECT * FROM quote WHERE id = 1505"
rs.Open strSQL, cn, adOpenStatic
With rs
Do Until .EOF
'parse out relevant project information
rs.MoveNext
Loop
End With
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
I can run the exact query in MySQL workbench, and it works as expected. The fact that the identical code can run in Excel without issue tells me there is perhaps a syntax difference in Outlook that I am unaware of? Anyone else have ideas?
NOTE, if it wasn't already obvious, I am omitting my database details with "Dummy", but the real code has the correct strings.
uj5u.com熱心網友回復:
This code was previously running fine on one PC, then I was issued a new laptop and I copied the outlook VBA module directly over.
You need to re-add all COM references you had on the old machine.
From the Tools menu, choose References to display the References dialog box.
The References dialog box shows all object libraries registered with the operating system. Scroll through the list for the application whose object library you want to reference. If the application isn't listed, you can use the Browse button to search for object libraries (*.olb and .tlb) or executable files (.exe and *.dll on Windows). References whose check boxes are selected are used by your project; those that aren't selected are not used, but can be added.
uj5u.com熱心網友回復:
Go to the VBA Editor, and choose Tools-References; and tick the box next to 'Microsoft ActiveX Data Objects' (the latest version). It should compile ok now.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/451954.html
標籤:mysql sql excel vba outlook
上一篇:將列拆分為陣列丟失前導零
