Iv used arrays to store usernames
Iv stored the array data in a procedure called "Usernames"

But im wondering if its possible to then use the arrays to do a crosscheck validation for a login.

Iv done some extent of research and Im either typng it wrong...Or I dont know what im trying to say.

I have this so far:

Option Explicit
'Declares an array to store the names for login
Dim arrNames(3) As String

Private Sub Usernames()
'Assigns names to array spaces
arrNames(0) = "BNSjon"
arrNames(1) = "BNSjack"
arrNames(2) = "BNSjane"
arrNames(3) = "BNSjill"
End Sub

Private Sub Form_Load()
'Disables password box
Call Disable_Passwordbox
Dim varUsername As String
Dim varPassword As String
varUsername = txtUsername.Text
varPassword = txtPassword.Text
End Sub

Im not sure if I need all that is in the last procedure there, but its what i have so far. Some help please. If it cant be done using arrays then let me know a different way to get it done.

Thanks Much
-Russell aka Rabbit

Private Sub Form_Load()
'Disables password box
Call Disable_Passwordbox
Dim varUsername As String
Dim varPassword As String
varUsername = txtUsername.Text
varPassword = txtPassword.Text
End Sub

In the first place, your variables varUserName and varPassword only have scope or a lifetime during the Form_load() procedure. As soon as the form is finished loading, those variables are history.

To check passwords, you need at least two items to check against: (1) The user, and (2) the password. Your array only stores the name. You might want a two dimensional array declared at the beginning of your form:

Option Explicit
Private ArrayNames(3,1) as String

Then you could assign a password in the Array that could easily be recalled later

Private Sub Form_Load()
    strNames(0, 0) = "Joe"
    strNames(0, 1) = "JoePass"

    strNames(1, 0) = "Jeff"
    strNames(1, 1) = "JeffPass"
    
    ' etc. ............
End Sub

Thank you SO MUCH for replying hkdani!
I really do appreciate it.

So I do believe I understand the steps to take so far...

So I create a 2dimensional array to store name and password.
So the validation code would look something like:

If varPassword = strNames(0,1) then
Unload Me
Else
MsgBox "so on so forth"
End If

Right? Im about to try the code now, thank you very much for your help.

My sincerest of thanks

-Russell aka Rabbit

Sigh, I tried, been trying since I posted last. It seems as if its not reading the array. Or I could not be entering it right....

This is what I have so far:

Option Explicit
'Declares an array to store the names for login
    Dim arrNames(3, 1) As String
    Dim strUsername As String
    Dim strPassword As String

Private Sub Form_Load()
'Disables password box
    Call Disable_Passwordbox
    
'Turns Main window "off"
    Call Main_OFF

Private Sub Usernames_Passwords()
    
'Assigns names to array space 1
        arrNames(0, 0) = "bnsjon"
        arrNames(1, 0) = "bnsjack"
        arrNames(2, 0) = "bnsjane"
        arrNames(3, 0) = "bnsjill"
        
'Assigns Passwords to array space 2
        arrNames(0, 1) = "jon"
        arrNames(1, 1) = "jack"
        arrNames(2, 1) = "jane"
        arrNames(3, 1) = "jill"
End Sub

Private Sub txtUsername_Validate(Cancel As Boolean)
strUsername = txtUsername.Text
'Username Check
    If strUsername <> arrNames(0, 0) Or strUsername = "" Then
        MsgBox "Incorrect Username!", vbOKOnly + vbExclamation, "INCORRECT!"
        txtUsername.SetFocus
    Else
        If strUsername = arrNames(0, 0) Then
            Call Enable_Passwordbox
            txtPassword.SetFocus
        End If
    End If
Cancel = False
End Sub

What I want to happen is that when I run the validation, it checks what the user entered against what is already in the array.
but it just keeps giving me the message box, as if its totally disregarding everything else once I click OK.

ANy help would be appreciated, thanks in advance

-Russell aka Rabbit

You need to loop through your array...

Dim FoundPass As Boolean, FoundName As Boolean, ForLoopCounter As Integer, UpperBoundOfArray As Integer
UpperBoundOfArray = UBound(ArrNames)
For ForLoopCounter = 0 To UpperBoundOfArray
  If Trim(strUsername) = Trim(ArrNames(ForLoopCounter, 0)) Then
    FoundName = True
    If Trim(strPassWord) = Trim(ArrNames(ForLoopCounter, 1)) Then
      FoundPass = True
      Exit For
    End If
  End If
Next ForLoopCounter
If FoundName = True And FoundPass = True Then
  'validation successful for both
Else
  'something missing
  If FoundName = False Then
    'msgbox...
  ElseIf FoundPass = False Then
    'msgbox...
  End If
