Hey guys,
Being the amateur that I am, I do not know how I can select a record in a listbox and then display the contents somewhere else.
Basically, I load a txt file for Input (which consists of user's and passwords), of which only 1 part is displayed in a list box-eg username. When this is selected and a cmd button is pressed, I want this to display all the information of the specific 'user' selected somewhere else. I know there is a selected property but technically none of the information shown in the list box is in an array... i think.
Thanks in advance
Code:
Dim user As Integer
user = lstusers.Selected ???
If user Then
If MsgBox("Please select a user", vbExclamation + vbOKOnly, "Select a user") = vbOK Then
End If
Else
If MsgBox("Are you sure you want to delete this user?", vbYesNo + vbExclamation, "Delete?") = vbYes Then
lstusers.RemoveItem (user)
End If
End If
End Sub
Use, List box ITEMDATA property, While pulling data from the table,
Code:
lstCashAc.Clear
While Not rs.EOF = True
list1.AddItem rs(1) 'Suppose This is Username
list1.ItemData(list1.NewIndex) = rs(0) ' This is user id
rs.MoveNext
Wend
On Double click of List1
Code:
Private Sub List1_DblClick()
With list1
mtxtD.text = .ItemData(.ListIndex)
txtname.Text = .List(.ListIndex)
MsgBox .ItemData(.ListIndex) & " " & .List(.ListIndex), vbInformation
End With
End Sub
Homer...here is a simple way to do what you want:
1-MY text file is formatted "username,password" (one per line)...you can alter the code to read your text file into the arrays as I show.
2-I have a cmdbutton to load the file, a listbox to hold the usernames, and two textboxes to display the selected username and that password
Code:
Option Explicit
Dim myArray() As String
Dim myPwdArray() As String
Private Sub Command1_Click()
Dim intFile As Integer, intfile2 As Integer, x As Integer, y As Integer
Dim myLine As String
intFile = FreeFile
Open App.Path & "\myTextFile.txt" For Input As intFile
Do While Not EOF(1)
Line Input #1, myLine
y = y + 1
Loop
ReDim myPwdArray(y) As String
Close #1
intfile2 = FreeFile
Open App.Path & "\myTextFile.txt" For Input As intfile2
Do While Not EOF(1)
Line Input #1, myLine
myArray = Split(myLine, ",")
List1.AddItem (myArray(0))
myPwdArray(x) = (myArray(1))
x = x + 1
Loop
Close #1
End Sub
Private Sub List1_Click()
Text1.Text = List1.List(List1.ListIndex)
Text2.Text = myPwdArray(List1.ListIndex)
End Sub
It looks like there's 3 properties in the file, User's Name, User's ID and User's Password. You could hold the data, as suggested by Sam, in multiple arrays, multi-dimentional Array or a User Defined Type. The important thing is to establish a relationship between the items in the ListBox and the Array element(s) where the information for that user is held.
Here's an example of how it might be done
Code:
Option Explicit
Private Type User_Information
UserName As String
UserID As String
Passsword As String
End Type
Private myUsers() As User_Information
Private Sub LoadUserData()
Dim strFileContents As String
Dim strFileRecords() As String
Dim strThisRecord() As String
Dim intFile As Integer
Dim intI As Integer
intFile = FreeFile
'
' Open and read the entire contents of the file
' File format is assumed to be:
' Full Name, UserID, Password
' e.g. Fred Jones, Fred, Fredpassword
'
Open "C:\MyDir\MyFile.text" For Input As intFile
strFileContents = Input(LOF(intFile), intFile)
Close intFile
'
' Split the contents into records
' and re-define the UDT array to the number of records in the file
'
strFileRecords = Split(strFileContents, vbNewLine)
ReDim myUsers(UBound(strFileRecords))
'
' For each record, split it into the three elements
' and assign to the appriproate members of the UDT
' Add the userID to the ListBox
'
For intI = 0 To UBound(strFileRecords)
strThisRecord = Split(strFileRecords(intI), ",")
myUsers(intI).UserName = strThisRecord(0)
myUsers(intI).UserID = strThisRecord(1)
myUsers(intI).Passsword = strThisRecord(2)
List1.AddItem strThisRecord(1)
Next intI
End Sub
Private Sub cmdView_Click()
'
' User has clicked the 'View' button
' Make sure that they have selected a User to view
' (If they haven't then List1.Listindex will be -1)
'
If List1.ListIndex >= 0 Then
'
' Set the labels on frmviewuser to the corresponding
' values associated with the selected User
' and Display the Form
'
With frmviewuser
.lblfullname.Caption = myUsers(List1.ListIndex).UserName
.lblusername.Caption = myUsers(List1.ListIndex).UserID
.lblpassword.Caption = myUsers(List1.ListIndex).Passsword
.Show
End With
Else
MsgBox "Please Select a User Name to View"
End If
End Sub
Private Sub Form_Load()
LoadUserData
End Sub
Option Explicit
Private Type User_Information
UserName As String
UserID As String
Passsword As String
End Type
Private myUsers() As User_Information
should all be located in the Declarations Section of the Form 'frmteachermain'.
The 'Option Explicit' statement forces you to define all variables used in the Code. Using this statement in all Forms / Modules is considered 'Best Practice' as it helps tremendously when debugging the code.
The 'Private myUsers() As User_Information' statement defines a Dynamic User Defined Type (UDT) Array of type 'User_Information' whose structure is defined by the declarations between the Type / End Type statements.
It is defined in the Declarations Section so that it can be accessed by any / all Subroutines and Functions in the Form, frmteachermain. (in 'technical terms' its Scope is Global to the Form)
[If you wanted it to be accessible by every Form / Module in the Application then you'd move the UDT definition and the Dynamic Array definition to a Module within the Application and change 'Private myUsers() As User_Information' to 'Public myUsers() As User_Information'. That would make the scope of 'myUsers()' Global to the Application. Also, if you wanted to create a second or more UDT arrays that were private to specific subroutines, you'd need to change 'Private Type User_Information' to 'Public Type User_Information' in the Module. That would make the UDT Definition Scope Global to the Application]
Last edited by Doogle; May 20th, 2013 at 01:25 AM.
ok, i got that but i'm still getting a 'subscript out of range error' -runtime 9 in this part of my code.
Code:
For intI = 0 To UBound(strFileRecords)
strThisRecord = Split(strFileRecords(intI), ",")
myUsers(intI).UserName = strThisRecord(0)
myUsers(intI).UserID = strThisRecord(1)
myUsers(intI).Passsword = strThisRecord(2)
List1.AddItem strThisRecord(1)
why would this be because the array is declared above. I think it has something to do with the loading of the sub "LoadUserData"
under the form load it currently shows
Option Explicit
Private Type User_Information
UserName As String
UserID As String
Passsword As String
End Type
Private myUsers() As User_Information
'Dim myUsers As User_Information
Private Sub cmdadd_Click()
frmnewuser.Show
frmnewuser.txtfullname.SetFocus
Me.Hide
Unload Me
End Sub
Private Sub LoadUserData()
Dim strFileContents As String
Dim strFileRecords() As String
Dim strThisRecord() As String
Dim intFile As Integer
Dim intI As Integer
intFile = FreeFile
'
' Open and read the entire contents of the file
' File format is assumed to be:
' Full Name, UserID, Password
' e.g. Fred Jones, Fred, Fredpassword
'
Open "H:\IPT\project\vb prject2013\names.txt" For Input As intFile
strFileContents = Input(LOF(intFile), intFile)
Close intFile
'
' Split the contents into records
' and re-define the UDT array to the number of records in the file
'
strFileRecords = Split(strFileContents, vbNewLine)
ReDim myUsers(UBound(strFileRecords))
'
' For each record, split it into the three elements
' and assign to the appriproate members of the UDT
' Add the userID to the ListBox
'\
For intI = 0 To UBound(strFileRecords)
strThisRecord = Split(strFileRecords(intI), ",")
myUsers(intI).UserName = strThisRecord(0)
myUsers(intI).UserID = strThisRecord(1)
myUsers(intI).Passsword = strThisRecord(2)
List1.AddItem (strThisRecord(1))
Next intI
'For intI = 0 To UBound(strFileRecords)
' strThisRecord = Split(strFileRecords(intI), ",")
' myUsers(intI).UserName = strThisRecord(0)
' myUsers(intI).UserID = strThisRecord(1)
' myUsers(intI).Passsword = strThisRecord(2)
' List1.AddItem strThisRecord(1)
'Next intI
End Sub
Private Sub cmdView_Click()
'
' User has clicked the 'View' button
' Make sure that they have selected a User to view
' (If they haven't then List1.Listindex will be -1)
'
If List1.ListIndex >= 0 Then
'
' Set the labels on frmviewuser to the corresponding
' values associated with the selected User
' and Display the Form
'
With frmviewuser
.lblfullname.Caption = myUsers(List1.ListIndex).UserName
.lblusername.Caption = myUsers(List1.ListIndex).UserID
.lblpassword.Caption = myUsers(List1.ListIndex).Passsword
.Show
End With
Else
MsgBox "Please Select a User Name to View"
End If
End Sub
Private Sub Form_Load()
Call LoadUserData
End Sub
CAVEATE: Doogle's approach is a fine one to use....I only offer mine as well.
I use two arrays (one multi-dimensional array would work just fine also).
I named you text file (in my example) myTextFile.txt
Here is the code I used with a listbox, a commandbutton, and two textboxes on a form.
Code:
Option Explicit
Dim myNameArray() As String
Dim myPwdArray() As String
Private Sub Command1_Click()
Dim intFile As Integer, intfile2 As Integer, x As Integer, y As Integer
Dim myLine As String, displayData As String
intFile = FreeFile
Open App.Path & "\myTextFile.txt" For Input As intFile
Do While Not EOF(1)
Line Input #1, myLine
y = y + 1
Loop
ReDim myPwdArray(y) As String
Close #1
intfile2 = FreeFile
Open App.Path & "\myTextFile.txt" For Input As intfile2
Do While Not EOF(1)
Line Input #1, myLine
myLine = Replace(myLine, """", "")
If Len(Trim(myLine)) > 0 Then
myNameArray = Split(myLine, ",")
List1.AddItem (myNameArray(0))
myPwdArray(x) = (myNameArray(1))
End If
x = x + 1
Loop
Close #1
End Sub
Private Sub List1_Click()
Text1.Text = List1.List(List1.ListIndex)
Text2.Text = myPwdArray(List1.ListIndex)
End Sub
YAY it worked. However if i wanted to do this with a text file that had 3 variables/inputs (eg Name,Age,Pass), would i just have split this section into another array?
Yes. In my example, add (assuming format of text file is now "John","56","23242")
Dim myAgeArray() as String in Declarations section, then
ReDim myAgeArray(y) As String after ReDim myPwdArray(y) As String, then change
myPwdArray(x) = myNameArray(1) to myPwdArray(x)=myNameArray(2)
and add
myAgeArray(x) = myNameArray(1), then finally (after adding a third textbox)
Change the click event of the listbox to:
Thanks for all the help. I know i seem like i'm nagging but this usage of text file isn't really up my ally...
Yet another question.
If i was to add another textfile consisting of scores etc. formatted like: "name","score","outof","difficulty","starsearned"
On the load of this form. When you click a name in the list we have already and press a button to go to a new form (where this information is displayed). It matches up the second text file's info to the new text file by comparing the name in the new file with the username from the original text file. I then want it to display this new info in the other form.
I understand the displaying bit will be quite similar as before however I don't know what i'm doing when it comes to 'linking' the two files
Thanks
heres the code: (Probs not right!)
Code:
Private Sub Form_Load()
Dim intFile As Integer, intfile2 As Integer, x As Integer, y As Integer
Dim myLine As String, displayData As String
intFile = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intFile
Do While Not EOF(1)
Line Input #1, myLine
y = y + 1
Loop
ReDim myPwdArray(y) As String
ReDim myUserArray(y) As String
Close #1
intfile2 = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile2
Do While Not EOF(1)
Line Input #1, myLine
myLine = Replace(myLine, """", "")
If Len(Trim(myLine)) > 0 Then
myNameArray = Split(myLine, ",")
lstusers.AddItem (myNameArray(0))
myPwdArray(x) = (myNameArray(2))
myUserArray(x) = (myNameArray(1))
End If
x = x + 1
Loop
Close #1
Dim intfile3 As Integer, intfile4 As Integer, z As Integer, q As Integer
Dim myLine1 As String, displaydata1 As String
intfile3 = FreeFile
Open "P:\IPT\project\vb prject2013\scores.txt" For Input As intfile3
Do While Not EOF(1)
Line Input #1, myLine1
z = z + 1
Loop
ReDim mmyname(z) As String
ReDim myScoreArray(z) As String
Close #1
intfile4 = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile4
Do While Not EOF(1)
Line Input #1, myLine1
myLine1 = Replace(myLine1, """", "")
If Len(Trim(myLine1)) > 0 Then
myOutofArray = Split(myLine1, ",")
myName1Array(q) = (myOutofArray(0))
myScoreArray(q) = (myOutofArray(1))
myOutofArray(q) = (myOutofArray(2))
'myDifficultArray(q) = (myOutofArray(3))
'myStarsArray(q) = (myOutofArray(4))
End If
q = q + 1
Loop
Close #1
End Sub
Private Sub lstusers_Click()
frmviewuser.lblfullname.Caption = lstusers.List(lstusers.ListIndex)
frmviewuser.lblpassword.Caption = myPwdArray(lstusers.ListIndex)
frmviewuser.lblusername.Caption = myUserArray(lstusers.ListIndex)
If myName1Array = lstusers.List(lstusers.ListIndex) Then
lbltasks.Caption = myNameArray
End Sub
Option Explicit
Dim myNameArray() As String
Dim myUserArray() As String
Dim myPwdArray() As String
Dim myDifficultArray() As String
Dim myScoreArray() As String
Dim myOutofArray() As String
Dim myName1Array() As String
Dim myStarsArray() As String
Private Sub lstusers_Click()
Dim I As Integer
frmviewuser.lblfullname.Caption = lstusers.List(lstusers.ListIndex)
frmviewuser.lblpassword.Caption = myPwdArray(lstusers.ListIndex)
frmviewuser.lblusername.Caption = myUserArray(lstusers.ListIndex)
frmviewuser.lblscore.Caption = vbNullString
Do
If Trim$(myName1Array(I)) = Trim$(frmviewuser.lblusername.Caption) Then
'
' (I) is the Index into the second set of arrays that match the name
' e.g. myscorearray(I) would be the score for this username
'
frmviewuser.lblscore.Caption = myScorearray(I)
Else
I = I + 1
End If
Loop Until frmviewuser.lblscore.Caption <> vbNullString Or I > UBound(myName1Array)
BTW (with no offence to Sam) I really don't like the idea of parsing the file twice, once to count the records and the second to read them.
If you're going to use Line Input then dimension the Dynamic Arrays to something 'big' and then 'ReDim Preserve' as necessary. For example
Code:
'
' Dimension the Dynamic Arrays to hold 200 elements
'
ReDim myPwdArray(199)
ReDim myUserArray(199)
intfile2 = FreeFile()
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile2
Do While Not EOF(1)
Line Input #intfile2, myLine
myLine = Replace(myLine, """", "")
If Len(Trim$(myLine)) > 0 Then
myNameArray = Split(myLine, ",")
lstusers.AddItem (myNameArray(0))
If x > UBound(myPwdArray) Then
'
' If we've run out of elements then add another 200
'
ReDim Preserve myPwdArray(UBound(myPwdArray) + 200)
ReDim Preserve myPwdArray(UBound(myUserArray) + 200)
End If
myPwdArray(x) = (myNameArray(2))
myUserArray(x) = (myNameArray(1))
End If
x = x + 1
Loop
'
' Resize the arrays to the actual number of elements used
'
ReDim Preserve myPwdArray(x - 1)
ReDim Preserve myPwdArray(x - 1)
Close intfile2
Whilst people tend to avoid using ReDim Preserve because it is 'slow', in this case, I suspect it's going to be faster than reading the file twice. If you set the initial Dimension of the arrays to an estimate of the maximum number of records in the file then it's only likely to have to 'ReDim Preserve' once for each Array.
EDIT: @Homer - I notice you're allocating File Numbers using FreeFile() and then using #1 in the Line Input and Close statements. You should be consistant and use the variables you've assigned the File Numbers to, (i.e. intFile and intfile2) rather than #1
I'm getting subscript out of range error in these lines. I think this error is because the array exceeds the length declared but I'm not sure why it is doing this though
Code:
intfile4 = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile4
Do While Not EOF(1)
Line Input #1, myLine1
myLine1 = Replace(myLine1, """", "")
If Len(Trim(myLine1)) > 0 Then
myViewScoreArray = Split(myLine1, ",")
myName1Array(q) = (myViewScoreArray(0))
myScoreArray(q) = (myViewScoreArray(1))
myOutofArray(q) = (myViewScoreArray(2))
'myDifficultArray(q) = (myOutofArray(3))
'myStarsArray(q) = (myOutofArray(4))
End If
q = q + 1
Loop
Close #1
EDIT: Only looked at your second post then doogle. I seem to be getting the same error as mentioned above when i Redim preserve the arrays after the loop before i close the file. why would this be?
Last edited by homer5677; Jun 2nd, 2013 at 12:39 AM.
.... and your 'End If' is in the wrong place. You are incrementing 'q' even if there's a blank line (which you ignore). It should look a bit like this:
Code:
intfile4 = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile4
Do While Not EOF(1)
Line Input #intfile4, myLine1
myLine1 = Replace(myLine1, """", "")
If Len(Trim(myLine1)) > 0 Then
myViewScoreArray = Split(myLine1, ",")
myName1Array(q) = (myViewScoreArray(0))
myScoreArray(q) = (myViewScoreArray(1))
myOutofArray(q) = (myViewScoreArray(2))
'myDifficultArray(q) = (myOutofArray(3))
'myStarsArray(q) = (myOutofArray(4))
q = q + 1
End If
Loop
Close #intfile4
End Sub
...... and another thing....... If you're going to continue with the 'two parses of the file' method, you must also check for blank lines when counting the records otherwise the arrays could end up being over Dimensioned. You should use something like this:
Code:
intfile3 = FreeFile
Open "P:\IPT\project\vb prject2013\scores.txt" For Input As intfile3
Do While Not EOF(intfile3)
Line Input #intfile3, myLine1
If Len(Trim$(myLine)) > 0 Then
z = z + 1
End If
Loop
ReDim mmyname(z) As String
ReDim myScoreArray(z) As String
Close intfile3
Option Explicit
Dim myNameArray() As String
Dim myUserArray() As String
Dim myPwdArray() As String
Dim myDifficultArray() As String
Dim myScoreArray() As String
Dim myViewScoreArray() As String
Dim myOutofArray() As String
Dim myName1Array() As String
Dim myStarsArray() As String
'Private Type User_Information
' UserName As String
' UserID As String
' Passsword As String
'End Type
'Private myUsers() As User_Information
'Dim myUsers As User_Information
Private Sub cmdadd_Click()
frmnewuser.Show
frmnewuser.txtfullname.SetFocus
Me.Hide
Unload Me
End Sub
'Private Sub LoadUserData()
'Dim strFileContents As String
'Dim strFileRecords() As String
'Dim strThisRecord() As String
'Dim intFile As Integer
'Dim intI As Integer
'intFile = FreeFile
'
' Open and read the entire contents of the file
' File format is assumed to be:
' Full Name, UserID, Password
' e.g. Fred Jones, Fred, Fredpassword
'
'Open "H:\IPT\project\vb prject2013\names.txt" For Input As intFile
'strFileContents = Input(LOF(intFile), intFile)
'Close intFile
'
' Split the contents into records
' and re-define the UDT array to the number of records in the file
'
'strFileRecords = Split(strFileContents, vbNewLine)
'ReDim myUsers(UBound(strFileRecords))
'
' For each record, split it into the three elements
' and assign to the appriproate members of the UDT
' Add the userID to the ListBox
'\
'For intI = 0 To UBound(strFileRecords)
' strThisRecord = Split(strFileRecords(intI), ",")
' myUsers(intI).UserName = strThisRecord(0)
' myUsers(intI).UserID = strThisRecord(1)
' myUsers(intI).Passsword = strThisRecord(2)
' List1.AddItem (strThisRecord(1))
'Next intI
'For intI = 0 To UBound(strFileRecords)
' strThisRecord = Split(strFileRecords(intI), ",")
' myUsers(intI).UserName = strThisRecord(0)
' myUsers(intI).UserID = strThisRecord(1)
' myUsers(intI).Passsword = strThisRecord(2)
' List1.AddItem strThisRecord(1)
'Next intI
'End Sub
Private Sub cmdView_Click()
'
' User has clicked the 'View' button
' Make sure that they have selected a User to view
' (If they haven't then List1.Listindex will be -1)
'
'If List1.ListIndex >= 0 Then
'
' Set the labels on frmviewuser to the corresponding
' values associated with the selected User
' and Display the Form
'
' With frmviewuser
' .lblfullname.Caption = myUsers(List1.ListIndex).UserName
' .lblusername.Caption = myUsers(List1.ListIndex).UserID
' .lblpassword.Caption = myUsers(List1.ListIndex).Passsword
' .Show
'End With
'Else
' MsgBox "Please Select a User Name to View"
'End If
'Label1.Caption = List1.List(List1.ListIndex)
'Label2.Caption = myPwdArray(List1.ListIndex)
frmviewuser.Show
Unload Me
End Sub
Private Sub cmdviewscores_Click()
frmviewscore.Show
Me.Hide
End Sub
Private Sub Form_Load()
Dim intFile As Integer, intfile2 As Integer, x As Integer, y As Integer
Dim myLine As String, displayData As String
intFile = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intFile
Do While Not EOF(1)
Line Input #1, myLine
y = y + 1
Loop
ReDim myPwdArray(y) As String
ReDim myUserArray(y) As String
'Close intFile
'intfile2 = FreeFile
'Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile2
' Do While Not EOF(1)
' Line Input #1, myLine
'myLine = Replace(myLine, """", "")
'If Len(Trim(myLine)) > 0 Then
'myNameArray = Split(myLine, ",")
' lstusers.AddItem (myNameArray(0))
' myPwdArray(x) = (myNameArray(2))
' myUserArray(x) = (myNameArray(1))
' End If
' x = x + 1
' Loop
'
'Close intfile2
'
' Dimension the Dynamic Arrays to hold 200 elements
'
ReDim myPwdArray(199)
ReDim myUserArray(199)
intfile2 = FreeFile()
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile2
Do While Not EOF(1)
Line Input #intfile2, myLine
myLine = Replace(myLine, """", "")
If Len(Trim$(myLine)) > 0 Then
myNameArray = Split(myLine, ",")
lstusers.AddItem (myNameArray(0))
If x > UBound(myPwdArray) Then
'
' If we've run out of elements then add another 200
'
ReDim Preserve myPwdArray(UBound(myPwdArray) + 200)
ReDim Preserve myPwdArray(UBound(myUserArray) + 200)
End If
myPwdArray(x) = (myNameArray(2))
myUserArray(x) = (myNameArray(1))
End If
x = x + 1
Loop
'
'Resize the arrays to the actual number of elements used
'
ReDim Preserve myPwdArray(x - 1)
ReDim Preserve myPwdArray(x - 1)
Close intfile2
Dim intfile3 As Integer, intfile4 As Integer, z As Integer, q As Integer
Dim myLine1 As String, displaydata1 As String
intfile3 = FreeFile
Open "P:\IPT\project\vb prject2013\scores.txt" For Input As intfile3
Do While Not EOF(intfile3)
Line Input #intfile3, myLine1
If Len(Trim$(myLine)) > 0 Then
z = z + 1
End If
Loop
ReDim myName1Array(z) As String
ReDim myViewScoreArray(z) As String
Close intfile3
intfile4 = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intfile4
Do While Not EOF(1)
Line Input #intfile4, myLine1
myLine1 = Replace(myLine1, """", "")
If Len(Trim(myLine1)) > 0 Then
myViewScoreArray = Split(myLine1, ",")
myName1Array(q) = (myViewScoreArray(0))
myScoreArray(q) = (myViewScoreArray(1))
myOutofArray(q) = (myViewScoreArray(2))
'myDifficultArray(q) = (myOutofArray(3))
'myStarsArray(q) = (myOutofArray(4))
q = q + 1
End If
Loop
Close #intfile4
End Sub
Private Sub lstusers_Click()
Dim I As Integer
frmviewuser.lblfullname.Caption = lstusers.List(lstusers.ListIndex)
frmviewuser.lblpassword.Caption = myPwdArray(lstusers.ListIndex)
frmviewuser.lblusername.Caption = myUserArray(lstusers.ListIndex)
frmviewscore.lblscore.Caption = vbNullString
Do
If Trim$(myName1Array(I)) = Trim$(frmviewuser.lblusername.Caption) Then
'
' (I) is the Index into the second set of arrays that match the name
' e.g. myscorearray(I) would be the score for this username
'
frmviewscore.lblscore.Caption = myScoreArray(I)
Else
I = I + 1
End If
Loop Until frmviewscore.lblscore.Caption <> vbNullString Or I > UBound(myName1Array)
End Sub
Last edited by homer5677; Jun 2nd, 2013 at 12:53 AM.
I've 'tidied' the code up and got rid of the extraneous bits and pieces. Perhaps you could run this and see what happens.
Code:
Option Explicit
Dim myNameArray() As String
Dim myUserArray() As String
Dim myPwdArray() As String
Dim myDifficultArray() As String
Dim myScoreArray() As String
Dim myViewScoreArray() As String
Dim myOutofArray() As String
Dim myName1Array() As String
Dim myStarsArray() As String
Private Sub cmdadd_Click()
frmnewuser.Show
frmnewuser.txtfullname.SetFocus
Me.Hide
Unload Me
End Sub
Private Sub cmdviewscores_Click()
frmviewscore.Show
Me.Hide
End Sub
Private Sub Form_Load()
Dim intFile As Integer, x As Integer
Dim myLine As String, displayData As String
'
' Dimension the Dynamic Arrays to hold 200 elements
'
ReDim myPwdArray(199)
ReDim myUserArray(199)
intFile = FreeFile()
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intFile
Do While Not EOF(intFile)
Line Input #intFile, myLine
myLine = Replace(myLine, """", "")
If Len(Trim$(myLine)) > 0 Then
myNameArray = Split(myLine, ",")
lstusers.AddItem (myNameArray(0))
If x > UBound(myPwdArray) Then
'
' If we've run out of elements then add another 200
'
ReDim Preserve myPwdArray(UBound(myPwdArray) + 200)
ReDim Preserve myPwdArray(UBound(myUserArray) + 200)
End If
myPwdArray(x) = (myNameArray(2))
myUserArray(x) = (myNameArray(1))
x = x + 1
End If
Loop
'
'Resize the arrays to the actual number of elements used
'
ReDim Preserve myPwdArray(x - 1)
ReDim Preserve myPwdArray(x - 1)
Close intFile
x = 0
ReDim myName1Array(199)
ReDim myScoreArray(199)
ReDim myOutofArray(199)
ReDim myDifficultArray(199)
ReDim myStarsArray(199)
intFile = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intFile
Do While Not EOF(intFile)
Line Input #intFile, myLine
myLine1 = Replace(myLine, """", "")
If Len(Trim(myLine)) > 0 Then
If x > UBound(myNameArray) Then
ReDim Preserve myName1Array(UBound(myName1Array) + 200)
ReDim Preserve myScoreArray(UBound(myScoreArray) + 200)
ReDim Preserve myOutofArray(UBound(myOutofArray) + 200)
ReDim Preserve myDifficultArray(UBound(myDifficultArray) + 200)
ReDim Preserve myStarsArray(UBound(myStarsArray) + 200)
End If
myViewScoreArray = Split(myLine, ",")
myName1Array(x) = myViewScoreArray(0)
myScoreArray(x) = myViewScoreArray(1)
myOutofArray(x) = myViewScoreArray(2)
myStarsArray(x) = myViewScoreArray(4)
myDifficultArray(x) = myViewScoreArray(3)
x = x + 1
End If
Loop
ReDim Preserve myName1Array(x - 1)
ReDim Preserve myScoreArray(x - 1)
ReDim Preserve myOutofArray(x - 1)
ReDim Preserve myDifficultArray(x - 1)
ReDim Preserve myStarsArray(x - 1)
Close #intFile
End Sub
Private Sub lstusers_Click()
Dim I As Integer
frmviewuser.lblfullname.Caption = lstusers.List(lstusers.ListIndex)
frmviewuser.lblpassword.Caption = myPwdArray(lstusers.ListIndex)
frmviewuser.lblusername.Caption = myUserArray(lstusers.ListIndex)
frmviewscore.lblscore.Caption = vbNullString
Do
If Trim$(myName1Array(I)) = Trim$(frmviewuser.lblusername.Caption) Then
'
' (I) is the Index into the second set of arrays that match the name
' e.g. myscorearray(I) would be the score for this username
'
frmviewscore.lblscore.Caption = myScoreArray(I)
Else
I = I + 1
End If
Loop Until frmviewscore.lblscore.Caption <> vbNullString Or I > UBound(myName1Array)
End Sub
The implication is that there's (at least) one line in the file which doesn't have 5 values.
I've just realised that you're opening names.txt to get the scores - shouldn't you be opening a different file ? (i.e. the one with the scores in it)
Code:
x = 0
ReDim myName1Array(199)
ReDim myScoreArray(199)
ReDim myOutofArray(199)
ReDim myDifficultArray(199)
ReDim myStarsArray(199)
intFile = FreeFile
Open "P:\IPT\project\vb prject2013\names.txt" For Input As intFile '-> shouldn't this be the file with the scores information etc in it?
Great - if you consider this particular problem solved you can close the Thread (so others know it's been sorted). Navigate to the top of the thread and click on "Thread Tools", select 'Mark Thread as Resolved' from the drop-down.
"BTW (with no offence to Sam) I really don't like the idea of parsing the file twice, once to count the records and the second to read them."
No offense taken. As I have mentioned before, I am a 'bull and jam' type VB programmer...not always the best/efficient way to do things, but it works for me. I don't program for others, just myself...however, I am always willing to learn better ways. Thanks to you (and others), I have picked up a lot of better coding practices since joining this forum last Augus.