Hello all. Pretty much finished my project for school, everything works, you can add and delete files, can't crash the program e.t.c. but I can't find out how to list these in order of date.

[img]http://img300.imageshack.us/img300/4570/visualbasichelp5ck.png[/img]

Currently I have the sorted property of the list box equalling true. As you can see it orders the dates taking notice of the first number rather than the whole date. How can I make it order the files in order by date. By the way I am from the UK so the dates are written UK style way rather than the American way...

:idea: I am guessing I need to get the list box or a function to read the first 10 digits of each file, tell it to think of them as dates then order the files in that order...

If I can't do this I will have to include a search function which would display all the bookings made for that day and I am sure would be a lot harder :sad:

Thanks for looking. Also I would like to compliment the site designers as this forum has a very nice design. :cheesy:

Recommended Answers

All 17 Replies

You can use listview component.

You'll have to do quite a bit of modification in your code to impliment this, since you already have it loading data and everything.... you'll need synchronized arrays to keep all your data in order, but here is a module I wrote that does bubble sort on dates, in either direction. The date format must be in the order of: dd/mm/yyyy. There are 3 functions, 2 are the bubble sort, and 1 (should be private, but what the hey) that is used to swap the elements to the respective indices. The 2 bubble sorts are: B2S_BSort_Date, and S2B_BSort_Date, which is Big To Small Bubble Sort, and Small To Big Bubble Sort Respectively. Based on the image you posted, you could easily modify the portions of the bubble sort in the nested loops that gather the portions of the date to work with your data without too much splitting.... but here is the project, and I wish you the best of luck.

:p

Thanks a lot for the code, its great and it works... But I am not going to use it. Clearly it is too advanced for my level and if I put it in I don't think even my teacher would no what was happening.

There are so many things in the code that I don't understand, to name a few:

Public Sub B2S_BSort_Date(DList() As String, LBox As ListBox)

For I = 0 To UBound(DList) ' What does Ubound do

and I have never used a module before :eek: , as you can probably guess, my programming code is really messy and long. I'm 18 and not in uni yet so this probably explains most of my limited knowlage.

Actually looking more closely at it I may be able to use the code. Just tell me what the above does and what this procedure does:

Public Sub Swap(ByRef AName() As String, Indice1 As Integer, Indice2 As Integer)
Dim tmp As String

tmp = AName(Indice1)
AName(Indice1) = AName(Indice2)
AName(Indice2) = tmp

End Sub

Thanks a lot for all of your help.

Also although you said its for dates in the order dd/mm/yyyy

This program seems to be only for dates in format mm/dd/yyyy and it organises them in that order :-|

In fact I shall even quote one of your list items

X(4) = "06/15/1996"

I need it to work for dates in the format dd/mm/yyyy

and I can't work out how to modify your program in order to do this :sad:

Ok, to start, the line: Public Sub B2S_BSort_Date(DList() As String, LBox As ListBox), creates a public subroutine. You can create these on a whim just because you feel like it. They are user defined subroutines. You have to 2 kinds (well, a lot more in VB) of procedures. Subs and functions. These are sort of like mini-programs. Things that you will do in your code a bunch of times, but you don't want to bulk it out by copy and pasting the same code blocks over and over. So you just make a sub that does it once, and you call it whenever you need to. The name of the sub, B2S_BSort_Date, is Big To Small (B2S) Bubble Sort Date. When you call this sub (just like any other sub you would call, like print, or whatever) you have to give it information. In the case of this sub, we are passing it 2 "parameters". The first, is an array, and the second is a listbox. An array is a single variable, that can contain more than 1 value. So, for example, you can have variable X, and variable X at position 0 or X(0), can equal one thing... while variable X at position 1 X(1) can be something else altogether. The only restriction is that they be the same type (if X is integers, then all of them have to be integers). In this case, the variable that our sub expects is a string, containing dates in the form of dd/mm/yyyy. The listbox is just a reference so that the sub knows where to put the sorted information (I was going to have the function return a sorted array, but that gets a bit more complicated). When you actually call the sub, is makes a copy of the array passed to it. In the attached project, we pass X to the sub. VB copies all the values in X into DList for the sub to use, so in our Sub, all throughout it, we use DList, just as if it were X.... Moving on.

