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 :P 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?

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

Recommended Answers

All 11 Replies

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" .

yeah i know what u mean by using the

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

put #1

means i need to have a if statement aswell..

reading kinda like

if opt10.value = 1 Then
               BankAccount.CurrentBalance - 10
           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.

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.

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.

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.

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 :D..

not that i dont mind u give me the code though :P

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.

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:

Sounds Pretty complicated actually.... but you are more than welcome... I hope it all turns out alright.

its not that bad, at first i thought it was going to be really hard but after i actually read the scenario

i've got everything working except for the withdrawing of the cash. so thats the only bit i'm stuck at..

i've just looked at your code aswell and i just can't follow it :P but i'll get there.

:) I'll walk you through it.

  1. First, we declare the variables we will use (with the exception of the noted)

  2. we check if the user selected one of the accounts that is listed in the listbox, if not, we complain, and exit the sub.
  3. We Ask the user for an amount to debit, if they leave it blank, or click cancel, we forget about it, and leave the sub
  4. We Made it this far, so we know the user selected something in the listbox, so, we find out what they selected, and split apart the string by space (which is how it's shown in the listbox). This makes an Array that contains each field of the info in the listbox.
  5. We Assign CardNo and PinNo Variables To The Card Number and The Pin Number That Is Stored In the Listbox
  6. We Open the File, And Check every record to see if the card number and the pin number, are the same as the card number and pin number of the account selected in the listbox.... if they are the same, then we have the record we want to work with... In order for us to remember which Record Number We Are Fiddling With, We Set the "SelRec" variable to the same number as the number we are in our loop (so, we set SelRec To The Record Number)
  7. We Close The File, And Re-Open It Again, To Retrieve The Record Number That We Want To Work With (Ya know, The one saved in SelRec)
  8. Then, We Set A New Variable (And Don't Actually mess With the record Yet) To What The New Balance would be, if we subtracted the amount the user entered to debit, from the current balance.
  9. If The Account Will Go Negative (Less Than 0) Then We Warn The User Of Such, and Give them the choice to not do something stupid, like get charged over-draft fees
  10. If They Say No, We Exit The sub, if they say yes, or if the account balance will not go negative, then we assign CurrentBalance To the variable that contains the balance after the debit (the variable we used to see if the balance would be less than zero or not.... if they said go ahead and debit, then the current balance would be the same as that variable right?)
  11. Open The File again.... and write the account information that we have (with the updated current balance) to the same position (record) that we are working with.... remember in step 6 we saved the record we want to work with in SelRec... Now we use it again to write the info back to the file at the same position.
  12. Programmatically Click The Button That Loads the info from the file into the listbox.... we do this so that the listbox has the updated information after we re-wrote the record.

If you have any confusion about any of it, let me know, and I'll see if I can explain it in more detail.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.