VB6 Strange Error 70

Posted on 2014-12-15
Last Modified: 2015-01-28
Can anyone explain why I am sometimes getting "error 70 bad file name or number" with the following code?
     FileNum = FreeFile
     Open "C:\MyPath\DataNew.dat" For Output As #FileNum
     Print #FileNum, "Test String"  '<<<< this line errors
     Close #FileNum
As I said it works perfectly most of the time but I cannot see why it should ever fail.
It happens in Windows XP and Windows 7
Thanks for you help
Question by:richardbc
  • 4
  • 4
  • 2
  • +2
LVL 32

Expert Comment

ID: 40499908
It's normally a permission denied error.

A normal use does not have write permissions to this path under normal circumstances. Another problem maybe that there is no more file handle available. This may occure when your program is making masive use of the FreeFile function or it is a long running application.

Author Comment

ID: 40499942
The thing is it work perfectly most of the time on hundreds of computers.
Re file handles - this is unlikely as the code is only called during startup and I close files after use so there is never more than 30 files open and that is only in one routine - the rest of the time no more than 12.
LVL 68

Expert Comment

ID: 40500007
Maybe the script is executed twice at the same time?

Author Comment

ID: 40500011
Not possible I have code to prevent more than one instance of the program running on one computer.
The code is in a routine that only can run once as the program is starting.
LVL 45

Expert Comment

by:Martin Liss
ID: 40500733
Is the file name actually "DataNew.dat" or is there code that generates the name? If it's the latter then it might be generating an invalid file name.
LVL 12

Expert Comment

ID: 40503809
I would have to agree with ste5an, specifically regarding "permissions".  If the error only occurs on, for example, PC1, but not on the other PCs, then you might have to verify that C:\MyPath has "write" permission for the user that's running your program.  Also, avoid using system folders, such as C:\Program Files or C:\Windows as standard users can't write to this folders under the default Windows XP/7 security policy.

Another thing might be the Anti-Virus software on the PC in question deleting OR locking the file right after it was created.

To ensure your program always has exclusive access to the file, you should use the "lock" keyword as follows:

Open "C:\MyPath\DataNew.dat" For Output Lock Write As #FileNum
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.


Author Comment

ID: 40504168
The filename is hard coded
As the code works most of the time on all computers and then happens on one I cannot see how it can be a permissions problem,
Also rerunning the code works correctly.
If it was a system folder I would have said so.
There is no reason to use lock write and this error would be different if the file was locked.
The reason I asked the question is because it only happens sometimes and there appears to be no reason.
LVL 45

Expert Comment

by:Martin Liss
ID: 40504190
Could more than one person be trying to write to the file? (That is one reason for error 70).
LVL 12

Expert Comment

ID: 40506047
There is no reason to use lock write and this error would be different if the file was locked.

This is completely misleading and an incorrect assumption.

Error 70 generally means "Permission denied" per the MSDN Help Library.  Please note that the term "permission denied" may not be necessarily related to "read/write permission" on the file.  And since the issue happens sporadically on just one machine, that particular machine may be running something or configured differently than the others.  Also, since the file is NOT exclusively locked when created, it is absolutely possible that some other processes can lock it thereafter, such as an Anti-virus software.  By the way, it is NOT always required to lock a file open for output or append - my suggestion to "use the lock method" was merely to see if that takes care of the issue in question.

Error 70 is quite general and has multiple meanings per the MSDN Help as follows:

You tried to open a write-protected file for sequential Output or Append.
Open the file for Input or change the write-protection attribute of the file.

You tried to open a file on a disk that is write-protected for sequential Output or Append.
Remove the write-protection device from the disk or open the file for Input.

You tried to write to a file that another process locked.
Wait to open the file until the other process releases it.

Author Comment

ID: 40527172
We did have a problem some years ago where there was an HP printer driver that locked files for no reason - updating the driver fixed this.
I do find it strange that the file is one that works perfectly most of the time and is one that no other process opens and is only opened once during the program starting.
I do not see anything in the suggestions that explains why the error should happen in this case but I was hoping that someone might know of some strange thing that can cause this error.
I know that Vb's error trapping is a Microsoft thing and therefore is not 100% accurate.
I was testing some code the other day and having opened a file for random access I got the error 70 after inputting many records - I found that the input was way past the EOF but it did not give the error until it had input hundreds of records past the EOF.   I did have and EOF trap but that failed to trap!
I still think VB6 is one of the great programming languages and I see it is still number 3 in the world!
Thanks for your help - I will just have to wear the problem as re-running the program runs without the error.
It's just I would like to find some way to prevent the error ever happening as it's annoying when any error happens!
LVL 45

Accepted Solution

Martin Liss earned 500 total points
ID: 40527226
You can ignore the error by doing something like this:

On Error GoTo ErrorRoutine

' code

Exit Sub
If err.Number = 70 Then
    Resume Next
End If

Open in new window

LVL 45

Expert Comment

by:Martin Liss
ID: 40574812
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now