Search Array to determine if MyName is last array value (1 Viewer)

XLEAccessGuru

XLEGuru
Local time
Today, 16:01
Joined
Nov 17, 2006
Messages
65
Hi everyone,

I'd like to use an array to store a list of names. Then I would like to search the array to see if my name is the last value in the array. In essence, I need to determine if the person currently filling out this form is the last person in a list of names stored in an array. Make sense?

I already have a loop that cycles through controls on the form as shown at the bottom of this post. The array will contain 12 elements maximum, but may not always contain 12.

For example - if the array contains:

{John Doe, Jane Doe, My Name, Josh Doe, Ellen Doe}

After the array is filled, in this case, I need a boolean variable - let's call it - bnLast - to return "FALSE" because "MyName" is NOT the last name in the array.

Can anyone offer a link or some code for this to help me out? I'm terrible with arrays. I am notoriously awful at array usage, so any help with this would be great. My looping code is show below. Just need help adding code in it to fille the array and code after it to tell me if MyName is the last value in the array.

Thanks in advance to anyone who can save me here! :D

(btw, I did search extensively in this forum and other areas for this, but had a hard time finding an example that would work here)

Code:
For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
        If Left(ctl.Name, 6) = "txtApp" Then
            If Not IsNull(ctl.Value) Then 'if textbox is not null
                Approver = ctl.Value 'identify value in text box
                If Approver = CurApprover Then 'if value matches currently logged in approver
                    boApproverPresent = True 'tell program that approver is present on the form
                    Exit For 'exit loop
                End If
            End If
        End If
    End If
Next ctl
 

DCrake

Remembered
Local time
Today, 21:01
Joined
Jun 8, 2005
Messages
8,632
Simple Software Solutions

Ok

As you said there will be 12 elements max
And you know who you are (WhoAmI)


'Count the number of Populated elements
For X = 0 to 11

If MyArray(X) = "" Then

Exit For

Endif

Next

If MyArray(X) <> WhoAmI Then

BlnLast = False

Else

BlnLast = True

Endif


Summary:

Count then number of names in the array (X)
Check if the last entry = your name (WhoAmI)

Return True or False


CodeMaster::cool:
 

DJkarl

Registered User.
Local time
Today, 15:01
Joined
Mar 16, 2007
Messages
1,028
Well if it's a single dimension array and you are always needing to check if the item is the last element then maybe something like this.

If "myName" = myAR(ubound(myAR)) then
'Do whatever for true
Else
'Do whatever for false
End If
 

XLEAccessGuru

XLEGuru
Local time
Today, 16:01
Joined
Nov 17, 2006
Messages
65
Thanks for all your help fellas (or ladies - not sure). Both of your posts helped me work it out. I turned out to be quite simple, but like I said, I'm not so great at arrays.
 

XLEAccessGuru

XLEGuru
Local time
Today, 16:01
Joined
Nov 17, 2006
Messages
65
Also filled Array out of order!

In addition to your help, I wanted to post this also because I had trouble getting the "Last" idea to work at first because I was filling the array based on text box values. Problem was, it was not filling the names in order because when you use a loop to loop through controls, it loops in the order they were created. To get the array to fill the element index that matched the "Approver Number" I used the code excerpt below.

Basically, I needed array index #1 to contain the value in Approver #1's text box, array index #2 to contain the value in Approver #2's text box, and so on. The trick was in naming each text box with a standard name and the last character in the control name being a number. The "y" variable was declared as a variant so I did not have to do any data type conversions.

Take a look - I'm quite proud of myself on this one considering my trouble with arrays! LOL


Code:
'repeat above loop to fill an array containing all approver IDs on form.
For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
        If Left(ctl.Name, 6) = "txtApp" Then 'identifies only textboxes that contain Approver ID info to be stored in the following array
            stCtlName = ctl.Name 'set control name variable
            y = CVar(Right(ctl.Name, 1)) 'set array index variable (equals last character of text box name which is the number representing both Approver # and array index where it should be stored.)
            If Not IsNull(ctl.Value) Then 'if textbox is not null
                aApprovers(y) = ctl.Value 'set corresponding array index equal to control contents
            End If
        End If
    End If
Next ctl
 

DCrake

Remembered
Local time
Today, 21:01
Joined
Jun 8, 2005
Messages
8,632
Simple Software Solutions

It's alright climbing on the back of giants but when you get your first foot on the rung and reach the top all by yourself it feels a hell of a lot better.

Reaching the summit of Everest is optional its the getting back down which is mandatory.
 

Users who are viewing this thread

Top Bottom