Results 1 to 8 of 8

Thread: [RESOLVED] Data input from file already in use

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Resolved [RESOLVED] Data input from file already in use

    Hello all, I hope you can help me resolve what seems to be quite a straightforward problem.


    I've bulit an application that reads in data records which are stored in text files. I use the following method for doing this:
    Code:
    strCSVPath = "c:\mypath"
    strCSVFileName = "myfile.txt"
    
    With db1
        .CursorLocation = adUseClient
        .Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strCSVPath & ";" & "Extended Properties=""text;FMT=Delimited;HDR=NO"""
    End With
    
    With rs1
        .Open "Select distinct * From [" & strCSVFileName & "]", db1, adOpenStatic, adLockPessimistic, adCmdText
    End With
    However, the files being read in are on a network drive, and if some other user has it open on their system when my program tries to run the above code, it gives an error message saying something like "Permission denied - another person or program is using the data source".

    I have tried to resolve this issue by attempting to copy the file to another location using the filecopy method, then running the above code on the copied file, but unfortunately this gives the same error as above when attempting to do the copy. What's strange is that I can copy the file manually with my mouse in Windows without any error, but if I try programatically do it I get the error.

    So, my questions are - a) how can I read the data when someone is already in the file, or b) how do I replicate Window's method of copying a file which is already in use.

    Many thanks for your time

    James
    Last edited by VB22; Dec 16th, 2007 at 05:21 PM.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Data input from file alredy in use

    You might be able to open the file, read the data and process it or write it to a temp file. Try this, supplying the path & file name
    Code:
    Dim bData() As Byte
    Dim ff as Integer
    ff=freefile()
    On Error Resume Next
    Open [path/file] For Binary Access Read As #ff
    If Err then 
       MsgBox "Failed to open file. Ensure Path/File Name exists"
    Else
       On Error GoTo 0  ' allow errrors to happen, if any
       ReDim bData(1 To LOF(ff))
       Get #ff, , bData()
       Close #ff
       ff=FreeFile()
       Open [temp path/file] For Binary As #ff
       Put #ff, , bData()
    End If
    Close #ff
    Replicating Window's file copy: If I was on a pc where I could reproduce your problem, I would have tested what I'm about to suggest. Unfortunately, you will have to do that. There are 2 APIs commonly used for copying files: CopyFile/CopyFileEx which is probably what VB uses. You can try that API but I think it might fail too. The other is SHFileOperation. Recommend trying those to copy the file.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Data input from file alredy in use

    Thanks a lot for your suggestions LaVolpe - I will give them a try when I am back at work tomorrow and let you know how I get on.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Data input from file alredy in use

    If you want to copy the file even when it's open, this works

    Code:
    Dim vFs As Object, vFile As Object
        Set vFs = CreateObject("Scripting.FileSystemObject")
        Set vFile = vFs.GetFile(cPath + "data.txt")
        vFile.Copy cPath + "import_archive\" + cFname

  5. #5
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    564

    Re: Data input from file alredy in use

    Quote Originally Posted by wes4dbt
    If you want to copy the file even when it's open, this works

    Code:
    Dim vFs As Object, vFile As Object
        Set vFs = CreateObject("Scripting.FileSystemObject")
        Set vFile = vFs.GetFile(cPath + "data.txt")
        vFile.Copy cPath + "import_archive\" + cFname

    That looks interesting! What do you need to add to your project for a reference to get the Scriping object to work?

    --DB

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Data input from file alredy in use

    As late-binding is used in that example (using CreateObject, and variables declared simply 'as object'), you don't actually need a reference - it will effectively be generated at run-time (which simplifies installation, and avoids various version based issues).

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Re: Data input from file alredy in use

    Excellent wes4dbt - just tried your code on my home machine and it worked perfectly

    Thanks everyone for your help!!

  8. #8
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    564

    Re: [RESOLVED] Data input from file already in use

    Well isn't that just the most handy thing I've ever seen! Cool. Thanks very much. You wouldn't believe what I was doing to make a backup of files in use.

    --DB

Posting Permissions

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



Click Here to Expand Forum to Full Width