Troubles with "OPEN FILE_NAME FOR APPEND AS #1"

rbaron

New Member
Joined
Nov 18, 2016
Messages
6
Hi all.

Totally new to this forum, as I have a perplexing problem that Google just isn't solving for me.

At work, we have upgraded to some new version of MS Office, using VBA 7.1
I am trying to append text to a text file, qwhich woirks fine as long as I don't specify any file location.

As soon as I specify a file location such as "C:/", I get runtime errors.My current code works, and saves the file automatically in the "C:\Users\baror1\Documents" (where baror1 is my userID when I log into Windows):

Private Sub Command427_Click()
Open "7705-LOG.txt" For Append As #1
Print #1, Me.CircuitID
Print #1, Me.NE1String
Print #1, Me.NE2String
Close
End Sub

The code above works just fine, and appends the text in the specified fields, into a text file na,ed "7705-LOG.txt", and places it automatically in the folder I've listed above.

The problem is, other users need to use the log file too, so I don't want it saving to my personal folder, I'd rather just save it to a network drive, or just the C: drive.

I have tried the following, but I keep getting the error I mentioned above:

ATTEMPT 1:

Private Sub Command427_Click()
Dim File_Path As String
File_Path = " C:\7705-LOG.txt"

Open File_Path For Append As #1

Print #1, Me.CircuitID
Print #1,
Print #1, Me.NE1String
Print #1, Me.NE2String
End Sub


The code above produces the following error:
Run-time error '52':
Bad file name or number


My next attempt looks like this:

ATTEMPT 2:
Private Sub Command427_Click()
Open "C:/7705-LOG.txt" For Append As #1
Print #1, Me.CircuitID
Print #1, Me.NE1String
Print #1, Me.NE2String
Close
End Sub

The code above produces the following error:
Run-time error '75':
Path/file access error





I can guarantee there's no problems accessing the C drive, and there's nothing wrong with the file name, as it works just fine when I don't specify a path.
Can someone please help!!??

All the code I've looked up online says this should work... why doesn't it?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What error messages are you getting?
 
Upvote 0
Just the runtime errors I listed above:

Run-time error '52':
Bad file name or number

and

Run-time error '75':
Path/file access error


1 thought occurs to me, my company has recently switched to some MicroSoft single sign-on service, and I'm starting to suspect it has something to do with that.

I was able to direct the file to the user's desktop with:

Code:
[I]Dim FileName As String[/I]
[I]FileName = "..\desktop\7705-LOG.txt"[/I]
[I]Open (FileName) For Append As #1[/I]


For now, that will work fine for people...
If someone else knows what's happening though, please continue to suggest ideas...
 
Upvote 0
It seems I can't get out of the user's domain...
what I mean by that is.. my userID is "baror1"

"../" brings me to that baror1 folder in the users folder... and access doesn;'t have a problem with that, but if I add another "../" to the beginning, which should take access to the root C: drive, that's when I get the runtime errors.

It seems, as long as I stay within the user folders of the currently logged in user, it works.
That's what makes me suspect this is an issue with the microsoft Single Sign-on service, which is a fairly new corporate service provided by Microsoft.
It seems Access does not want to leave the user's 'domain' so to speak...
 
Upvote 0
@ Norie

Norie, actually the OP has provided the error messages. Easy enough to miss them. :)

@ rbaron

For your first error, it looks like you have a space at the beginning of the string used for the path and filename.

For your second error, it looks like you incorrectly have a front slash (/) as the path separator, instead of a backslash (\).
 
Upvote 0
@ Norie

Norie, actually the OP has provided the error messages. Easy enough to miss them. :)

@ rbaron

For your first error, it looks like you have a space at the beginning of the string used for the path and filename.

For your second error, it looks like you incorrectly have a front slash (/) as the path separator, instead of a backslash (\).


ARG.
Bad on me for mis-typing in the forum.

I checked my code, and both those errors are *not* actually in my code. (did some quick editing when I pasted into this forum to make it easier to read, but ended up making those small errors when I posted this)

Sorry for that...



ATTEMPT 1:

Code:
Private Sub Command427_Click()
[I]Dim File_Path As String
[I]File_Path = "C:\7705-LOG.txt"
[I]Open File_Path For Append As #1
[I]Print #1, Me.CircuitID
[I][I]Print #1, Me.NE1String
[I]Print #1, Me.NE2String
End Sub[/I][/I][/I][/I][/I][/I][/I]


The code above produces the following error:
Run-time error '52':
Bad file name or number


ATTEMPT 2:
Code:
Private Sub Command427_Click()
[I]Open "C:\7705-LOG.txt" For Append As #1
[I]Print #1, Me.CircuitID
[I]Print #1, Me.NE1String
[I]Print #1, Me.NE2String
[I]Close
[I]End Sub[/I][/I][/I][/I][/I][/I]
The code above produces the following error:
Run-time error '75':
Path/file access error


ATTEMPT 3:
Code:
Private Sub Command427_Click()
[I]Dim File_Path As String
[I]File_Path = "..\..\7705-LOG.txt"   ' To get to the C: drive
[I]Open File_Path For Append As #1
[I]Print #1, Me.CircuitID
[I][I]Print #1, Me.NE1String
[I]Print #1, Me.NE2String
End Sub[/I][/I][/I][/I][/I][/I][/I]



The code above produces the following error:
Run-time error '75':
Path/file access error



ATTEMPT 4:
This attempt worked for me, and placed the file on the user's desktop
Code:
Private Sub Command427_Click()
[I]Dim File_Path As String
[I]File_Path = "..\desktop\7705-LOG.txt"   ' To get to the C: drive
[I]Open File_Path For Append As #1
[I]Print #1, Me.CircuitID
[I][I]Print #1, Me.NE1String
[I]Print #1, Me.NE2String
End Sub[/I][/I][/I][/I][/I][/I][/I]


It's interesting that attempt 4 worked for me, but attempt 3 did not...
 
Upvote 0
Note that it is entirely possible you cannot write to the bare C: drive on another users machine (and probably you shouldn't be able to either). The same is likely true for C:\Users - which is where ../../ should get you, actually.

To be honest, it sounds like you really need to pick a good location for your text file.


BTW, you should also be closing the file.
 
Last edited:
Upvote 0
Ahh thanks...
I will make sure I add the CLOSE to that.

I'm just trying to access the C drive of the machine that the program is running on, not a *different* user's C drive...
Just the root C drive of the local machine.

That's why I can't understand why I'm getting the error... but like I said previously, it probably has something to do with this new Microsoft Single Sing-On service our work signed up with...

and my work-around to place the file on the desktop works fine, so the user can just go find it himself and open it.
(I was previously, trying to place the file on C drive, so I could have another button in the form to open the file, and a third button to delete the file.)
 
Upvote 0
Just don't save things on the C: drive, I think that's a good rule to follow. Access to the C: drive is restricted in many environments.
 
Upvote 0
Thanks all for your help and suggestions... I really appreciate all your time and effort.
i think we've got a good work-around here.. not what I had originally envisioned, but this will work just fine.

Many Thanks!!!

Rob.
 
Upvote 0

Forum statistics

Threads
1,221,783
Messages
6,161,940
Members
451,730
Latest member
BudgetGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top