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 33

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 69

Expert Comment

ID: 40500007
Maybe the script is executed twice at the same time?
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


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 46

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

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 46

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 46

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 46

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 55
Export Data to Different .csv Files 26 105
Automatic Email Reminder 4 38
VBA - Excel, Hide/unhide range of rows on sheet with listbox selection 9 48
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

803 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