 |
 |

03-29-2003, 10:12 PM
|
|
Freshman
|
|
Join Date: Mar 2003
Posts: 25
|
|
File Problems: Files Not Closing etc.
|
My problem is this: I have a login form, it reads 2 files, each file is a different login. If I login using file 1 user/pwd and then logout back to login screen and try to log in again to same form with same file, it say: File Already Open. Same thing for both logins to both forms with both files.
No someone suggested put close right after the open of the file which worked for logining in and out repeadidly with the first file/form but with the second form/login/file it says: bad file name/ file not found
so I switched back to my original format, with the original problem.
here's the code:
'Holds user response from message box
Dim intResponse As Integer
'Holds current index in the file
Dim intIndex As Integer
'Holds the number of records in the file
Dim intNumberRecords As Integer
'Holds the password from the file
Dim strPwd As String
'Holds the username from the file
Dim strUser As String
'Holds value to determine whether user/pwd are found
Dim intFound As Integer
'on an error go to HandleErrors
On Error GoTo HandleErrors
'If the user/password text boxes are not blank do the following
If txtUser.Text <> "" And txtPwd.Text <> "" Then
'Open the file login.txt from the current path for input
Open App.Path & "\login.txt" For Random As #1 Len = Len(gudtUserLogin)
'determine the size of the file
intNumberRecords = LOF(1) / Len(gudtUserLogin)
'For the first record in the file to the end of the file
For intIndex = 1 To intNumberRecords
'Get the current indexed record and put it in the userdefined type
Get #1, intIndex, gudtUserLogin
'Put the UDT pwd and username fields into the appropriate variables
strPwd = gudtUserLogin.strPassword
strUser = gudtUserLogin.strUserName
'If the Password and User name match up with the current user name
'and password read in from the file load the main form and unload
'this form
If Trim(UCase(txtPwd.Text)) = Trim(UCase(strPwd)) And _
Trim(UCase(txtUser.Text)) = Trim(UCase(strUser)) Then
frmAdminDatabase.Show
Unload Me
Exit Sub
End If
'If no user/pwd match found set found to no
intFound = 0
Next intIndex
Close #1
'Close the file
'Open the file login2.txt from the current path for input
Open App.Path & "\login2.txt" For Random As #2 Len = Len(gudtUserLogin)
'determine the size of the file
intNumberRecords = LOF(2) / Len(gudtUserLogin)
'For the first record in the file to the end of the file
For intIndex = 1 To intNumberRecords
'Get the current indexed record and put it in the userdefined type
Get #2, intIndex, gudtUserLogin
'Put the UDT pwd and username fields into the appropriate variables
strPwd = gudtUserLogin.strPassword
strUser = gudtUserLogin.strUserName
'If the Password and User name match up with the current user name
'and password read in from the file load the main form and unload
'this form
If Trim(UCase(txtPwd.Text)) = Trim(UCase(strPwd)) And _
Trim(UCase(txtUser.Text)) = Trim(UCase(strUser)) Then
frmUser.Show
Unload Me
Exit Sub
End If
'If no user/pwd match found set found to no
intFound = 0
Next intIndex
Close #2
'Close the file
'If a pwd/username match not found display an invalid login message and
'reset the focus to the username text box
If intFound = 0 Then
MsgBox "You entered an invalid login", vbOKOnly, "Invalid Login"
txtUser.SetFocus
End If
'If either text box was left blank send an error message
Else
MsgBox "You must enter a Username and Password", vbOKOnly, "Invalid Entry"
txtUser.SetFocus
End If
'Exit the sub procedure if no errors occured
Form_Load_Exit:
Exit Sub
HandleErrors:
'If error 71 occurs, display msgbox error message informing user to put disk in
'and prompt for a response
If Err.Number = 71 Then
intResponse = MsgBox("No Disk in Drive", vbOKCancel, "Error: Disk")
'If ok resume program run from where the error occured
If intResponse = vbOK Then
Resume
'If they cancel exit the program
Else
mnuFileExit_Click
End If
'Raise any other errors that might have occured
Else
Err.Raise Err
End If
End Sub
|
|

03-29-2003, 10:20 PM
|
|
Newcomer
|
|
Join Date: Mar 2003
Posts: 24
|
|
|
hmm you have to also close the files before every exit sub if you don't intend to read from it later on, at least before the exit sub which shows the user form and unloads the login.
|
__________________
http://www.perseus-game.net
|

03-29-2003, 10:22 PM
|
|
Freshman
|
|
Join Date: Mar 2003
Posts: 25
|
|
Thank you very much,
appreciate it!! 
|
|

03-29-2003, 10:25 PM
|
 |
Junior Contributor
|
|
Join Date: Feb 2003
Location: Philippines
Posts: 336
|
|
I think you have Exited the Sub without closing the file:
If Trim(UCase(txtPwd.Text)) = Trim(UCase(strPwd)) And _
Trim(UCase(txtUser.Text)) = Trim(UCase(strUser)) Then
frmAdminDatabase.Show
Unload Me
Exit Sub <-------- Exits the sub while File #1 is not yet closed
End If
'If no user/pwd match found set found to no
intFound = 0
Next intIndex
Close #1
'Close the file
I'm not sure if I am right 
(hehe I'm 5 secs late  )
|
|

03-29-2003, 10:42 PM
|
|
Freshman
|
|
Join Date: Mar 2003
Posts: 25
|
|
|

03-29-2003, 11:34 PM
|
|
Contributor
|
|
Join Date: Mar 2002
Location: Essex, UK
Posts: 690
|
|
Could I suggest a function? It looks like you're repeating code, which is designed to work something out. The same block of code is used to open a text file, check to see if it contains login information, and then carry on. It seems a prime candidate to become a function...
Code:
Private Function isLoginValid(as_file As String) As Boolean
'Open the file login.txt from the current path for input
Open App.Path & "\" & as_file & ".txt" For Random As #1 Len = Len(gudtUserLogin)
'determine the size of the file
intNumberRecords = LOF(1) / Len(gudtUserLogin)
'For the first record in the file to the end of the file
For intIndex = 1 To intNumberRecords
'Get the current indexed record and put it in the userdefined type
Get #1, intIndex, gudtUserLogin
'If the Password and User name match up with the current user name
'and password read in from the file load the main form and unload
'this form
If Trim(UCase(txtPwd.Text)) = Trim(gudtUserLogin.strPassword) And _
Trim(UCase(txtUser.Text)) = Trim(gudtUserLogin.strUser) Then
isLoginValid = True
Close #1
Exit Function
End If
Next intIndex
Close #1
End Function
If you use this function like this:
Code:
If isLoginValid("login") = True Then
frmAdminDatabase.Show
else
If isLoginValid("login2") = True Then frmUser.Show
end if
Aah, so much easier - less code to debug! 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|