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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading a saved file! 1

Status
Not open for further replies.

WastinAway

Technical User
Jun 21, 2004
31
0
0
US
I wrote a simple program that will tell me how much money out of paycheck goes where! BUt I came amongst a problem. Here is what happens. I enter a dollar ammount in a text box then hit calculate. it calculates 25% of and 75% of and puts it in seperate txtboxes. Then it takes the 75% txtbox and enters that ammount into a lstbox. After that amount is in the lstbox then I hit save. It saves that amount to a word document. after I save,I exit. When I run the progrma again it loads that amount from the word document into a new txtbox called running total. My problem is I want to add the data in the word document to the new total everytime one is entered so that I can keep a running total of what all is entered. SO I don't have to reenter all the info again. Thanks I hipe this is clear enough. HEre is what I have so far.





Private Sub cmdcalculate_Click()

Dim NetPay As Single, Percent As Single, Percent2 As Single

If IsNumeric(txtnet.Text) Then
NetPay = txtnet.Text
Percent2 = GetPercent2(NetPay)
Percent = GetPercent(NetPay)
txtdadper.Text = Format(Percent2, "currency")
txtpercent.Text = Format(Percent, "currency")
LstNetPay.AddItem Format$(Percent2, "currency")
DisplayTotal
Else
MsgBox "Number must be numeric", vbExclamation, "Input Error"
txtnet.Text = ""
txtnet.SetFocus
End If

End Sub

Private Sub cmdclear_Click()

txtnet.Text = ""
txtpercent.Text = ""
txtdadper.Text = ""
txtnet.SetFocus

End Sub


Private Function GetPercent(NetPay As Single) As Single

GetPercent = NetPay * 0.25

End Function




Private Sub DisplayTotal()

Dim counter As Integer
Dim total As Single

For counter = 0 To LstNetPay.ListCount - 1
total = total + LstNetPay.List(counter)
Next counter

txttotal.Text = Format$(total, "currency")

End Sub

Private Sub mnufileclear_Click()

LstNetPay.Clear
DisplayTotal

End Sub

Private Sub mnufileexit_Click()

Unload Me

End Sub

Private Function GetPercent2(NetPay As Single) As Single

GetPercent2 = NetPay * 0.75

End Function

Private Sub Save_Click()

Open "Running Total" For Append As #1
Print #1, txttotal.Text
Close #1

End Sub

Private Sub form_load()

Open "Running total" For Input As #1
Do Until EOF(1)
Line Input #1, a
txtruntotal = a
Loop
Close #1

End Sub
 
Ok, from what i'm getting here, you're having problems bringing in input from files.
I know that using a word document is nice, and I'm sorry to all the hardcores out there. However, going back to Visual Basic 101 seems like a simpler solution for this one.
I've modified the code and reposted it, hopefully it will help. If not, tell me where you think it's going wrong and i'll be happy to adjust.

Code:
Private Sub cmdcalculate_Click()

    Dim NetPay As Single

    If IsNumeric(txtnet.Text) Then
        NetPay = txtnet.Text
        'A few changes here eliminate lines of code and  
        'unneeded functions
        txtdadper.Text = Format(NetPay * 0.25, "currency")
        txtpercent.Text = Format(NetPay * 0.75, "currency")
        LstNetPay.AddItem txtpercent.txt
        Call DisplayTotal
    Else
        MsgBox "Input must be numeric", vbExclamation, "Input Error"
        'Here is probably better (for practical reasons)
        'to select the text, not delete it
        txtnet.SelText
        txtnet.SetFocus
    End If

End Sub

Private Sub cmdclear_Click()
    'Clears all txtboxes
    txtnet.Text = ""
    txtpercent.Text = ""
    txtdadper.Text = ""
    txtnet.SetFocus

End Sub

Private Sub DisplayTotal()

    Dim counter As Integer
    Dim total As Single
    
    'This may be a misunderstanding on my part, i thought
    'listboxes only held string values, so additions can be
    'made only after converting a number?
    For counter = 0 To LstNetPay.ListCount - 1
        'Takes leading spaces (if any) from the listbox entries
        'and converts to numbers
        total = total + Val(Trim(LstNetPay.List(counter)))
    Next counter

    txttotal.Text = Format$(total, "currency")

