Opentextfile leads to error 5 (1 Viewer)

Kingz

Member
Local time
Today, 19:40
Joined
Mar 19, 2024
Messages
37
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:40
Joined
Sep 21, 2011
Messages
14,326
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?
 

ebs17

Well-known member
Local time
Today, 19:40
Joined
Feb 7, 2020
Messages
1,949
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:40
Joined
May 7, 2009
Messages
19,248
see post #2, Error 5 is Access Denied (pertains to file).
 

Kingz

Member
Local time
Today, 19:40
Joined
Mar 19, 2024
Messages
37
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!
 

cheekybuddha

AWF VIP
Local time
Today, 18:40
Joined
Jul 21, 2014
Messages
2,280
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.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:40
Joined
May 7, 2009
Messages
19,248
or you can set it once in VBA (tools->options)
2024-04-10_18-12-05.jpg
 

cheekybuddha

AWF VIP
Local time
Today, 18:40
Joined
Jul 21, 2014
Messages
2,280
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

Top Bottom