|
-
Apr 7th, 2000, 05:44 PM
#1
Thread Starter
Hyperactive Member
Hello,
I seem to be having a problem with a dynamic variable array.
I am trying to read usernames and their corresponding passwords from a text file, but the array only loads the first pair. The highest subscript I reach (after doing tests), is 0.
This is the code I used for the whole Login form:
Code:
Dim username(), password() As String
Public LoginSucceeded As Boolean
Dim i
Dim LoggedUser As String
Private Sub cmdCancel_Click()
LoginSucceeded = False
End
End Sub
Private Sub cmdOK_Click()
i = 0
For i = LBound(username) To UBound(username)
If txtUserName = username(i) And txtPassword = password(i) Then
LoggedUser = username(i)
MsgBox "You have successfuly logged in!"
frmChat.Show
Me.Hide
GoTo theline
Else
MsgBox "Invalid username and/or password!"
End
End If
Next
theline: End Sub
Private Sub Form_Load()
i = 0
ReDim username(i)
ReDim password(i)
Open "D:\VB Stuff\Users.txt" For Input As #2
Do While Not EOF(2)
ReDim Preserve username(i)
ReDim Preserve password(i)
Input #2, username(i), password(i)
i = i + 1
Loop
Close #2
End Sub
Please help me. Thanks.
-
Apr 7th, 2000, 06:44 PM
#2
PowerPoster
Yre you sure that you've written the name and password into the file so you can read it? I think you have to seperate them by "," or <Tab>. Anyway, try this:
Code:
'Your text file
user1, password1
nextuser, nextpassword
testuser, hello
And update your project with this:
Code:
'Instead of declare username() and password() put this into a module:
Type tUser
UserName as String
Password as String
End Type
Global User() as tUser
Global UserCount as Long
'And use this function to load them:
Public Sub LoadAccounts(iFileName as String)
Dim A as Long
Dim FileNumber as Long
'Check if file exists
If Dir(iFileName) = "" Then
Exit Sub
EndIf
'Read accounts
FileNumber = FreeFile
Open iFileName For Input As #FileNumber
While not EOF(FileNumber)
UserCount = UserCount + 1
Redim Preserve User(UserCount)
Input #FileNumber, User(UserCount).UserName, User(UserCount).Password
Wend
Close #FileNumber
OK, hope his helps (not, I'm sure )
-
Apr 7th, 2000, 09:01 PM
#3
Thread Starter
Hyperactive Member
thanks
Hi again,
Thanks for your help. I will try to see if it works. In the mean time, I figured out that the array doesn't get 'messed up' when loading the items, but rather when I try to compare the username and password. I know that I divided the passwords and usernames properly (with quotation marks). Have a look at the following code:
Code:
For i = LBound(username) To UBound(username)
If txtUserName = username(i) And txtPassword = password(i) Then
LoggedUser = username(i)
MsgBox "You have successfuly logged in!"
frmChat.Show
Me.Hide
'Do Some More Things...
Else
MsgBox "Invalid username and/or password!"
End
End If
Next
That seems to be where the problem is.
Thanks again for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|