Code Red > VB(A)

Replacing Data in Text File

(1/3) > >>

hendie:
I'm using a dialogue to show data from a text file. I want to allow the user to be able to change data in one field ~ if this data changes then I want to overwrite the old data with the new data.... and I'm having problems.
am I correct in thinking that the only way to accomplish this is by rewriting the original file to a new one and substituting the new data when I reach the line(s) of the old data ? i.e. there's no replace or overwrite method in VBA (I'm using 2000 btw)
another problem is that there may be multiple instances of the particular string in the file but I only want to replace the single item. I can locate the item, I just can't delete it or overwrite it.
any ideas ?

Keith™:
You would not want to overwrite the data file at the time the user changes the form data. What you can do is ...

Set a bit whenever any data changes in the form.
When the user closes (hides) the form with OK, if the "DataChanged" bit is true, then rewite the entire file using every data field, then unload the form.
When the user closes the form with CANCEL, clear the "DataChanged" bit and unload the form.
This way if a user changes something they can still cancel and not affect the old data.

A trick that I have used also is to create an array the size of my data field count, and place the data into that array for access later when I need to use it in my program.

hendie:
I think I may have made things difficult for myself here. I had my data in the format


--- Code: ---$CATEGORY(A)
#Material(n)
value
#Material(n)
value
#Material(n)
value
$CATEGORY(B)
#Material(n)
value
#Material(n)
value
etc etc
--- End code ---

and was sorting it into two listboxes on form initialise. The user would select a category, this would populate the material listbox. The user could then select a material and the value would be shown.
I wanted this value to be modified (if required) at runtime.
In theory, the same material can exist under several categories, each having a unique value. I can locate the particular material successfully and I've managed to butcher a "solution" together by using
       
--- Code: ---Open "C:\MyFile.dat" For Binary Access Read Write As #DatFile
--- End code ---

locating the record, and then using

--- Code: ---Put #DatFile, , MyNewValue
--- End code ---

which works (for the most part) but I'm getting inconsistent results writing the new value when the length of the new value doesn't match the length of the old value.
Once that part has been accomplished, the user exits back to another form which will then reload and utilise the newly modified (or added)  value.

I've been using chr(13) as part of the MyNewValue and sometimes it's successful while other times it adds a sort of bold pipe character to the value. I'm still trying to suss it out.

[cryptic clue] it's for that thing I'm working on Keith [/cryptic]

Keith™:
How about a 2 column list box? Would that work?
Have you tried just using 'vbCr' instead of Chr(13).... I believe it actually incorporates Chr(13)&Chr(10)
ooOO(or is that vbCrLf)OOoo

anyway it is a little simpler to utilize because you don't have to set the variable first as it is a constant.

hendie:
Keith,
I've tried
vbcr
vbcrlf
chr(13)
chr(13)+chr(10)

I'm going to try and see if I can find some pattern in the results I'm getting and maybe throw in a function to check the variables length & adjust code accordingly.
I'm trying not to rewrite the format of my data file as it means a lot of code rewriting... but if it has to be done then....



maybe I'll just have an "Add or or Edit Materials" button


--- Code: ---Shell "Notepad.exe" & " " & Datafile, vbNormalFocus
--- End code ---


 :P  :P  :P

Navigation

[0] Message Index

[#] Next page

Go to full version