for sjrl only !!!

i want an element in my form (i don't know it's name, it may be this listbox) that aligns the data in the database in columns like Explorer does with the files, sizes, dates and so on
how to make this columns and when clicked a column header it aligns the data by date, by email , by name and so on
marusiaAsked:
Who is Participating?
 
sjrlConnect With a Mentor Commented:
Use a listview control instead of the listbox. It is part of the windows common controls and allows you to sort easily.
To make it look like explorer you need to set the view property to lvwReport.

You can setup the columns at design time or runtime whichever you prefer.

The code to do it at runtime is:


'This sets the listview to look like explorer
'experiment with other values if you wish
    ListView1.View = lvwReport
'This sets up three columns in the list
'The first parameter is the index, the next two are the display name and key (can't remeber which way round, and the last is the width.
    ListView1.ColumnHeaders.Add 1, "Name", "Name", 1000
    ListView1.ColumnHeaders.Add 2, "Age", "Age", 1000
    ListView1.ColumnHeaders.Add 3, "Email", "Email", 2000



To add the data to the listview:

Dim newListItem As ListItem

'Remove all items from the list
    ListView1.ListItems.Clear

'Here RS is the recordset, and rcount the number of records as in you're previous question
'Loop through the records adding each to the list
    For i = 0 To rcount - 1
'This line adds a record and sets the text property to RS!Name
'With the listview, the first column is the .text property, the second column is .subitems(1), the third .subitems(2) etc.
       Set newListItem = ListView1.ListItems.Add(, RS!Name, RS!Name)
       newListItem.SubItems(1) = RS!Age
       newListItem.SubItems(2) = RS!Email
    Next i
'Set our reference to nothing.
    Set newListItem = Nothing      


To sort the data on columns:

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    With ListView1
'Check to see whether the listview is sorted.
        If .Sorted And .SortKey = ColumnHeader.Index - 1 Then
'If so, check whether it is sorted on the column the user clicked on.
'If, so reverse the order.
            If .SortOrder = lvwAscending Then
                .SortOrder = lvwDescending
            Else
                .SortOrder = lvwAscending
            End If
        Else
'O/wise sort ascending.
            .SortOrder = lvwAscending
            .SortKey = ColumnHeader.Index - 1
            .Sorted = True
        End If
    End With
End Sub
 



I hope this helps.
If you've got any questions post them!
0
 
marusiaAuthor Commented:
oh .. i forgot
i need simple explanations on the code you'll post
very simple really
just to know what i am doing
0
 
marusiaAuthor Commented:
there is a little problem with this code
it give an error (key is not unique in collection) in the line ->

Set newListItem = ListView1.ListItems.Add(, RS!Name, RS!Name)

what is the problem, am I doing something wrong

also ... can you guide me where i can learn some more about databases

i am waiting
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sjrlCommented:
The problem here is that you have 2 people with the same name in the database. To get round this problem change

Set newListItem = ListView1.ListItems.Add(, RS!Name, RS!Name)
to
Set newListItem = ListView1.ListItems.Add(, , RS!Name)


This should work.
0
 
marusiaAuthor Commented:
ok
this works
now i have another problem :))
if you are tired of me just say it
so
when using the vmano's code i have five equal records

name-age-email
name-age-email

and so on...
how can i have different records:

name-age-email
name2-age2-email2

i got rid of the 'for' loop and tried

RS.AddNew
RS!Name = "Name"
RS!age = "Age"
RS!email = "EMail"
RS.Update

RS.AddNew              
RS!Name = "Name2"
RS!age = "Age2"
RS!email = "EMail2"

but when i list them it's again
name-age-email
name-age-email

what is wrong now

P.S. if you want i can post it as another question with another 200 points
i am really sorry bothering you but i want to make a database program
it's like my dream and girls' dreams should always become reality :)

btw. i am Marusha from Poland, who are you ?

0
 
sjrlCommented:
In the code you listed you didn't call RS.Update after setting the second record. Let me know if this doesn't work.

Stuart
(From England)
0
 
sjrlCommented:
And another posting is probably a good idea.
0
 
marusiaAuthor Commented:
i'll make another posting but let's do it here )i'll pay you in the other posting, don't worry)

so ...
i just missed to paste RS.update here, it's in the code
see the code i am using and tell me if you see something wrong

Dim DB As Database
Dim TD      As TableDef
Dim FD      As Field
Dim RS      As Recordset
Dim i       As Integer
           
filename = txtFileName
Set DB = CreateDatabase(filename, dbLangGeneral)
Set TD = DB.CreateTableDef("Table1")
Set FD = TD.CreateField("Name", dbText)
TD.Fields.Append FD
Set FD = TD.CreateField("Age", dbText)
TD.Fields.Append FD
           
Set FD = TD.CreateField("EMail", dbText)
TD.Fields.Append FD
           
DB.TableDefs.Append TD
           
Set RS = DB.OpenRecordset("Table1", dbOpenTable)
           
RS.AddNew              
RS!Name = "Name"
RS!age = "Age"
RS!email = "EMail"
RS.Update

RS.AddNew
RS!Name = "Name2"
RS!age = "Age2"
RS!email = "EMail2"
RS.Update

RS.Close
DB.Close

0
 
sjrlCommented:
Does this code run?
If so, what is in the new database?
0
 
sjrlCommented:
Marusha are you still checking this page?
0
 
sjrlCommented:
Did you manage to get it to work?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.