?
Solved

Using FreeFile(1) and trying to open a file

Posted on 2005-04-11
18
Medium Priority
?
2,117 Views
Last Modified: 2012-08-13
Here's the code...

    Dim intContractFile as Integer
    intContractFile = FreeFile(1)
    Open strDataPath & "\LARCONTR" For Random As #intContractFile Len = 220

When I check the debug progress, intContractFile = 256

Here's the error...

    Run-Time Error '5':
    Invalid Procedure Call or Argument

The open command in the help file shows this as a file number argument:

    filenumber: Required. A valid file number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.
** AND **
    filenumber:  Number used in the Open statement to open a file. Use file numbers in the range 1-255, inclusive, for files not accessible to other applications. Use file numbers in the range 256-511 for files accessible from other applications.

And here's what FreeFile says:

    FreeFile[(rangenumber)]  :The optional rangenumber argument is a Variant that specifies the range from which the next free file number is to be returned. Specify a 0 (default) to return a file number in the range 1  255, inclusive. Specify a 1 to return a file number in the range 256-511.

I'm at a loss.  I need help.  I need to open the file in the higher range because the file IS being used by other applications.
0
Comment
Question by:aravary
  • 6
  • 5
  • 2
  • +4
17 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 13755927
Are you open any file in the upper range?  I do not have a problem on my Win XP Pro machine.

As a test, hard code everything:

Open "c:\test.txt" For input as #555
Close #555

0
 
LVL 32

Expert Comment

by:Erick37
ID: 13755931
That should have been: Are you able to open any file in the upper range?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13755977
I'm not doing very well at all today :\

Obviously use a number from 256-511 (not #555)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 54

Expert Comment

by:Ryan Chong
ID: 13755985
try like:

 Dim intContractFile as Integer
    intContractFile = FreeFile
    Open strDataPath & "\LARCONTR" For Random As #intContractFile

....
0
 
LVL 1

Author Comment

by:aravary
ID: 13755996
I should have added this: The code is being used in a VB DLL that I'm using in a bunch of my own apps.

I can not open ANY files in the upper range within the DLL.  If I use the immediate window in VB, it works, but not within the code itself.  Using WinXP Pro.
0
 
LVL 4

Expert Comment

by:Sicos
ID: 13756021

The code seems ok to me...

Are you sure that the path where you are trying to create the file exists?

Or try this instead .. just to be sure...

    Dim intContractFile as Integer
    intContractFile = FreeFile(1)
    Open "C:\TestFile.txt" For Random As #intContractFile Len = 220

Greetings,
Sicos
0
 
LVL 1

Author Comment

by:aravary
ID: 13756039
Sicos:  Already attempted.  If I use FreeFile(0) or FreeFile, it works fine.  It doesn't seem to want to open a file with a filenumber > 255.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13756077
I just reproduced the problem by attempting to open a file > 255 in an ActiveX DLL.  It does not like it.
0
 
LVL 1

Author Comment

by:aravary
ID: 13756101
Erick37:  Indeed, it does not.  If it's a flaw by design, then that's all I need to know and then I'll look elsewhere for a solution.
0
 
LVL 9

Expert Comment

by:sk33v3
ID: 13757218
Just our of curiousity can you try 65535 as the value too?
0
 
LVL 6

Expert Comment

by:cjard
ID: 13765194
you do not supply a value to FreeFile; it supplies you with a value:

Dim fileID as Integer
fileID = FreeFile

Open "c:\test\file.txt" For Binary As #fileID
0
 
LVL 6

Expert Comment

by:cjard
ID: 13765267
having read the last line of your post a little more closely, im at a bit of a loss as to how freefile gives you a file ID that is already in use.. this is in use by your application, right? if it is another application that is using the file, then i dont think that is anything to do with file numbers; its to do with file locking. an application amy choose to lock a file when opened so that other apps cannot access it. FreeFile has no bearing on this, FreeFile merely finds you a file id number that is free for use in your app so that your app can have multiple files open at once

example of opening a file with locking:

Open "C:\test.txt" For Input Lock Read Write As #fileID


now try and open it in notepad.. you should get a message that access is denied because file is in use (when your app has it open)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13765825
This is the documentation of file numbers:

"file number
Number used in the Open statement to open a file. Use file numbers in the range 1–255, inclusive, for files not accessible to other applications. Use file numbers in the range 256–511 for files accessible from other applications."

In an ActiveX DLL, you can use the range 1-255, but for some reason not 256-511.  I have found no other documentation of how to use a file number for "files accessible from other applications."


0
 
LVL 1

Author Comment

by:aravary
ID: 13771560
I think this problem stems from a design flaw.  Wether it's intentional or not, I don't think I'll be able to find a solution to this.  In any case, this would be the first time I would be using a file number > 255, so I don't even know if that route would have been the best one for me.

So if DLL's aren't supposed to use file numbers > 255 and there's no real solution to my problem, how do I handle this question?

Not to sound like a virgin, but this is my first time.
0
 
LVL 1

Author Comment

by:aravary
ID: 13771630
cjard: The file locking is not the issue, since I will be using RANDOM as a method to access my data.  I will be using record locking.  As far as the functionality the "freefile" function, I think it will give you a free file number from the current applciation only.  I was just assuming Open would handle things a little differently if it was using a file number > 255.  The documentation in MSDN is a little lacking as to why we whould use file numbers > 255 if the files are being used in other applications.
0
 
LVL 1

Author Comment

by:aravary
ID: 14365615
I wish I could clean up after myself.  I never got the answer I needed.  I have no objections.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 14402904
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Independent Software Vendors: 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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
Course of the Month13 days, 22 hours left to enroll

807 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