End If

Now, while I check for both, you could break the loop up and the following if to put each part in the validate event for each control, or as I have tried to show you could put this under the command button's click event.

Good Luck

Thanks for replying vb5prgrmr

Can you explain a couple lines to me?
Why do I need to do that? And what are those two supposed to do?

ForLoopCounter As Integer, UpperBoundOfArray As Integer

I understand what the code is trying to do here, but I dont think I understand why you used those specific codes
So what does the part I highlighted in red do?

If Trim(strUsername) = Trim(ArrNames(ForLoopCounter, 0)) Then
    FoundName = True

I get the assigning of values here, but what does this mean?

UpperBoundOfArray = UBound(ArrNames)

And im not familiar with the "Next" syntax either

Next ForLoopCounter

I hope im not being too much of a nuisance here, but your help is truly appreciated as well as any others.

Thanks in advance

-Russell aka Rabbit

I don't see anywhere in your code where you load the values for array. I would go ahead and do that in the load event. As long as you have the array declared in the beginning

Option Explicit
Dim arrNames(3, 1) As String

then your array values will be available through the life of the program.

But as VB5 said, you need to loop through your array to see if the person has enter a valid match. But since you're checking for a password, you also need to provide in your program a place for your user to input not only his name, but his password.

I would suggest a couple of text boxes for that purpose.
Since you already know the bounds of your array you can just loop throught it.

Private Sub CheckUserPassword ()
Dim i as integer
Dim bFound as Boolean
bFound = False

for i = 0 to 3
    if arrNames(i, 0) = strUserName then
        if arrNames(i,1) = strPassWord then
              ' Success
              bFound = True
              exit for ' this will exit the loop
        end if
    end if
next i ' The next statement just increments the variable by one

' what to do if invalid username or password
if bFound = False then
     ' code to notify user
end if
End sub()

Ubound just returns the upper boundary of an array. In your case, you already know what it is. The Ubound property is usually used with Dynamic Arrays

Returns aLong containing the largest available subscript for the indicated dimension of anarray.

Syntax

UBound(arrayname[, dimension])

The UBound function syntax has these parts:

Part Description
arrayname Required. Name of the arrayvariable; follows standard variable naming conventions.
dimension Optional; Variant (Long). Whole number indicating which dimension's upper bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.
MSDN

Hey hkdani, thanks for the reply

I used the syntax you and vb5 posted. But im now getting an error with the NEXT

Private Sub U_PCheck()
    Dim intArray As Integer, varFound As Boolean
    varFound = False
'Logical test to check username
    For intArray = 0 To 3
        If strUsername = arrNames(intArray, 0) Then
            If strPassword = arrNames(intArray, 1) Then
                varFound = True
            Exit For
        End If
    Next intArray
    
    If varFound = True Then
        'Call Enable_Passwordbox
        'Call OK_ON
        Call Main_ON
        Unload Me
    Else
        MsgBox "Incorrect Username or Password Entered!", _
        vbExclamation, "INCORRECT LOGIN!"
        txtUsername.SetFocus
    End If
End Sub

I commented out those two CALLs, wasnt going to use them
Anything wrong there?

Thanks in advance

-Russell aka Rabbit

WTF?

I addedd these two lines between the Dim and the varFound = falseand all of a sudden the NExt error stopped...

strUsername = txtUsername.Text
strPassword = txtPassword.Text

Anyhow, im gone back to debugging. Will be back with more hopefully

Ok...False alarm.

Still getting the NExt error

It says
"Next without For"

What does that mean?

It says
"Next without For"

You left out an End if in the Nested If ...End if

If strUsername = arrNames(intArray, 0) Then
            If strPassword = arrNames(intArray, 1) Then
                varFound = True
                Exit For
            ' This is where you should have an End if  
        End If

I still dont know what it mean, but I solved it. I didnt end one of the IFs

I had put this code under the btnOk click event as

Private Sub btnOK_Click()
    Call U_PCheck
End Sub

But when I press ok it goes to the main screen like its supposed to. BUT I didnt enter a username OR password.
This is the code currently under the called procedure

Private Sub U_PCheck()
    Dim intArray As Integer, varFound As Boolean
    strUsername = txtUsername.Text
    strPassword = txtPassword.Text
    varFound = False
'Logical test to check username
    For intArray = 0 To 3
        If strUsername = arrNames(intArray, 0) Then
            If strPassword = arrNames(intArray, 1) Then
                varFound = True
            Exit For
            End If
        End If
    Next intArray
    
    If varFound = True Then
        'Call Enable_Passwordbox
        'Call OK_ON
        Call Main_ON
        Unload Me
    Else
        MsgBox "Incorrect Username or Password Entered!", _
        vbExclamation, "INCORRECT LOGIN!"
        txtUsername.SetFocus
    End If
