Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Visual Basic Programming

Visual Basic Programming Discussion forum for Visual Basic, an event driven programming language and associated development environment from Microsoft for its COM programming model.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-15-2008, 12:32 PM
Alhazred Alhazred is offline
Newbie
 
Join Date: Jul 2007
Posts: 23
Rep Power: 5
Alhazred is on a distinguished road
Default Decide what to do if a function succeed or not

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?
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-15-2008, 03:20 PM
TcM's Avatar   
TcM TcM is offline
Code Warrior
 
Join Date: Aug 2006
Location: In a technologic world :p
Posts: 8,332
Rep Power: 68
TcM is a glorious beacon of lightTcM is a glorious beacon of lightTcM is a glorious beacon of lightTcM is a glorious beacon of lightTcM is a glorious beacon of light
Default

ok try this code:

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

Last edited by TcM; 01-17-2008 at 10:09 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SecurityAudit vinay Visual Basic Programming 27 01-07-2008 01:14 PM
Managed C++ calling VB .net dll function pranab73 Managed C++ 0 12-25-2007 01:19 PM
multi-pass preprocessing kenna C and C++ 11 08-14-2007 11:45 AM
small help in this generator function plz SamehSpiky C and C++ 3 06-22-2007 12:47 PM


All times are GMT -5. The time now is 06:34 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads