Welcome to Dream.In.Code
Become a VB Expert!

Join 149,909 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,191 people online right now. Registration is fast and FREE... Join Now!




How to decide what to do if a sub succeds or not?

 
Reply to this topicStart new topic

How to decide what to do if a sub succeds or not?

Alhazred
15 Jan, 2008 - 09:38 AM
Post #1

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 29


My Contributions
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 by VerifyFile?
CODE

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

User is offlineProfile CardPM
+Quote Post

tody4me
RE: How To Decide What To Do If A Sub Succeds Or Not?
15 Jan, 2008 - 09:41 AM
Post #2

Only Jenny Craig makes thin clients...
Group Icon

Joined: 12 Apr, 2006
Posts: 1,349



Thanked: 5 times
Dream Kudos: 100
My Contributions
Simple, you don't have a way to return a value back from a sub in VB. You have to make it a function, and have it return a type of variable, such as boolean.

This post has been edited by tody4me: 15 Jan, 2008 - 09:41 AM
User is offlineProfile CardPM
+Quote Post

Banned
RE: How To Decide What To Do If A Sub Succeds Or Not?
15 Jan, 2008 - 11:16 AM
Post #3

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 12


My Contributions
Alhazred:
tody4me its right..
QUOTE(tody4me @ 15 Jan, 2008 - 10:41 AM) *

... You have to make it a function, and have it return a type of variable, such as boolean.


Why dont u use a function instead of a sub??

Anyway if you still dont wanna use a function declare a public variable (in a module or in the form are u gonna using it) and change the value according with your needs.

There's no other way...
crazy.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 01:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month