Working with text files

Asked By Mahima Chandani
12-Jan-10 06:56 AM
Earn up to 0 extra points for answering this tough question.

Hello,

I want to write a code that will read contents of a text file from a particular folder & add the contents into MS-Access database, after reading the file move it to another folder & move on to the next file in the folder.

I already have a code to read the contents of a file but my problem is that for faster execution I want my application to run in multiple instances.

If I run the script as is then the same file will be read by all instances.

Can you help me with a solution that if I run 3 instances of the application then all read different files & not the same file.

 

 

  FileStream Lock

F Cali replied to Mahima Chandani
12-Jan-10 08:21 AM

When you open your file for reading, you can lock the file (assuming you are using FileStream to open the file).  The FileStream object has a Lock method which locks the file so that no other process can access it.

Dim FileSt As FileStream = New FileStream("test.dat", FileMode.Open, FileShare.None)

        Try
            FileSt.Lock(0100)
            Console.WriteLine("Locked")
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try

So even if you have multiple instances of your application, check for files if they are locked before you process them.

Regards,
SQL Server Helper

  FileStream Lock

Mahima Chandani replied to F Cali
12-Jan-10 09:28 AM

Hello,

Got your point. But will I also be able to use Try, Catch in VBA ??

If no then what will be the command for the same.

Will try using the code snippet you have mentioned & let you know if it works.

 

Regards,

Mahie

  VBA Error Handling

F Cali replied to Mahima Chandani
12-Jan-10 09:33 AM

In VBA, the equivalent of the Try/Catch is the On Error command:

On Error Goto 0
On Error Resume Next
On Error Goto <label>:

Regards,
SQL Server Helper

Create New Account