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!

Error Message!! 2

Status
Not open for further replies.

WastinAway

Technical User
Jun 21, 2004
31
0
0
US
I can't get my program to run with these to commands in this script together. What can I do to allow it to run with them both in it? here is what I have, thanks! I have to take [Private Sub form_load()] out before the program will run.




Private Sub Save_Click()

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

Open "Running Total 2" For Append As #2
Print #2, txttotal2.Text
Close #2

Open "Tax Total" For Append As #3
Print #3, txttax.Text
Close #3

End Sub

Private Sub form_load()

Dim RunTotal As Single
Dim RunTotal2 As Single
Dim taxtotal As Single
Dim a As String
Dim b As String
Dim c As String



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 ''
RunTotal = RunTotal + CCur(a)
Loop
Close #1
'' Put the running total into the appropriate text box ''
txtruntotal.Text = Format(RunTotal, "currency")



Open "Running Total 2" For Input As #2
Do Until EOF(2)
Line Input #2, b
RunTotal2 = RunTotal2 + CCur(b)
Loop
Close #2
txtruntotal2.Text = Format(RunTotal2, "Currency")


Open "Tax Total" For Input As #3
Do Until EOF(3)
Line Input #3, c
taxtotal = taxtotal + CCur(c)
Loop
Close #3
txttaxtotal.Text = Format(taxtotal, "Currency")



End Sub
 
Hi:

This may be a long shot, but I have had problems in the past with populating controls within the Form_Load sub. I have moved the code to the Form_Activate sub, enclosing the code within an If statement to limit the running of the code to the first call of Form_Activate.

For example:
Code:
Public Sub Form_Activate()
    Static blnFirstRun as Boolean

    . . . 

    If Not blnFirstRun Then
        ' Add your code from the Form_Load sub.
        blnFirstRun = True
    End If

End Sub

HTH,
Cassandra
 
That still gave me aerror message! anymore ideas would be appreciated. Just for verification I would have left the save_click() part alone right?
 
Hi to all,

may I ask what error message appears when the program is run ?
 
Perhaps telling us what error you are getting might be a start.

Maybe you could add a second button to the form and put the code in the form load event in there as a test then run and press the buttons in order>

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
The error message reads: run-time error '53' file not found.
 
Reason is when you run the program it changes the current path... DLL's are run under the path WINNT(etc)\System32
Not sure about VBP and EXE's...

What I would do is either Imply the path or set it to the applicatiosn directory....

i.e.
Code:
 Open App.Path & "\Running Total" For Input As #1
Or
Code:
 Open "C:\MyFolder\Running Total" For Input As #1

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
I'm not answering your question here, but I just wanted to say that it is good practice to use the FreeFile() function instead of hardcoding the file numbers.
 
Also Maybe your having issues with spaces in the file name. Try replacing those with _

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
But It won't let me create those files. That is the problem I am having. My error message is involved with a crossing of the two commands. It try's to load files that don't exist. How can I get the progrma to create those files when the program is run outside of VB for the first time? Thanks
 
Ahhh then Esquared was on to something... try this...
Code:
Private Sub Save_Click()
    Dim fn1 As Integer
    Dim fn2 As Integer
    Dim fn3 As Integer

    fn1 = FreeFile
    Open "Running Total" For Append As #fn1
        Print #fn1, txttotal.Text
    Close #fn1
    
    fn2 = FreeFile
    Open "Running Total 2" For Append As #fn2
        Print #fn2, txttotal2.Text
    Close #fn2
    
    fn3 = FreeFile
    Open "Tax Total" For Append As #fn3
        Print #fn3, txttax.Text
    Close #fn3
    
End Sub

Private Sub form_load()

    Dim RunTotal As Single
    Dim RunTotal2 As Single
    Dim taxtotal As Single
    Dim a As String
    Dim b As String
    Dim c As String
    Dim fn1 As Integer
    Dim fn2 As Integer
    Dim fn3 As Integer
    
    fn1 = FreeFile
    Open "Running Total" For Input As #fn1
        Do Until EOF(fn1)
            '' Read the line from the text file''
            Line Input #fn1, a
            '' Convert the read string into a currency, and add it to a running total ''
            RunTotal = RunTotal + CCur(a)
        Loop
    Close #fn1
    '' Put the running total into the appropriate text box ''
    txtruntotal.Text = Format(RunTotal, "currency")
    
    
    
    fn2 = FreeFile
    Open "Running Total 2" For Input As #fn2
        Do Until EOF(fn2)
        Line Input #fn2, b
        RunTotal2 = RunTotal2 + CCur(b)
        Loop
    Close #fn2
    txtruntotal2.Text = Format(RunTotal2, "Currency")
         
    
    fn3 = FreeFile
    Open "Tax Total" For Input As #fn3
        Do Until EOF(fn3)
        Line Input #fn3, c
        taxtotal = taxtotal + CCur(c)
        Loop
    Close #fn3
    txttaxtotal.Text = Format(taxtotal, "Currency")
         
    
  
