Reading a text file - Concerns about others trying to write to file

Posted on 2009-02-12
Last Modified: 2012-05-06
Hi everyone,

I am trying to import a text file every five minutes to give the latest items scanned in at the factory.  My code seems to work perfectly but I am concerned that I could be locking the source text file and causing other users not to be able to save data to it.  I have pasted my code into the code window, can someone tell me if this reads the source data into memory and then closes it or does it leave the source file open (and hence locked) while I read it.  Is there a better way to do this process than the one I am using.

Thanks in advance,

Open "p:\mypath" & Format(Date, "DDMMYYYY") + ".amp" For Input As #1

Open in new window

Question by:Afrofish
    LVL 69

    Accepted Solution

    Exclusive access is required to write to a file. Can't you copy the file before opening it in read mode?
    LVL 10

    Assisted Solution

    You would need to close it for others to open it. Can you let others view it in a browser (some extra coding) instead of opening the actual file or as mentioned copy it. Maybe you can name the original with an unknown extension and then read and write to it but save the copy as a text file for others to read. A few thoughts.

    Author Comment

    Thanks Guys,

    How would you recomend I copy the data? I have pasted my new code below, please could you see if it looks OK.  It seems to work fine but if there is a more efficient way to do it I would love to know.

    Thanks Again,

    FileCopy "C:\mypath\" & Format(Date, "DDMMYYYY") + ".amp", _
             "C:\" & Format(Date, "DDMMYYYY") + ".amp"
    Open "C:\" & Format(Date, "DDMMYYYY") + ".amp" For Input As #1
    'Code Here
    Kill "C:\" & Format(Date, "DDMMYYYY") + ".amp"

    Open in new window

    LVL 6

    Assisted Solution

    1. Your app has to read the file, is doesn't need any write permissions, right?
    2. Other apps have to write to the file, they need write permissions.

    First you it should be guaranteed that in 2. no collisions can occur.
    Therefore exclusive access is the best for 2.
    If the file is already open exclusively by an app, and a second tries
    to open it also exclusively, an error will be raised which can
    be handled to wait until the file is free for access.

    If 2. is implemented well your access to the file should be no problem.
    LVL 69

    Assisted Solution

    by:Éric Moreau
    That's the way to go. The only concerns I would make is that not all users have access to write to the root drive. You may prefer to keep the file into the same path but with a different name or extension.

    Author Comment

    Again, thanks very much for the prompt replies guys..

    While I accept VKs suggestion to add the error checking to the database trying to open the file, they are unfortunately out of my current project scope.  I have now been running my code for around 30 mins with no errors to the other DB so it seems to be working.  Time permitting I will revisit the other databases to check their error checking routines but I am happy in the meantime that I have minimised my impact on the system.

    Author Closing Comment

    Thanks very much guys.  Extremely helpful, prompt and clear.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    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…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    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…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now