?
Solved

for sjrl only !!!

Posted on 1998-09-24
11
Medium Priority
?
159 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:marusia
  • 7
  • 4
11 Comments
 

Author Comment

by:marusia
ID: 1436489
oh .. i forgot
i need simple explanations on the code you'll post
very simple really
just to know what i am doing
0
 

Accepted Solution

by:
sjrl earned 400 total points
ID: 1436490
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
 

Author Comment

by:marusia
ID: 1436491
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:sjrl
ID: 1436492
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
 

Author Comment

by:marusia
ID: 1436493
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
 

Expert Comment

by:sjrl
ID: 1436494
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
 

Expert Comment

by:sjrl
ID: 1436495
And another posting is probably a good idea.
0
 

Author Comment

by:marusia
ID: 1436496
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
 

Expert Comment

by:sjrl
ID: 1436497
Does this code run?
If so, what is in the new database?
0
 

Expert Comment

by:sjrl
ID: 1436498
Marusha are you still checking this page?
0
 

Expert Comment

by:sjrl
ID: 1436499
Did you manage to get it to work?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question