File Checking Question

almostsane
05-15-2003, 07:42 PM
Hi...

I'm Making a Exam making program for my HSC major work, and i've run into an issue..

My program has multiple user accounts, with each user's information saved in a file called "username.eef". when they log-on it opens there file to access there information and load it into an array.

If the user inputs an invalid username, they get the "Runtime Error: '53' - File Not Found" messagebox. i was wondering if there is anyway to check in a folder if there is a file??

eg i want to check for the "username.eef" is there for the username they entered is located in the folder, that way it checks for the file, before attempting to open it.

this way if they enter an invalid username, it will say "Invalid Username" insted of "File Not Found"

i can do this by having an index of usernames, and checking it, but that leaves it open for curtain errors.

thank you for your help

DrPunk
05-16-2003, 05:12 AM
Irotallic gave quite a nice answer to this problem here (http://www.visualbasicforum.com/t77532.html)

Another way to do it would be to catch the error (i.e. On Error Goto X) and check for Error 53. If that's the error you know that the user doesn't exist.

almostsane
05-17-2003, 09:24 PM
Irotallic gave quite a nice answer to this problem here (http://www.visualbasicforum.com/t77532.html)

Another way to do it would be to catch the error (i.e. On Error Goto X) and check for Error 53. If that's the error you know that the user doesn't exist.

can you give me a more detailed example... just i don't have MSDN, and can't look up help on error handling... and the online MSDN is crappy

pinster
05-18-2003, 12:44 AM
It's very simple. You can use DIR$ function to check the availability of the file. Something like this:


Public Function isAvailable(sFile As String) as boolean
'sFile is the full path of the file you want to check eg. C:\username.eef
if len(Dir$(sFile)) > 0 then
isAvailable = true 'the file exists
else
isAvailable = false 'the file doesn't exists
end if
end function


Otherwise, you can also use error trapping. Something like this:


On error goto ErrHandler
...
...
ErrHandler:
if err.number = 53 then
msgbox "File doesn't exists!"
else
msgbox err.description
end if

almostsane
05-18-2003, 05:05 AM
it's fixed...

userpath = App.Path + "\users\" + txtUsername.Text + ".eef"
If Len(Dir(userpath)) <> 0 Then 'checks if userfile exists
Open userpath For Input As #1 'opens the UserFile
'MsgBox (userpath)<-- error checking
'loads contents of Userfile into the array "Info"
Do Until EOF(1)
ReDim Preserve info(x + 1)
Input #1, info(x)
x = x + 1
Loop

'MsgBox (info(1) + Str(x)) <-- error checking
Pass = info(1) 'gets password from array
acc = info(2) 'gets account type from array
'test to see if it worked
If txtPassword.Text = Pass Then
' checks if user is an ADMIN
If acc = "ADMIN" Then
Unload Me 'unloads this form
frmAmain.Visible = True 'if they are, load admin main
Else
Unload Me 'unloads this form
frmUmain.Visible = True ' if they arn't, load user main
End If
Else
MsgBox ("Invalid Password, Please try again") 'Tells User to input correct Password
End If

Else
MsgBox ("Invalid Username, Pease try again") 'prompts user to input correct username
End If

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum