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

Posted on 2009-02-12
Medium Priority
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 70

Accepted Solution

Éric Moreau earned 1000 total points
ID: 23621462
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

borgunit earned 500 total points
ID: 23621677
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

ID: 23622091
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Assisted Solution

VK earned 500 total points
ID: 23622281
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 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 1000 total points
ID: 23622283
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

ID: 23622646
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

ID: 31546016
Thanks very much guys.  Extremely helpful, prompt and clear.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
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.
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…
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…
Suggested Courses
Course of the Month14 days, 3 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