End Sub

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
It's as simple as this:

Trying to open for reading a file that doesn't exist results in an error, Run-time error #53: File not found.

You can either trap on this error and respond to the fact that the file is empty, or you can open it briefly for append which will create it if it doesn't exist.

Assuming there is no file "c:\test.txt" the following will give this error:

[tt] Open "c:\test.txt" For Input As #1
Close #1[/tt]

but this will not generate an error

[tt] Open "c:\test.txt" For Append As #1
Close #1
Open "c:\test.txt" For Input As #1
Close #1[/tt]
 
ESquared, that didn't work either unless I didn't do what you said. Here is what I thought you were talking about. The bold line is where the error message shows up when I run the program in VB. It will not create those files I need. When I run the program it tries to load the files, But the files do not exist yet. IS there a way to have the program create the files for me. So I don't have to create them. Plus I have to take the Load procedure out to even creat the files. Thanks for the help!!!





Private Sub Save_Click()

Open "c:\Running Total.txt" For Append As #1
Print #1, txttotal.Text
Close #1

Open "c:\Running Total 2.txt" For Append As #2
Print #2, txttotal2.Text
Close #2

Open "c:\Tax Total.txt" For Append As #3
Print #3, txttax.Text
Close #3

End Sub
Private Sub form_load()

Dim RunTotal As Single
Dim RunTotal2 As Single
Dim taxtotal As Single
Dim a As String
Dim b As String
Dim c As String



Open "c:\Running Total.txt" 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 ''
RunTotal = RunTotal + CCur(a)
Loop
Close #1
'' Put the running total into the appropriate text box ''
txtruntotal.Text = Format(RunTotal, "currency")



Open "c:\Running Total 2.txt" For Input As #2
Do Until EOF(2)
Line Input #2, b
RunTotal2 = RunTotal2 + CCur(b)
Loop
Close #2
txtruntotal2.Text = Format(RunTotal2, "Currency")


Open "c:\Tax Total.txt" For Input As #3
Do Until EOF(3)
Line Input #3, c
taxtotal = taxtotal + CCur(c)
Loop
Close #3
txttaxtotal.Text = Format(taxtotal, "Currency")



End Sub
 
Code:
Private Sub form_load()

    Dim RunTotal As Single
    Dim RunTotal2 As Single
    Dim taxtotal As Single
    Dim a As String
    Dim b As String
    Dim c As String
    
 [COLOR=red] on error goto ErrorHandler  [/color]
    
    Open "c:\Running Total.txt" 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 ''
            RunTotal = RunTotal + CCur(a)
        Loop
    Close #1
    '' Put the running total into the appropriate text box ''
    txtruntotal.Text = Format(RunTotal, "currency")
    
    
    
    Open "c:\Running Total 2.txt" For Input As #2
        Do Until EOF(2)
        Line Input #2, b
        RunTotal2 = RunTotal2 + CCur(b)
        Loop
    Close #2
    txtruntotal2.Text = Format(RunTotal2, "Currency")
         
    
    Open "c:\Tax Total.txt" For Input As #3
        Do Until EOF(3)
        Line Input #3, c
        taxtotal = taxtotal + CCur(c)
        Loop
    Close #3
    txttaxtotal.Text = Format(taxtotal, "Currency")
[COLOR=red]exit sub
Errorhandler:

'eeep we've had an error
'set our text boxes to zero values
'and continue

  txtruntotal.Text = Format(0, "currency")
  txtruntotal2.Text = Format(0, "Currency")
  txttaxtotal.Text = Format(0, "Currency")

[/color red]
    
  
End Sub

Add the bits in red!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Matt: You're assuming that if the first file is missing, the others are, also.

WastinAway: You didn't do what I said.

Code:
[red]Open "c:\Running Total.txt" For Append As #1
Close #1 ' Creates the file if it is missing[/red]
Open "c:\Running Total.txt" 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 ''
            RunTotal = RunTotal + CCur(a)
        Loop
    Close #1
    '' Put the running total into the appropriate text box ''
    txtruntotal.Text = Format(RunTotal, "currency")

Alternately:

Code:
[red]On Error Resume Next[/red]
Open "c:\Running Total.txt" For Input As #1
[red]If Err.Number = 0 Then[/red]
        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 ''
            RunTotal = RunTotal + CCur(a)
        Loop
    Close #1
    '' Put the running total into the appropriate text box ''
    txtruntotal.Text = Format(RunTotal, "currency")
End If
[red]On Error Goto 0 'restore default error handler[/red]

This is the simplest way I can see to handle file not found for each instance. You might want to check and see if it is really Error 53 and if not, do On Error Goto 0 and then Resume.

-------------------------------------
A sacrifice is harder when no one knows you've made it.
 
of course I missed the End If in red. :)

-------------------------------------
A sacrifice is harder when no one knows you've made it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top