Author Topic: Replacing Data in Text File  (Read 5779 times)

0 Members and 1 Guest are viewing this topic.

hendie

  • Guest
Replacing Data in Text File
« on: October 06, 2004, 05:39:26 AM »
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™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Replacing Data in Text File
« Reply #1 on: October 06, 2004, 08:45:36 AM »
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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

hendie

  • Guest
Replacing Data in Text File
« Reply #2 on: October 06, 2004, 09:15:25 AM »
I think I may have made things difficult for myself here. I had my data in the format

Code: [Select]
$CATEGORY(A)
#Material(n)
value
#Material(n)
value
#Material(n)
value
$CATEGORY(B)
#Material(n)
value
#Material(n)
value
etc etc

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: [Select]
Open "C:\MyFile.dat" For Binary Access Read Write As #DatFile
locating the record, and then using
Code: [Select]
Put #DatFile, , MyNewValue
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™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Replacing Data in Text File
« Reply #3 on: October 06, 2004, 09:52:10 AM »
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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

hendie

  • Guest
Replacing Data in Text File
« Reply #4 on: October 06, 2004, 10:09:21 AM »
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: [Select]
Shell "Notepad.exe" & " " & Datafile, vbNormalFocus

 :P  :P  :P

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Replacing Data in Text File
« Reply #5 on: October 06, 2004, 10:41:08 AM »
I don't think a rewrite would be needed... are you saying that the code you currently have will not completely replace the line if need be?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

hendie

  • Guest
Replacing Data in Text File
« Reply #6 on: October 06, 2004, 11:01:34 AM »
if the new data value is the same length as the old data value, it replaces the line successfully.
If the new data is a different length (shorter) then it replaces only that number of characters, leaving remnants of the old value behind. If it's longer, it eliminates the carriage return at the end of the line.

if the data values are the same length then this works
i.e. oldvalue= 1234
new value = 5678

Ndens = Ndens & vbCrLf
Code: [Select]

Put #DatFile, , Ndens

results in 5678
but if oldvalue = 1234
newvalue = 56
then the result is 56 but an extra carriage return is added
if oldvalue = 1234
newvalue = 56789
then the result is 12345(with a couple of "pipe" characters" ~ which don't show up here) and it removes two carriage returns from the file !

if I use Ndens = Ndens & vbCr then for example
oldvalue =1234
newvalue = 56
then I end up with 12(pipe character)4

confused ? yes I am

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Replacing Data in Text File
« Reply #7 on: October 06, 2004, 12:25:48 PM »
What are you using to read the data. The reason I ask is because if you open a file for binary operations, the put# writes data by the byte count, not by the line. Essentially you are saying replace these X bytes with these X bytes. If the destination is the same as the source, there is no problem, if the two sets of data are different, then the result will be inconsistent, with either extra carriage returns, a pipe symbol (due to the way the characters are written) or loss of data on the following line.

A much easier solution would be to open the file in sequential mode and then find the line to be replaced and use write or print to put the data in the file.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

hendie

  • Guest
Replacing Data in Text File
« Reply #8 on: October 06, 2004, 04:00:05 PM »
I tried using Print & Write and kept getting "bad file mode" ~ that's why I switched to trying Binary ~( I guess I'm just stabbing in the dark here until I hit something) I've never really tried to manipulate files before so it's all pretty new territory for me.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Replacing Data in Text File
« Reply #9 on: October 06, 2004, 04:27:21 PM »
you cannot open the file as read / write with print and write. You have to open it as input or output.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

hendie

  • Guest
Replacing Data in Text File
« Reply #10 on: October 06, 2004, 04:45:17 PM »
I know, I had the file open for Input when I was getting the bad file mode error.
I don't know how much time i'll be able to spend on it tomorrow and next week I'll be sampling the delights of Chesapeake's finest  so it ain't gonna get touched then.
Maybe the break will give me a fresh approach (though I would have loved to have solved it before I went stateside)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Replacing Data in Text File
« Reply #11 on: October 07, 2004, 12:16:45 AM »
Well don't give up... I'll see what other solutions I can devise in the meantime
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie