im fairly new to vb 6 and doing a course which requires a project.
in my project i need to save a name, age, and form of a user in a random access file when a button is pressed which i have done, and then when another button is pressed, display the name for each record in a list box which i have also managed.

the next bit is selecting a name from the list box, clicking another button(load user info) and displaying the records name, age and form in seperate text boxes.

this is my code so far for saving and loading into the list box

(module)
Public Type recvar
    Name As String * 10
    age As Integer
    Form As String * 2
End Type

Option Explicit
Dim users As recvar, position As Integer, position2 As Integer

Private Sub cmdSave_Click()
    Open App.Path & "\userfile.txt" For Random As #1 Len = Len(users)
            users.Name = frmMain.txtSaveNme.Text
            users.age = frmMain.txtSaveAge.Text
            users.Form = frmMain.txtFormSave.Text
        Put #1, position2 + 1, users
    Close #1
    
    position2 = position2 + 1
    txtSaveNme.Text = ""
    txtSaveAge.Text = ""
    txtFormSave.Text = ""
End Sub


Private Sub cmdLoad_Click()
    position = 1
    Open App.Path & "\userfile.txt" For Random As #1 Len = Len(users)
        Do While Not EOF(1)
            Get #1, position, users
                listUsers.AddItem (users.Name)
                position = position + 1
        Loop
    Close #1

End Sub

then this is where i get stuck and don't know how get the record
number form the selected item in the list box (listUsers):

Private Sub cmdLaodInfo_Click()
     Open App.Path & "\userfile.txt" For Random As #1 Len = Len(users)
        Get #1,  , users
            frmMain.txtLoadNme.Text = users.Name
            frmMain.txtLoadAge.Text = users.age
            frmMain.txtFormLoad.Text = users.Form
    Close #1


End Sub

please help !!

dim getdata as string

getdata = Listbox1.list(1)

will return the data or string on position 1

or if you want to detect which data has been clicked use this

dim getpos as integer

getpos = listbox1.listindex

if index 1 is click getpos will hold the value of 1 or whatever index position is click on the listbox
you can use msgbox to display the value of getpos if you want to verify
hope it helps..

thanks , thats helped a little but i still need to access other saved data for whatever position is selected so:

if i saved a users name as "matt"
age as "20"
and form as "12"

then when i click load the name only will be displayed in the list box "matt" along with any other record names i saved which using the first code i have done,
then if i were to click a name in the listbox i.e "matt" and click another button ,
the name "matt", age "20", and form "12" will all be displayed in seperate text boxes.
is that any clearer?

hmmmm. let me say. you have a listbox for the names of persons, ryt? so when you select a person's name i.e. as what you've said is 'matt' then when you clicked the button "Load User Info" other form will show and there the information of matt will be seen in their respective texboxes or labels etc.

So what you're going to do is to simply query matt's record and then display his infos. you're coding...

frmMain.txtLoadNme.Text = users.Name
frmMain.txtLoadAge.Text = users.age

which is for me is not a good practice.

best practice is make a property for the unique record like IDNumber property on your form "UserInfoForm" then assign the idnumber of matt in the idnumber property then make your query.

yeah a unique record ID would be the best way to go, because what if you have more than one Matt? How then will you distinguish him from another Matt? The code however for what you have, without the need to complicate it with userid's, would be, in your "load" function, to make an array that keeps all the record data, and then sort through it when you want to pull up a specific item. For example:

global UserRecords() as recvar ' // Put This In Your Module

Private Sub cmdLoad_Click()
    ReDim UserRecords(0)
 
    position = 1
    Open App.Path & "\userfile.txt" For Random As #1 Len = Len(users)
        Do While Not EOF(1)
            Get #1, position, users
             If UBound(UserRecords()) > 0 Then
                  ReDim Preserve UserRecords(UBound(UserRecords()) + 1)
                  UserRecords(UBound(UserRecords())) = users
             Else
                  UserRecords(0) = users
             End If
  
             listUsers.AddItem (users.Name)
             position = position + 1
        Loop
    Close #1
End Sub

Now, you should have a global array, called "UserRecords()" which is an array of type "recvar" (users). At This point, later when you need to call up a record, you can pretty much do:

Private Sub cmdLaodInfo_Click()
     for each xname in UserRecords()
          if xname.Name = listUsers.list(listUsers.listindex)     
                 frmMain.txtLoadNme.Text = xname.Name
                 frmMain.txtLoadAge.Text = xname.age
                 frmMain.txtFormLoad.Text = xname.Form
     next xname
End Sub

or something like that (I haven't tested this, so, it could have bugs. You may need to adjust accordingly)

thanks comatose,
i am a little un sure what the "xname" variable is because it is undefined and when i replace it with users which i think it should be
i get a "compile error: for each control variable on arrays must be varient" .......

unless you are using option explicit, you don't need to declare it. Variables that are not declared in VB6 by default are of type variant. Variant is a powerful (but crappy, resource using) variable that basically gets implicitly cast based on how it's needed. So, if it's a number, but used as a string, then it becomes a string. If it's a string, that's used a integer, it becomes an integer...etc.
For each is a looping type that is sort of like a for loop, but you just tell it to loop through all of the known values of an array. In the code above, basically I say "for all of the items in the UserRecords() array, do this code" Since we are looping through an array of recvar (UserRecords()) then xname becomes a variable of type recvar between the words "for" and "next xname". Since xname is then of type recvar, xname has all of the elements of recvar, such as .Form, .Name, and .Age.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.