Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2007
    Posts
    88

    Red face Unanswered: text file read/write question

    Hi Gurus,

    I am developing a program to read a text file and save the data to access table.
    One lab machine is doing some tests and constantly write test data into that text file. Since I don't have the machine to do test, and I just create a text file by my own and keep adding data manually, close, save, open, add data, close, save....... to simulate the situation. However, it is definitely not the real situation as the lab machine write the data to text file at the same time.

    In the code I use
    Open TestFile For Input As #FFile
    to open the file and read data.

    Most of the time the program works OK in my simulation situation. However, tonight, one time it pop out an error message saying that the file is already open. I am worry about the file opening conflict issue may occur in the real lab situation.

    Do you know whether while the lab machine writing the data in the text file and my access program is reading the data from the text file at the same time , will it have the "file is already open" problem.

    If that happen, how to resolve the problem.???

    Thank you very much!

    NewGuy

  2. #2
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Yes it will probably happen. Two ways to deal with it.

    1. Have your program ignore the issue (using error handling) and just try again later. The real problem here is that while your program is accessing the file, the other one might just crash when it tries to update the file.

    2. COPY the file first. Copying a file only reads it (doesn't even open it) and you can do it while the file is open. Then import the copied file. This is safest imo.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  3. #3
    Join Date
    Aug 2007
    Posts
    88

    Red face Thanks for responding

    Quote Originally Posted by StarTrekker
    Yes it will probably happen. Two ways to deal with it.

    1. Have your program ignore the issue (using error handling) and just try again later. The real problem here is that while your program is accessing the file, the other one might just crash when it tries to update the file.

    2. COPY the file first. Copying a file only reads it (doesn't even open it) and you can do it while the file is open. Then import the copied file. This is safest imo.
    Thank you so much for help. I still have more questions need your further guidance.

    1. Would you please share a piece of code to do error handling to try again later to resolve the issue I have? I am not too good in error handling and text file open stuff. Thanks a lot!

    2. I don't know how the lab machine deal with the file opening. Do you think it constantly open the file to write data or it only open the file when it need to write the data in it?

    3. If the machine constantly open the data file to write, my program is trying to open the file to read it every 10 seconds. Will my program constantly encounter the "file is already open" problem? Then will error handling help?

    4. I don't quite understand why my program can not read the file while the machine is writing. My program is just using the command open file for input to read the file (doesn't even open it). Why it doesn't allow to open the file twice (machine open it for write, my program open it for read.) at the same time? When I manually open the notepad text file to read, other person have no problem to open the file at the same time. It actually can be opened many times at once. It seems like a snapshot. It takes the last change/save to be the final file.

    Thanks again.

    NewGuy

  4. #4
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    1. Sure.
    Code:
    Private Sub Form_Close()
        On Error GoTo ErrClose
        ... commands ....
        Exit Sub
    ErrClose:
        If Err.Number = <error code you want to trap> Then 
            ... commands...
        End If
        Resume [Next] or Exit Sub
    End Sub
    2. No idea. I would assume it intermittently opens, writes and closes the file.
    3. If it does work like that, you will need to copy the file first. The error handling thing will just fail every time if that's where the "File already open" is coming from. I would do some research on site if I were you. FIND OUT the answers to these questions before proceeding.
    4. It is possible that the error was produced by your code failing to close the file and then trying to open it again. Again, I'd do some testing on site to find out the answers to your questions on that -- even if the code you trial just reads the data from the file and writes it nowhere.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  5. #5
    Join Date
    Aug 2007
    Posts
    88
    Quote Originally Posted by StarTrekker
    1. Sure.
    Code:
    Private Sub Form_Close()
        On Error GoTo ErrClose
        ... commands ....
        Exit Sub
    ErrClose:
        If Err.Number = <error code you want to trap> Then 
            ... commands...
        End If
        Resume [Next] or Exit Sub
    End Sub
    2. No idea. I would assume it intermittently opens, writes and closes the file.
    3. If it does work like that, you will need to copy the file first. The error handling thing will just fail every time if that's where the "File already open" is coming from. I would do some research on site if I were you. FIND OUT the answers to these questions before proceeding.
    4. It is possible that the error was produced by your code failing to close the file and then trying to open it again. Again, I'd do some testing on site to find out the answers to your questions on that -- even if the code you trial just reads the data from the file and writes it nowhere.
    Thank you so much for spending time in answering my questions and sharing the code. I hope I will be able to do the test tomorrow or some time next week. I am waiting for them to setup the machine program and network to new PCs, before I can try my program on it.

    Really appreciate your help!

    NewGuy.

  6. #6
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    You're welcome
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •