944,033 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
May 13th, 2006
0

overwriting files.

Expand Post »
Hi,

i'm trying to create a project where i can click an option button and it will overwrite a file.

i know the code to overwrite it but the problem is i don't know how to get it to work if that makes sence..

what i'm trying to do is make a program that reads bank accounts.. once the account file is loaded i want to be able to withdraw cash. for example if i withdraw £10 i want it to overwrite the amount that i have in my file.. so if i had £250 then it would go to £240..

does anyone know how to do this?

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Put #1, BankAccount, CurrentBalance

i know thats how i overwrite it, its just i dont know how to get the money to take it away.

any help would be great.. thx in advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmarsh is offline Offline
18 posts
since Aug 2005
May 13th, 2006
0

Re: overwriting files.

I would do it differently. depending on the size of the file, I would either read the entire file into memory, and then manipulate what I want to change, and then over-write the file with the new information, or (assuming the file is pretty big) read the data in, line by line, and change what I need when I get to that line, and in the mean time write the information back to a temporary file. Once The loop is done, I'd delete the original, and rename the temp file to the name of the original. I would use put either (unless the file is somehow a random access or binary), but I'd use the print command..... if you post the file (or some of it) so that I can get a better feel for it, I can help you out a little more. Also, something to keep in mind, is that it depends on how you open the file..... say: open "c:\somefile.txt" for output as #1 will automagically over-write anything in the file, when you do your first print #1, "new data".
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 13th, 2006
0

Re: overwriting files.

yeah i know what u mean by using the
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. open "c:\somefile.txt" for output as #1

but i have to use a commondialog box i need to act as a system admin loading up a file each work day and then from there i have a certain amount of accounts.. at the moment i only have 3 accounts that i need to read. once i've actually loaded 1 of the accounts i have 3 choices. i can either print a mini statement, withdraw cash or withdraw cash with a receipt.

but when i withdraw cash i need to then update the file.. lets call it accounts.txt so that if they have £250 in there account to begin with and they take £50 out then it will change there amount to £200. thats why i need the
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. put #1
means i need to have a if statement aswell..

reading kinda like

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. if opt10.value = 1 Then
  2. BankAccount.CurrentBalance - 10
  3. end if

the only thing i dont know how to get to work is the money part.. i can get the if statement working ok but the rest i'm a little foggy on.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmarsh is offline Offline
18 posts
since Aug 2005
May 13th, 2006
0

Re: overwriting files.

again, I have to know exactly how the textfile is setup (the file with the accounts) if you post a few of the records, and modify them if needs be for confidintiality reasons, I can help you a lot more.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 13th, 2006
0

Re: overwriting files.

i've added the file that i'm using... and i've also uploaded a little vb file that will allow u too read it so u can see it.
Attached Files
File Type: zip Write to file.zip (3.3 KB, 161 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmarsh is offline Offline
18 posts
since Aug 2005
May 13th, 2006
0

Re: overwriting files.

Ok, first thing's first. I have to Strongly Discourage Your selection in record keeping. I have TONS of reasons why, but the most compelling reasons can not be explained with words..... they come from experience in so doing. If this is not a school project, and/or this is not a required means to storing your data (if it's a school project, then naturally the teacher would want it done a certain way, if it's a business application, there might be some kind of sick reason why the business/customer wants it done that way), then your best bet is change the way the data is stored. Period. I have a lot of experience working with databases stored using random files and user defined types.... and every outcome has turned out poorly.

I also don't believe in encouraging change without offering an alternative, and therefore, I suggest 2 alternatives. 1, would be use a structured database (be it SQL, or Access.... or oracle, who cares.... something made for storing records.), and the other, is to use a delimited flat file. Both of these options make your life, infinitely better, and simplier. One main point is portability. Eventually, you are going to want to port the information into another program (I know it doesn't seem that way now, and maybe with this app you won't, but getting started running with your shoe laces tied sucks). I can't tell you the number of times I saved data a certain way (using random files and types) and then a boss or a client needed to be able to view the information in another program (most often excel). Then I had to write another program to extract the data from my data file, and port it to a format that excel could read...... what a pain.

Alright, enough of the lecture.... you have been urged, and warned.... I'm working on the code to over-write a single record right now.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 14th, 2006
0

Re: overwriting files.

thx comatose.. and i do agree with u on the programming but unlucky for me, it has to be done this way i know it sucks.

thx for all ur help and i dont expect you to give me all the code just a step in the right direction would be great ..

not that i dont mind u give me the code though
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmarsh is offline Offline
18 posts
since Aug 2005
May 14th, 2006
0

Re: overwriting files.

If you don't mind me asking, why does it have to be done in a random file? Homework? If there is any other way, believe me, you want it. That said, I've added a button (Debit Button) To Your Project, that debits a given amount from the selected account. Hope your up for a bit of constructive criticism, because I'm about to lay it out. One Thing, is I would change the setup a bit, so that you don't have to select a file, and then seperately display the records in it with another button. Both Of those operations should go hand in hand, for user interface comfortability.... You select the file, and Bam, Everything is Ready. Speaking of which, have you clicked the button to load a file, and then clicked "Cancel" on the common dialog box? Nice Surprize there.

You made life a bit more complicated by using a sorted listbox.... my first plan was to add a new listbox that would remain hidden, and would directly coorespond to the record selected in the listbox, but you can't keep record numbers in line with record info, when the listboxes get sorted.... SO, we had to rip apart the information (by " ") in the listbox, and then open the file and loop through every record, and compare it to the card number and the pin. If both of those matched, then we set a variable that contains the record number that was selected by the user in the listbox. Then we load Acct (The user defined type) with the information of the record that was selected by the user. We pop up an inputbox asking for the amount to debit, and we check if the amount to debit would make the account balance negative. if so, we pop up a msgbox asking if it's OK for them to go into the negative balance, if not, we leave the sub, if so (or if the balance would not be negative after the transaction), we deduct the amount from .currentbalance, and re-write the information to the file, at the position of the selected record. Then we programmatically "click" your button that loads the data into the listbox (so that it updates after the debit) and that about does it.

Another thing, is that I commented out your option explicit (I hate rules... I always have). If you want to add it back in, be my guest, but I did not define 3 of my variables, 1 array of type string, and 2 scalar of type string (Rparts [array for record parts], CardNo [To Get The Card Number of the selected record from the listbox], and PinNo [Also Taken From the Info in the listbox])

This should give you a pretty good idea of what you have to work with, and the code is commented pretty well, so you should be able to get a feel of what you need to do to credit the account, or do whatever else is needed. If you don't mind my critique, and you need any more help, just let me know.
Attached Files
File Type: zip Write to file.zip (3.9 KB, 151 views)
Last edited by Comatose; May 14th, 2006 at 4:22 pm. Reason: Forgot Attachment
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 14th, 2006
0

Re: overwriting files.

yeah i have to have it in a random file for college work and the file that i sent was just to set up some records. that program doesn't get used at all in my real one.. it was just to show u what i ment.

the actual program that i'm working is a mock ATM. where u enter ur pin number and then u can either withdraw cash, with or without a receipt, print a mini statement and also display your balance.. just like any normal ATM..

thx for all your help comatose :cheesy:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmarsh is offline Offline
18 posts
since Aug 2005
May 14th, 2006
0

Re: overwriting files.

Sounds Pretty complicated actually.... but you are more than welcome... I hope it all turns out alright.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Copying Table & Data From 1 MS Access Satabase To another
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Writting scripts to decode





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC