?
Solved

VB6 - Making sure that a file is closed

Posted on 2007-10-10
7
Medium Priority
?
545 Views
Last Modified: 2008-01-09
Dear Gurus
I've been encountering a situation where a text file channel, according to VB, is already open.  It occurs about 10% of the time the routine is called. This has given me a rather puzzling problem.  How is it that a 'File already open' error can occur at line #112, given that the file channel would have just been closed??

The size of the files saved is rather small, about 1,000 characters. I've carefully checked through the code in my app:
(i) all references to files make use of the 'intfnum = FreeFile' method, ie, no fixed channel assignments
(ii) files are closed prior to the end of each subroutine
I searched EE and found a suggestion to introduce a small delay, hence the loop below.  I'm not sure if this has made a difference.

Your help in advance is most appreciated.

Private Function DO_THE_FILE_SAVE(ByVal FilePath As String, ByVal s As String)
'11-oct-07, bring it here
Dim intfnum As Integer, FileChannelNumber As Integer
Dim Result As Long, j As Long
     Const procname As String = "DO_THE_FILE_SAVE"
     On Error GoTo Err

100     If IsFileAlreadyOpen(FilePath, s, FileChannelNumber) = -1 Then
            Close #FileChannelNumber 'if the file happens to still be open from a previous operation, close it
        End If
       
104     For j = 1 To 40000 'delay, typically a few hundred milliseconds, so we can try again
            DoEvents
        Next
       
110     intfnum = FreeFile 'a new file handle
112     Open FilePath For Output As #intfnum
114     Print #intfnum, s
116     Close #intfnum
       
120     Exit Function
           
Err:
200     MsgBox "ErrCode: " & Erl & vbCrLf & "Error: " & Err.Description 'File already open' error
End Function

Private Function IsFileAlreadyOpen(ByVal FilePath As String, ByVal s As String, ByRef FileChannelNumber As Integer)
'11-oct-07
Dim intfnum As Integer
     Const procname As String = "IsFileAlreadyOpen"
    On Error GoTo Err
   
100     intfnum = FreeFile
        Open FilePath For Input As #intfnum
        Close #intfnum
        IsFileAlreadyOpen = 0 'ie, ok
        Exit Function
   
Err:
        Select Case Err.number
        Case 55 'thanks EE
            IsFileAlreadyOpen = -1 'error
            FileChannelNumber = intfnum
        End Select
End Function
0
Comment
Question by:BrianBeck
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:Jai S
ID: 20053976
you dont need a loop to delay a process - thts called as  BAD practice

use
Sleep(millisecs) instead
and call DoEvents
Sleep(your integer value)
DoEvents
0
 

Author Comment

by:BrianBeck
ID: 20054162
Hi jaiganeshsrinivasan:

Thanks, will do.  Would be happen to know of an answer to the underlying file open problem?
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 20055848
Private Function DO_THE_FILE_SAVE(ByVal FilePath As String, ByVal s As String)
'11-oct-07, bring it here
Dim intfnum As Integer
Dim Result As Long, j As Long
     Const procname As String = "DO_THE_FILE_SAVE"
     On Error GoTo Err

100     while (IsFileAlreadyOpen(FilePath) = -1) Then
            doevents
            Sleep(millisecs) instead  ' suggested by jaiganeshsrinivasan
            Wend
       
110     intfnum = FreeFile 'a new file handle
112     Open FilePath For Output As #intfnum
114     Print #intfnum, s
116     Close #intfnum
       
120     Exit Function
           
Err:
200     MsgBox "ErrCode: " & Erl & vbCrLf & "Error: " & Err.Description 'File already open' error
End Function

Private Function IsFileAlreadyOpen(ByVal FilePath As String)
'11-oct-07
Dim intfnum As Integer
     Const procname As String = "IsFileAlreadyOpen"
    On Error GoTo Err
   
100     intfnum = FreeFile
        Open FilePath For Input As #intfnum
        Close #intfnum
        IsFileAlreadyOpen = 0 'ie, ok
        Exit Function
   
Err:
        Select Case Err.number
        Case 55 'thanks EE
            IsFileAlreadyOpen = -1 'error
        End Select
End Function

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:BrianBeck
ID: 20055888
Hi EDDYKT:
Thanks, but from what I've seen 'sleeping' or 'waiting' doesnt seem to solve the underlying problem.
Is there some way in which I can brute force delete the existing file (I've already saved the new contents in a string, so once the file is deleted, I'd simply create a new text file with the same name as the original one).
In other words, is there a way to bypass VB's built in open file check, and simply request Windows to delete the file?  I realise that this may be unorthodox, but can it be done?
Brian
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 2000 total points
ID: 20056157
the question is why the file still being open? by who?
One way i suggest is to use registry instead of file.

This way you may not have problem to lock the data.

>>In other words, is there a way to bypass VB's built in open file check, and simply request Windows to delete the file?  I realise that this may be unorthodox, but can it be done?
not sure you can do it, try the following (not guaratee)

1. use command. del /F /S filepath
2. user api. Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long

etc
0
 

Author Comment

by:BrianBeck
ID: 20056227
Hi EddyKT
Thanks for your suggestions, though as I need an automated, reliable approach, the registry method seems promising.  I'm unsure as to why the file remains open.

Is there a limit to the number of characters in can have in a string?  Can the string have CRLF delimeters?
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 20056253
if you use string value in registry 8192 is max number of char (if i remember correctly)
if you have more than the above number, use expanable string
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses

862 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