End Sub
    
Private Sub mnufileclear_Click()
    'Clears the Current File Contents?
    LstNetPay.Clear
    DisplayTotal

End Sub

Private Sub mnufileexit_Click()
    
    Unload Me
    
End Sub

Private Sub Save_Click()
    'use the FreeFile function avoids conflicts.
    '(May not apply here but is good practice)
    Dim FileNum As Integer
    FileNum = FreeFile
    
    Open App.Path & "runtotal.txt" For Append As #FileNum
        Print #FileNum, txttotal.Text
    Close #FileNum
    
End Sub

Private Sub form_load()
    'Declare Variables (Practice thing again :)
    Dim a As String
    
    Dim FileNum As Integer
    FileNum = FreeFile
    
    Open App.Path & "runtotal.txt" For Input As #FileNum
        Do Until EOF(FileNum)
            'This takes the last input to the file, no?
            Line Input #FileNum, a
            txtruntotal.Text = a
        Loop
    Close #1
    
End Sub



jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
I think the problem you've got is that when you're reading the values from your "word document" (most people would refer to the file you're using as a "text file"), you're not adding them together. Instead, you're putting each one into your txtruntotal text box and then writing over them again.

To get your program working so that txtruntotal will contain the running total of all the saved values in your text file, you should modify your Form_Load code as follows...

Code:
Private Sub form_load()

    Dim a As String
    Dim tempCurrency As Currency

    tempCurrency = 0
    
    Open "Running total" For Input As #1
    Do Until EOF(1)

        '' Read the line from the text file

        Line Input #1, a

        '' Convert the read string into
        '' a currency, and add it to a
        '' running total

        tempCurrency = tempCurrency + CCur(a)

    Loop
    Close #1

    '' Put the running total into the
    '' appropriate text box
    
    txtruntotal.Text = tempCurrency

End Sub
 
OK I used StevieK's procedure and it worked fine. BUt if there is no value in the textfile( Running Total ) then I get a error message. How can I correct this? Thanks for all the help!!!
 
Here is what I have and the bold part is where the error is at, thanks!


Private Sub cmdcalculate_Click()

Dim NetPay As Single, Percent As Single, Percent2 As Single

If IsNumeric(txtnet.Text) Then
NetPay = txtnet.Text
Percent2 = GetPercent2(NetPay)
Percent = GetPercent(NetPay)
txtdadper.Text = Format(Percent2, "currency")
txtpercent.Text = Format(Percent, "currency")
LstNetPay.AddItem Format$(Percent2, "currency")
DisplayTotal
Else
MsgBox "Number must be numeric", vbExclamation, "Input Error"
txtnet.Text = ""
txtnet.SetFocus
End If

End Sub

Private Sub cmdclear_Click()

txtnet.Text = ""
txtpercent.Text = ""
txtdadper.Text = ""
txtnet.SetFocus

End Sub


Private Function GetPercent(NetPay As Single) As Single

GetPercent = NetPay * 0.25

End Function




Private Sub DisplayTotal()

Dim counter As Integer
Dim total As Single

For counter = 0 To LstNetPay.ListCount - 1
total = total + LstNetPay.List(counter)
Next counter

txttotal.Text = Format$(total, "currency")

End Sub

Private Sub mnufileclear_Click()

LstNetPay.Clear
DisplayTotal

End Sub

Private Sub mnufileexit_Click()

Unload Me

End Sub

Private Function GetPercent2(NetPay As Single) As Single

GetPercent2 = NetPay * 0.75

End Function

Private Sub Save_Click()

Open "Running Total" For Append As #1
Print #1, txttotal.Text
Close #1

End Sub

Private Sub form_load()

Dim RunTotal As Currency
Dim a As String


Open "Running total" For Input As #1
Do Until EOF(1)
Line Input #1, a
RunTotal = RunTotal + CCur(a)
Loop
Close #1
txtruntotal.Text = Format(RunTotal, "currency")
End Sub

 
Here is what I have and the bold part is where the error is at, thanks!


Private Sub cmdcalculate_Click()

Dim NetPay As Single, Percent As Single, Percent2 As Single

If IsNumeric(txtnet.Text) Then
NetPay = txtnet.Text
Percent2 = GetPercent2(NetPay)
Percent = GetPercent(NetPay)
txtdadper.Text = Format(Percent2, "currency")
txtpercent.Text = Format(Percent, "currency")
LstNetPay.AddItem Format$(Percent2, "currency")
DisplayTotal
Else
MsgBox "Number must be numeric", vbExclamation, "Input Error"
txtnet.Text = ""
txtnet.SetFocus
End If

End Sub

Private Sub cmdclear_Click()

txtnet.Text = ""
txtpercent.Text = ""
txtdadper.Text = ""
txtnet.SetFocus

End Sub


Private Function GetPercent(NetPay As Single) As Single

GetPercent = NetPay * 0.25

End Function




Private Sub DisplayTotal()

Dim counter As Integer
Dim total As Single

For counter = 0 To LstNetPay.ListCount - 1
total = total + LstNetPay.List(counter)
Next counter

txttotal.Text = Format$(total, "currency")

End Sub

Private Sub mnufileclear_Click()

LstNetPay.Clear
DisplayTotal

End Sub

Private Sub mnufileexit_Click()

Unload Me

End Sub

Private Function GetPercent2(NetPay As Single) As Single

GetPercent2 = NetPay * 0.75

End Function

Private Sub Save_Click()

Open "Running Total" For Append As #1
Print #1, txttotal.Text
Close #1

End Sub

Private Sub form_load()

Dim RunTotal As Currency
Dim a As String


Open "Running total" For Input As #1
Do Until EOF(1)
Line Input #1, a
RunTotal = RunTotal + CCur(a)
Loop
Close #1
txtruntotal.Text = Format(RunTotal, "currency")
End Sub
 
Hi WastinAway,

In case you didn't manage to fix your error...

The line you highlighted line will not execute if the input file is empty, so I'm assuming your input file is corrupted or you have a blank line in it or something...

If you want to trap for specific errors then you're best bet is to generate a log file as you read the input file. This way you can simply let the user know that some error(s) occurred, and they can then match the logfile with the input file and try to fix the problem.

Since you already know how to generate text files, I'll just show you how to catch the error: To trap the error you mentioned, and let the user know that something went wrong, you could do something like the following:

Code:
Private Sub form_load()

    Dim a As String
    Dim tempCurrency As Currency
    Dim bErrorsOccurredWhileReadingInput As Boolean

    tempCurrency = 0
    
    Open "\Running total" For Input As #1
    
    Do Until EOF(1)

        '' Read the line from the text file

        Line Input #1, a

        '' If error occur, simply continue

        On Error Resume Next

        '' Convert the read string into
        '' a currency, and add it to a
        '' running total

        tempCurrency = tempCurrency + CCur(a)
        
        If Err.Number <> 0 Then

            '' We were unable to convert the
            '' input, so remember that at
            '' least one error has occurred.
        
            bErrorsOccurredWhileReadingInput = True

            '' Clear the error status

            Err.Clear
            
        End If
        
        On Error GoTo 0

    Loop
    Close #1

    '' Put the running total into the
    '' appropriate text box
    
    txtRunningTotal.Text = tempCurrency

    '' If we detected any errors while reading
    '' the input, then let the user know but
    '' continue on as best we can.
    
    If bErrorsOccurredWhileReadingInput Then
        Call MsgBox( _
           "WARNING!" & _
           vbNewLine & vbNewLine & _
           "Errors occurred while reading " & _
           "in the previous payments from " & _
           "file. Your account information " & _
           "may no longer be accurate.", _
           vbCritical, _
           "Error reading payment details" _
        )
    End If

End Sub

Stephen King
Systems Developer and Support Analyst
Infoplex Ltd.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top