Results 1 to 36 of 36

Thread: [RESOLVED] ListBox Help

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Resolved [RESOLVED] ListBox Help

    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

  2. #2
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Re: ListBox Help

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    So in order to use the list box for specific record selection, the text file needs to be an array within the listbox??

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: ListBox Help

    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

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    I'm still stuck. Here is the file.

    frmviewuser.fr.frmfrmteachermain.fr.frm

    Once a specific user is selected and the view button is pressed, the full details are shown on the other form of that user.

    [edit: there is a lot of crap commented because i was trying all different things!]

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    What does this do? I having trouble working this in (ie can't find it). Is anything meant to be under it??
    Quote Originally Posted by Doogle View Post
    Code:
    Private myUsers() As User_Information

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    These statements
    Code:
    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    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
    Code:
    LoadUserData
    Is this the problem??

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    Can you post the actual code you're using please ?

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    sorry here it is.
    Code:
    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

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: ListBox Help

    [QUOTE=homer5677;4417167]I'm still stuck. Here is the file.

    I found your form okay, but not the text file...if you will attach that, I'll see what format it is in and offer more suggestions..

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    here is the text file
    Attached Files Attached Files

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: ListBox Help

    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

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    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?

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: ListBox Help

    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:

    [CODE]Text1.Text = List1.List(List1.ListIndex)
    Text2.Text = myAgeArray(List1.ListIndex)
    Text3.Text = myPwdArray(List1.ListIndex)[/
    CODE]

  17. #17

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    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
    frmviewscore.fr.frm
    frmteachermain.fr.frm
    scores.txt
    names.txt
    Last edited by homer5677; Jun 1st, 2013 at 03:46 AM. Reason: made a bit easier to read!

  18. #18

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    really stuck here guys

  19. #19
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    Something like this?
    Code:
    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)
    Last edited by Doogle; Jun 2nd, 2013 at 12:00 AM.

  20. #20
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    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
    Last edited by Doogle; Jun 2nd, 2013 at 12:24 AM.

  21. #21

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    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.

  22. #22
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    Is 'myViewScoreArray' dimensioned somewhere?
    Code:
    Dim myViewScoreArray() As String
    EDIT: are there at least 3 comma separated values in every line of the file ?

  23. #23
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    .... 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

  24. #24

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    Quote Originally Posted by Doogle View Post
    Is 'myViewScoreArray' dimensioned somewhere?
    Code:
    Dim myViewScoreArray() As String
    EDIT: are there at least 3 comma separated values in every line of the file ?
    Yes i declared it in public. And yes it actually has 5 comma separated values in every line

  25. #25
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    ...... 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

  26. #26
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    Quote Originally Posted by homer5677 View Post
    Yes i declared it in public. And yes it actually has 5 comma separated values in every line
    Could you copy and paste all the code you're now using please.

  27. #27

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    Also the txt files:
    names.txt
    scores.txt

    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 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.

  28. #28
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    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
    Last edited by Doogle; Jun 2nd, 2013 at 01:54 AM.

  29. #29
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    I've just made a couple of typo corrections in the code, you may need to copy and paste it again.

  30. #30

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    still getting the subscript out of range error at this stage
    Code:
            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
    EDIT: Stars coming up with same error had a quick look through and couldn't find and naming errors. But my eyes may just be deceiving me
    Last edited by homer5677; Jun 2nd, 2013 at 01:34 AM.

  31. #31
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    There's one typo I missed
    Code:
    ReDim myNameArray(199)
    should be
    Code:
    ReDim myName1Array(199)
    EDIT: I've now corrected it in Post#28
    Last edited by Doogle; Jun 2nd, 2013 at 01:55 AM.

  32. #32

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    its still giving that error. Now at
    Code:
        myStarsArray(x) = myViewScoreArray(4)
                myDifficultArray(x) = myViewScoreArray(3)
    Last edited by homer5677; Jun 2nd, 2013 at 02:08 AM.

  33. #33
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    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?

  34. #34

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    60

    Re: ListBox Help

    Yes, its always something simple isn't it. Thanks for all your help doogle its working quite nicely now!

  35. #35
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: ListBox Help

    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.

    Good luck with the rest of it!

  36. #36
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: [RESOLVED] ListBox Help

    @Doogle.....REF:
    "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.

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