[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


VB6 Strange Error 70

Posted on 2014-12-15
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
LVL 35

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 71

Expert Comment

ID: 40500007
Maybe the script is executed twice at the same time?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 49

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 49

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 49

Accepted Solution

Martin Liss earned 2000 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 49

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

656 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