End Sub

Thanks in advance, all help is appreciated

-Russell aka Rabbit

BUT I didnt enter a username OR password.

Put a break at Call Main_ON and use your immediate window to check the values of strUserName , strPassword and the values in your arrNames.

Im not too sure I know HOW to use the immediate window. Where do I put the debug.print to get the values you asked for?

Just click inside the gray column to the left of Call Main_ON() .
Put a question mark before the variable name and press enter in the Immediate Window. The next line should show the value of the variable.


Immediate Window
-----------------------------------
?strUserName
JoeUser

I would put your debug.print right after the if varFound = True Then line.

if varFound = True then
    debug.print strUserName, strPassWord, ' etc. .................

When I did the immediate window it gave me blank responses. to indicate that the variables were empty.

thanks for the tips guys. If it wasnt for you two id be screwed.

But I figured it out!

YEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEES YEEEEEEEEEEEEEEEEEEEEEEEEEEESSSSS YEEEEES YES YES YES YES YESSSSSSSSSS YEEEEEEEEEEEEEEEEEEEEEEES!!!!!!!!!!!!!!!!!!!!!!!!!

Sorry I took so long to post the solution , I was doing my victory screams for 10mins. Its been 2whole days!!! I only got 10hours sleeep!! YEEEEEEEEEEEEEEEEEEES!!!!

I decided to put the array data in a procedure here :

Private Sub Usernames_Passwords()
    
'Assigns names to array space 1
        arrNames(0, 0) = "bnsjon"
        arrNames(1, 0) = "bnsjack"
        arrNames(2, 0) = "bnsjane"
        arrNames(3, 0) = "bnsjill"
        
'Assigns Passwords to array space 2
        arrNames(0, 1) = "jon"
        arrNames(1, 1) = "jack"
        arrNames(2, 1) = "jane"
        arrNames(3, 1) = "jill"
End Sub

Then I created a procedure for to check the usernames and passwords:

Private Sub U_PCheck()
    Dim intArray As Integer, varFound As Boolean
    strUsername = txtUsername.Text
    strPassword = txtPassword.Text
    varFound = False
    
'Will call the array data to be tested
    Call Usernames_Passwords
    
'Logical test to check username
    For intArray = 0 To 3
        If strUsername = arrNames(intArray, 0) Then
            If strPassword = arrNames(intArray, 1) Then
                varFound = True
            Debug.Print strUsername, strPassword
            Exit For
            End If
        End If
    Next intArray
    
    If varFound = True Then       
        Call Main_ON
        Unload Me
    Else
        MsgBox "Incorrect Username or Password Entered!", _
        vbExclamation, "INCORRECT LOGIN!"
        txtUsername.SetFocus
    End If
End Sub

Then I called that procedure under the btnOK_Click here:

Private Sub btnOK_Click()
    Call U_PCheck
End Sub

AND IT WORKS!!!! YEEEEEEEEES!!!!!!!!!!!!

For anyone who has the same or similar problem I do hope this helps you.

And hkdani and vbprgrmr thank you again

-Russell aka Rabbit

Thanks for replying vb5prgrmr

Can you explain a couple lines to me?
Why do I need to do that? And what are those two supposed to do?

ForLoopCounter As Integer, UpperBoundOfArray As Integer

You forgot the Dim which means Dimension e.g. your telling the complier to dimension/(reserved space in memory for this variable that will need x size). The x size is provided by the As VariableType, integer, byte, long, double, string, varient, etc.

Of which, you should always declare your variable and to force the environment (ide) to force you to do such a thing goto the ide's menu > tools > options and on the editor tab check the box next to where it says "Require Variable Declaration". Then in all future projects you will see Option Explicit at the top of each form and module.

I understand what the code is trying to do here, but I dont think I understand why you used those specific codes
So what does the part I highlighted in red do?

If Trim(strUsername) = Trim(ArrNames(ForLoopCounter, 0)) Then
    FoundName = True

The trim function removes extraneous spaces from a string because "this string" is not equal to " this string " because of the spaces.

I get the assigning of values here, but what does this mean?

UpperBoundOfArray = UBound(ArrNames)

I see someone has explained this already

And im not familiar with the "Next" syntax either

Next ForLoopCounter

In vb's help, on the index tab, type in "for keyword". Then select the "for...next statement" (not the for each...next statement) and click on display. There you can read all about a for loop and if you click on see also at the top you will be able to read about other looping structures.

I hope im not being too much of a nuisance here, but your help is truly appreciated as well as any others.

Thanks in advance

-Russell aka Rabbit

Not a problem.

Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.