Reputation: 330
Im trying to load the contents of a textfile into a variable but I get the titular error, searching the VBA knowledgebase led me to belive that
Set mySQL = My.Computer.FileSystem.ReadAllText("C:\sql_query_temp.res")
Would solve it but that just produces 'Error : Object Required' when I run. Heres my code, what have I missed?
'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
Upvotes: 0
Views: 3134
Reputation: 223
I think this might work.
It will allow you to search any given directory rather than fixating on just the one. This directory is then placed onto the variable 'inFileName'
inFileName = Application.GetOpenFilename("Text & r01 Files(*.*),*.*", , "Open Neutral File", "OPEN)
Hope this helps.
Upvotes: 0
Reputation: 175826
My.Computer
is VB.NET which is entirely different from VBA
instead you can;
Function readFile(path As String) As String
Dim hF As Integer
hF = FreeFile()
Open path For Input As #hF
readFile = Input$(LOF(hF), #hF)
Close #hF
End Function
...
mySQL = readFile("C:\sql_query_temp.res")
Upvotes: 3