Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you load a saved file?

Status
Not open for further replies.

WastinAway

Technical User
Jun 21, 2004
31
US
I can get the file I want loaded into the textbox. But what I am trying to do is: Everytime I save the data in my program it goes to a certain file. But when I exit out of the program and come back in, it only brings back the latest entry. I want it to take the latest amount and add it to the ammounts saved in the file. Then give me a new total. How would I go about doing this? I hope I was descriptive enough!
 
I'm not completely sure I understand what you're trying to do but here's a functions that writes to a text file. If what you want is add contents to the file/without replacing it, then you must specify bAppend as True. Here it is (take a look a the lines in bold):
Code:
[COLOR=darkblue]Public Sub[/color] WriteToTextFile(_
       sContents [COLOR=darkblue]As String[/color], _
       sFile [COLOR=darkblue]As String[/color], _
       [COLOR=darkblue]Optional[/color] bAppend [COLOR=darkblue]As Boolean[/color])

  [COLOR=darkblue]Dim[/color] iFileNum [COLOR=darkblue]As Integer[/color]
  [COLOR=darkblue]Dim[/color] bIsOpen [COLOR=darkblue]As Boolean[/color]
    
  [COLOR=darkblue]On Error GoTo[/color] Err_Handler
     
  bIsOpen = [COLOR=darkblue]False[/color]
  iFileNum = FreeFile() 
     
  [b][COLOR=darkblue]If[/color] bAppend [COLOR=darkblue]Then
    Open[/color] sFile [COLOR=darkblue]For Append As[/color] #iFileNum
  [COLOR=darkblue]Else
    Open[/color] sFile [COLOR=darkblue]For Output As[/color] #iFileNum
  [COLOR=darkblue]End If[/color][/b]
     
  [COLOR=green]' If execution flow is here, 
  ' the file opened OK[/color]
  bIsOpen = [COLOR=darkblue]True[/color]
     
  [COLOR=darkblue]Print[/color] #iFileNum, sContents
     
  [COLOR=green]' Purposely go into the error handler 
  ' to close the file[/color]
Err_Handler:
  [COLOR=darkblue]If[/color] bIsOpen [COLOR=darkblue]Then[/color]
    [COLOR=darkblue]Close[/color] #iFileNum
  [COLOR=darkblue]End If[/color]
     
  [COLOR=darkblue]If[/color] Err [COLOR=darkblue]Then[/color]
    Err.Raise Err.Number, , Err.Description
  [COLOR=darkblue]End If[/color]
[COLOR=darkblue]End Sub[/color]
Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top