Now Arrays start their "slots" (indices, or elements) at position 0, not 1 (unless you specify otherwise in VB, but don't do that). So we For I = 0 To UBound(DList). Ubound is a function that returns the highest number of an array. For example, if we have an array, with X(0), X(1), X(2) and X(3), then ubound(X) would respond with 3. Because there are 4 (remember zero is a number) elements in our array, the highest number (the upper most element) is 3. This makes it possible for our sub (for the for loop) to work with numbers that change. For I = 0 to 10 is pretty limiting.... so we use ubound basically to say for each element of the array (for every value the array has) do this stuff. A module is just like a form, only it has no visual aspects to it. It's a place where you can store procedures (subs or functions) and variables that the entire project (more than 1 form) can use... if you had 3 forms, and you needed to call the same function from both of them, a module is perfect.

Now, This sub:

Public Sub Swap(ByRef AName() As String, Indice1 As Integer, Indice2 As Integer)
Dim tmp As String

tmp = AName(Indice1)
AName(Indice1) = AName(Indice2)
AName(Indice2) = tmp

End Sub

Again, the first line tells VB this is a sub, named Swap. Remember how when we called the sort function, it made a copy of X into DList.... well, that's called "passing by value", which means it passes the value of the variable to the sub, but not the actual variable itself. This is so you can make changes to the variable all you want, but it won't affect the actual variable. So even though in our sort procedure, we change DList all around, it never actually changes X... well in this sub, we pass the array by reference. That means that the actual original variable gets changed, not just a copy of it. Then we pass the sub 2 integers, Indice1 and Indice2. Then we make a temporary variable, which holds the value of indice1, then we replace indice1 with indice2, and then indice2 with the value of the temporary variable. Basically, to break it down simply, this sub swaps 2 elements of an array, so pretend that we have X(0) and X(1). X(0) = "hi" and X(1) = "bye". If we call the sub swap, with: swap(X, 0, 1), then X(0) will NOW be "bye", and X(1) will now be "hi". It just swaps 2 elements of an array.

Breaking it all down, without so much technical crap ;), what we do basically is a nested for loop. That's a for loop, then inside of it, another for loop. And it is going over the same list, but this lets us compare everything in the array to itself. So, pretend we have X:

X(0) = "hi"
X(1) = "bye"
X(2) = "something"
X(3) = "else"

This system will say: ok, is X(0) greater than X(0), and if so, swap them. Is X(0) greater than X(1), if so swap them, is X(0) greater than X(2) if so, swap them, is X(0) greater than X(3), if so, swap them... is X(1) greater than X(0), if so swap them, is X(1) greater than X(1), if so, swap them, is X(1) greater than X(2), if so swap them.... so on, and so on, and so on. until everything is compared and swapped, at which point, you have a bubble sorted listbox.

Whew!
*Wipes His Forehead*

Crap, I see that, when I get home from work, I'll work on switching around the dates.

I write simple date sort, hope you like it

it should work correctly..... all I did was take the nested for loop for months, and move it above the nested for loop for days, so instead of:

Dim I As Integer
Dim X As Integer
Dim IDay As String
Dim XDay As String
Dim IMonth As String
Dim XMonth As String
Dim IYear As String
Dim XYear As String

' /* Sort By Order Of Days*/
For I = 0 To UBound(DList)
    For X = 0 To UBound(DList)
        IDay = Left(DList(I), 2)
        XDay = Left(DList(X), 2)
        If XDay < IDay Then
            Call Swap(DList(), I, X)
        End If
    Next X
Next I

' /* Sort By Order Of Months */
For I = 0 To UBound(DList)
    For X = 0 To UBound(DList)
        IMonth = Mid(DList(I), 4, 2)
        XMonth = Mid(DList(X), 4, 2)
        If XMonth < IMonth Then
            Call Swap(DList(), I, X)
        End If
    Next X
Next I

' /* Sort By Order Of Years */
For I = 0 To UBound(DList)
    For X = 0 To UBound(DList)
        IYear = Right(DList(I), 4)
        XYear = Right(DList(X), 4)
        If XYear < IYear Then
            Call Swap(DList(), I, X)
        End If
    Next X
Next I

I made it:

Dim I As Integer
Dim X As Integer
Dim IDay As String
Dim XDay As String
Dim IMonth As String
Dim XMonth As String
Dim IYear As String
Dim XYear As String

' /* Sort By Order Of Months */
For I = 0 To UBound(DList)
    For X = 0 To UBound(DList)
        IMonth = Mid(DList(I), 4, 2)
        XMonth = Mid(DList(X), 4, 2)
        If XMonth < IMonth Then
            Call Swap(DList(), I, X)
        End If
    Next X
Next I

' /* Sort By Order Of Days*/
For I = 0 To UBound(DList)
    For X = 0 To UBound(DList)
        IDay = Left(DList(I), 2)
        XDay = Left(DList(X), 2)
        If XDay < IDay Then
            Call Swap(DList(), I, X)
        End If
    Next X
