Solved

VB6 Strange Error 70

Posted on 2014-12-15
13
139 Views
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
0
Comment
Question by:richardbc
[X]
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
13 Comments
 
LVL 34

Expert Comment

by:ste5an
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.
0
 

Author Comment

by:richardbc
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.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40500007
Maybe the script is executed twice at the same time?
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:richardbc
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.
0
 
LVL 48

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.
0
 
LVL 12

Expert Comment

by:jkaios
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
0
 

Author Comment

by:richardbc
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.
0
 
LVL 48

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).
0
 
LVL 12

Expert Comment

by:jkaios
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.
0
 

Author Comment

by:richardbc
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!
0
 
LVL 48

Accepted Solution

by:
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
ErrorRoutine:
If err.Number = 70 Then
    Resume Next
End If

Open in new window

0
 
LVL 48

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.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

695 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