Results 1 to 3 of 3

Thread: problme with array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    New Jersey
    Posts
    334
    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.

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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 )

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    New Jersey
    Posts
    334

    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
  •  



Click Here to Expand Forum to Full Width