Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Stuck on How to Reference Arrays

Status
Not open for further replies.

mrf1xa

Technical User
Jan 14, 2002
366
GB
Hi

I am populating an array using the code below, which works fine and I can see the correct contents in the array in the locals window. The code is called from the forms on-load event.
However, I then need to reference this array on the click of a button and compare an element of it to the contents of a text box, but I seem to be missing something obvious. Here is what I have:

Private Type typPerson
UsNm As String * 20
FName As String
Sname As String
PIN As Integer
End Type
Private Sub PopulateArray()

Dim aryUsers() As typPerson
Dim intCount As Integer
intCount = 1

Open "C:\Nigel\VB6\vbTestNames.csv" For Input As #1

ReDim aryUsers(intCount)

Do Until EOF(1)
Input #1, aryUsers(intCount).UsNm
Input #1, aryUsers(intCount).FName
Input #1, aryUsers(intCount).Sname
Input #1, aryUsers(intCount).PIN
intCount = intCount + 1
ReDim Preserve aryUsers(intCount)
Loop

'Debug.Print UBound(aryUsers)

End Sub

Private Sub cmdLogin_Click()

Dim aryUsers()
Dim i As Integer
i = 1

Do Until i = 9
If txtUserName.Text = aryUsers(i).UsNm Then
MsgBox "Match"
Else
MsgBox "No Match"
End If
i = i + 1
Loop

End Sub

Using this and holding the cursor over 'aryUsers(i)' shows nothing i.e. it doesn't seem to recognise the array- the error message when I run it says 'subscript out of range'. Where am I going wrong please?


Nigel
Didn't someone say work is supposed to be fun? They didn't have computers then I guess....
 
It looks like you are thinking that the aryUsers in the two routines are the same array but they are not. They are two seperate arrys that are each local to the subroutine they are declared in.

You will have to make it a Global.
 
Try

Dim aryUsers() As String

the way you have it is you are trying to 'Dim' a variable with nothing there.
 
Your arrays are local to the procedures. If you want to use the array in multiple procedures you may have to increase its scope and make it a modular variable. Don't forget to remove the local declarations. Place this declaration in the form declarations section.

Code:
Dim aryUsers() As typPerson




Take Care,

zemp

"If the grass looks greener... it's probably because there is more manure."
 
Many thanks to rdrokse and zemp, just the pointer I needed!

dyarwood, bad practice I know, but if I fail to declare it as a String or whatever, my understanding it that it defaults to variant?

Cheers

Nigel
Didn't someone say work is supposed to be fun? They didn't have computers then I guess....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top