|
-
May 18th, 2013, 01:25 AM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|