我试图将文本文件的内容加载到变量中,但我得到了名义上的错误,搜索 VBA 知识库让我相信
Set mySQL = My.Computer.FileSystem.ReadAllText("C:\sql_query_temp.res")
会解决它,但是当我运行时只会产生“错误:需要对象”。这是我的代码,我错过了什么?
'Requires Microsoft ActiveX Data Objects x.x library in references
Public Sub ConnectToOdbc()
Dim myconn As New ADODB.Connection
Dim myrs As New Recordset
Dim mySQL As String
Dim myrows As Long
'Open file containing SQL query
mySQL = My.Computer.FileSystem.ReadAllText("C:\sql_query_temp.res") <----- bad!
'Open Connection
myconn.Open "DSN=database"
'Do Query
myrs.Source = mySQL
Set myrs.ActiveConnection = myconn
myrs.CursorLocation = adUseClient
myrs.Open
'Count Rows
myrows = myrs.RecordCount
'Add text to word document!
Selection.TypeText (myrows)
'Close Connection
myrs.Close
myconn.Close
End Sub