File I/O question

I want to be able to scan through a file, find a peice of data, then once ive found this data i want to write new data over top of it, Anyone know how to do this? I try to use the Open ... method but i cant open a file and read and write to it useing this method.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dim v as integer
Dim i as integer
v = FreeFile

i = 0

Open FileName for Random Read Write as #v Len = Len(Record)

Do While Not EOF(v)
    i = i + 1
    Get #v, i, Record
    If Record <is the right one> Then
        Put #v, i, NewRecord
        Exit Do
    End If

Close #v
CzehAuthor Commented:
It first says invalid procedure call on the "Open" line, so i tryed taking off the Len=Len(Record) that works then it says "Variable uses an automation type not supported by VB" in the get statement. Hmmm, btw this is what my file looks like:

Foyal was on the right track......
First you must define a template for your data file. I can't tell from your data exactly how it's laid out.
First, define a data Type for you file elements, and declare one (here's one for meat products):

Type ProductRecord
    PLU AS Integer
    PID As String * 9
    UPC_ID As Long
    Description1 As String * 32
    Description2 As String * 32
    Description3 As String * 32
End Type
Global prCurrentProduct As ProductRecord

Your elements can be anything you wish. A lot of people define everything with strings. You must declare the length of each string.

You then define data for each element:

With prCurrentProduct
  .PLU = 123
  .PID = "123456789"
  .UPC_ID = 22345
  .Description1 = "TEST LINE 1"
  .Description2 = "TEST LINE 2"
  .Description3 = "TEST LINE 3"
End With

Here's where Foyal's code, or at least a little of it comes in. This opens  afile and stores this recor as the "first" product:

Open "TESTFILE" for Random as #1 Len = Len(prCurrentProduct)
Put #1, prCurrentProduct
Close #1

You can read it back using:

Open "TESTFILE" for Random as #1 Len = Len(prCurrentProduct)
Get #1, prCurrentProduct
Close #1

If you have more than 1 "product" you just change the "Put #" to recall or write the record for that product. That's to loop in Foyal's search for the correct record.

It al starts with defining your data structure....good luck!


The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Thanks KFrick... I had seen Czeh's response to my answer, but I didn't have time to clarify my answer at the time. The user defined type is the secret to the whole thing.
See 'ya,
CzehAuthor Commented:
This is good, i need a little bit more info, currently my lines of text in my file change and have varying lengths, is their any way to read only one line, or 1 'set' of the file without haveing to know the length?

BTw thanks for all the help
Set the Type definition to the Longest Length required for each variable....shorter lengths will be stored with enough space behind them to make everything "line up" when you attempt to retrieve the data.
Is this a file you are createing, or just trying to read?

Alternately, you can open the file for INPUT and read it in using the LINE INPUT # statement. You could even test for the required data. This method is Much Slower, but may work for you:

Open "TestFile" For Input As #1
Open "NewFile" For Output AS #1
While Not EOF(1)
  Line Input #1, Data$
  Debug.Print Data$
  If Data$ = {Your test here} Then
    Data$ = NewData$
  End If
  Print #2, Data$
Close #1
Close #2

Then rename your old file as a backup, and rename your new file as the old file.

This is not so easy yet, is it!!!???


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.