Solved

for sjrl only !!!

Posted on 1998-09-24
11
145 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
Comment Utility
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 200 total points
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:sjrl
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Expert Comment

by:sjrl
Comment Utility
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
Comment Utility
And another posting is probably a good idea.
0
 

Author Comment

by:marusia
Comment Utility
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
Comment Utility
Does this code run?
If so, what is in the new database?
0
 

Expert Comment

by:sjrl
Comment Utility
Marusha are you still checking this page?
0
 

Expert Comment

by:sjrl
Comment Utility
Did you manage to get it to work?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now