Jump to content

Decide what to do if a function succeed or not

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Alhazred

Alhazred

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
My little application has a text field and 2 bottons.
Writing a passowrd and clicking on "Avvia" my application checks if the password is correct, if it is looks for the file "test.txt", if the file exists it is renamed as test.exe and loaded.
The problem comes if the file doesn't exists, my application gives correctly an error message as you can read in VerifyFile sub, but then it goes on with the line that try to rename the file and an error occurs closing my application as I press "OK".
How can I say to Avvia_Click() to do nothing if the file has not been found?

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Public Sub VerifyFile(FileName As String)

  On Error Resume Next


  Open FileName For Input As #1 'Apre lo specifico file

  If Err Then

    MsgBox ("File " & FileName & " not found")

    Exit Sub

  End If

  Close #1

End Sub



Private Sub Avvia_Click()

  Dim a


  If (Text1.Text = "password") Then

    Call VerifyFile("test.txt")

    Name ".\test.txt" As ".\test.exe"

    Sleep 3000

    a = Shell(".\test.exe", 1)

  End If

  

End Sub



Private Sub Chiudi_Click()

  Name ".\test.exe" As ".\test.txt"

End Sub



#2
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
ok try this code:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim CanOpenFile As Integer
Public Sub VerifyFile(FileName As String)
  On Error GoTo Err

  Open FileName For Input As #1 'Apre lo specifico file
  Close #1
  CanOpenFile = 1
  Exit Sub
Err:
    MsgBox ("File " & FileName & " not found")
    CanOpenFile = 0
End Sub


Private Sub Avvia_Click()
Dim a
  If (Text1.Text = "password") Then
    Call VerifyFile("test.txt")
    If CanOpenFile = 1 Then
    Name ".\test.txt" As ".\test.exe"
    Sleep 3000
    a = Shell(".\test.exe", 1)
    End If
  End If
  
End Sub


Private Sub Chiudi_Click()
  Name ".\test.exe" As ".\test.txt"
End Sub

The only problem is the shell command you should fix that as it's wrong, but I fixed your problem. You can improve the code, I just gave you the idea.

If It did help please don't forget to add +rep to my post, Thanks