【发布时间】:2015-11-04 23:27:20
【问题描述】:
我想使用 Outlook 宏连接到 MS SQL Server 数据库。但我不知道是代码错误还是我需要添加库/驱动程序或这里发生了什么但它不起作用。
Private Sub Application_Startup()
On Error GoTo ExitHere
'adodb connection to other database
stg_cn.Open "Provider = SQLOLEDB;" & _
"Data Source = 192.168.100.100;" & _
"Initial Catalog = hugeDB;" & _
"Integrated Security=SSPI;" & _
"User ID = oneuser;" & _
"Password = onepassword;"
sQuery = "SELECT * FROM documents where location = 'IE'"
'set reference to query
Set cmd = New ADODB.Command
cmd.ActiveConnection = stg_cn
cmd.CommandType = adCmdText
cmd.CommandText = sQuery
Set rs = cmd.Execute
Do While Not rs.EOF
For i = 0 To rs.Fields.count - 1
MsgBox (i + 1)
Next
rs.MoveNext
Loop
ExitHere:
If Not stg_cn Is Nothing Then stg_cn.Close
Set rs = Nothing
Set stg_cn = Nothing
Exit Sub
End Sub
【问题讨论】:
-
错误/问题是什么?你没有给我们太多的继续。
-
您说的是 Outlook 宏,但代码以
Workbook_Open开头,即 Excel。如果该过程在 Outlook 中,是否应将其称为Application_Startup? -
我改了,还是不行……难道是我连接数据库的方式应该不一样?
标签: sql sql-server vba outlook