Next I


' /* Sort By Order Of Years */
For I = 0 To UBound(DList)
    For X = 0 To UBound(DList)
        IYear = Right(DList(I), 4)
        XYear = Right(DList(X), 4)
        If XYear < IYear Then
            Call Swap(DList(), I, X)
        End If
    Next X
Next I

And that seems to work properly...

edit: oops, nevermind, still working on it.

Creative Visal, and Well done.

Regardless of how nice visal's is ;), I'm going to post new code that works on dates regardless of their format. This is still a bubble sort, but it uses a niffty built in Date module, and calls the datediff method. Date diff returns a number (in either days, years, etc, depending on the first parameter), and compares 2 dates, and returns the difference (in our case, in days) between them. This removes the need for you to add a module, and it also significantly lowers sort time, because there is only 1 nested for loop, and not 3. I'm keeping the Swap function, but I'm sticking it in the form, instead of in a module (to be honest, I like the swap function). Just so you are clear, it loops through each element, just like before, and compares it to each element, but it uses the datediff function, then it checks if the datediff function, returned a negative value (it checks for -, meaning negative of course). If the value returns negative, then we swap the elements of the array. If you want to sort this backwards, so that smallest is on top (or, earliest rather), then just replace the = to a <> in the if statement, and it will sort them the opposite direction... Here is the project.

First of all I would like to say wow. Then thanks and wow again.

:D

invisal's is very short but it brings in alotta stuff I don't understand. So I will use Comatose's as I can understand more about what is happening there and I should find it alot more easier to annotate. I have to pretty much explain every line in my project so it appears like I know what am doing.

Comatose, thanks for the lengthy replies (perhaps too in depth) it is all apprieciated, I really didn't think I would get any answers, but clearly you are very talented and kind people here.

Thanks again, now I just have to write an analysis, design, user guide, implementation and a evaluation :mad: and I only got 10 days to do it...

:cry:

More help please.

Comatose, you define your list boxes items in your code.

Dim X(9) As String
Dim Retval As String
Dim tmp As String
Dim I As Integer
Dim R As Integer

X(0) = "21/10/2004"
X(1) = "22/11/1995"
X(2) = "23/10/2004"
X(3) = "24/05/2005"
X(4) = "25/03/1996"
X(5) = "26/04/2004"
X(6) = "27/09/1993"
X(7) = "28/07/1995"
X(8) = "29/12/1992"
X(9) = "30/10/2001"

Mine can't be pre defined, they are loaded up from a saved file. You can add and delete bookings from this file and the results are displayed in the list box. How do I get your code to manipulate the information in the list box.

:idea: I would prefer to use a command button called order and when its clicked it would read all the current items in the list box and order them.

Ideas? :-|

I had written a long explaination... but power went out, and computer rebooted..... and I'm not re-writing it. Basically, I introduced "dynamic arrays" which are just like normal arrays, but they can change size, by using the redim keyword. We set the array to be the same size as the listbox, we take everything in the listbox, stick it in the array, sort the array, clear the listbox, and put the sorted information back into the listbox....

Wheres tmpdate been defined? When I try to implement it in my code as this variable hasn't been defined it won't work, yet it works in your code and I don't know where you have defined it.

For Each tmpDate In DateArray
List1.AddItem tmpDate
Next tmpDate

I forgot to declare it.... just stick it up at the top with the other dim's... define it as string. (You use option explicit, which forces you to implicitely declare your variables.... I don't.)

I forgot to declare it.... just stick it up at the top with the other dim's... define it as string. (You use option explicit, which forces you to implicitely declare your variables.... I don't.)

I thought that so in your code for a test I put option explicit in at the top and dim tmpdate as string and I get this complie error when I run and click the button:

For each control variable on arrays must be varient

:?:

EDIT: I dimmed it as a Variant and then it worked :D

I have never used a varient variable before, whats it do e.t.c?

Variants are a Data type... just like string, integer, long, etc. Actually, a variant lets VB decide what kind of data type to make it. It even allows for some change in the variable type. It's a bulky variable, and really is not good programming practice, because it will bulk your code. Not to mention that no other programming language in the world (including VB.NET) supports this type of behavior. For each uses variants, because you can for each through an array of any kind of data in the world you want. The array can be integers, or strings, or longs.... makes no difference. So the control variable (the one that represents the element of the array in the loop), has to be of a type that VB can choose on a whim. If you do a for each on an array of strings, then the control variable will be a string..... if you do a for each on an array of integers, then the control variable will be integer... My suggestion is to only use them though, when VB requires to.

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.