Opentextfile leads to error 5

Kingz

Member
Local time
Tomorrow, 00:52
Joined
Mar 19, 2024
Messages
63
Hi guys,

I'm trying to access a file which seemingly exists, but gives me an error 5.

Here is my code:

`Strfilepath="...\query.txt"
Set DBs = currentdb()

Set fso = CreateObject("Scripting.FilesystemObject")
If fso.FileExists(strfilepath) then
Set file = fso.opentextfile(strfilepath,ForReading, false)
End if
StrSqlquery= file.readall
File.close

It's the "opentextfile" line that gives me the error.
Thanks in advance.
 
And error 5 is? :(
Strfilepath appears to be commented out?
Have looked at your variables?
Not even sure that would be a valid path anyway?
 
Code:
ForReading
If you use late binding, constants from the object used are unknown.
So you would have to insert the real value or declare the constant explicitly.
Code:
Const ForReading = 1
 
see post #2, Error 5 is Access Denied (pertains to file).
 
Code:
ForReading
If you use late binding, constants from the object used are unknown.
So you would have to insert the real value or declare the constant explicitly.
Code:
Const ForReading = 1
Thanks dude.. That did it!
 
Make sure you have Option Explicit declared at the top of every code module (above or below Option Compare Database).

This will catch any errors for undefined variables/constants or typos in your code.
 
or you can set it once in VBA (tools->options)
2024-04-10_18-12-05.jpg
 
or you can set it once in VBA (tools->options)
Do this. (y) Option Explicit will then be added automatically to all new code modules

But you will also need to add it manually to all existing code modules.
 

Users who are viewing this thread

Back
Top Bottom