Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > File Problems: Files Not Closing etc.


Reply
 
Thread Tools Display Modes
  #1  
Old 03-29-2003, 10:12 PM
Templeton Peck Templeton Peck is offline
Freshman
 
Join Date: Mar 2003
Posts: 25
Default 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
Reply With Quote
  #2  
Old 03-29-2003, 10:20 PM
XenonMB XenonMB is offline
Newcomer
 
Join Date: Mar 2003
Posts: 24
Default

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
Reply With Quote
  #3  
Old 03-29-2003, 10:22 PM
Templeton Peck Templeton Peck is offline
Freshman
 
Join Date: Mar 2003
Posts: 25
Default

Thank you very much,

appreciate it!!
Reply With Quote
  #4  
Old 03-29-2003, 10:25 PM
starbitz's Avatar
starbitz starbitz is offline
Junior Contributor
 
Join Date: Feb 2003
Location: Philippines
Posts: 336
Default

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 )
Reply With Quote
  #5  
Old 03-29-2003, 10:42 PM
Templeton Peck Templeton Peck is offline
Freshman
 
Join Date: Mar 2003
Posts: 25
Default

Your both right, thanks
Reply With Quote
  #6  
Old 03-29-2003, 11:34 PM
Chris J Locke Chris J Locke is offline
Contributor
 
Join Date: Mar 2002
Location: Essex, UK
Posts: 690
Default

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!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation Problem - PLs help urgenlty dpdsouza Installation / Documentation 4 12-02-2004 07:09 PM
Doesn't want to register! MikeyM Installation / Documentation 5 03-02-2003 08:22 PM
Characters!!! ericgamer47 Game Programming 51 09-14-2002 04:23 PM
Required files burningodzilla General 11 09-12-2001 04:51 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->