Question : Use Loop to create sequential numbers

I have some data in a text field [SortDate]  and I need to append a 3 digit number to the end.

The data is currently in the format 11/22/2010 (it was a date, and to match newly created data has to stay in this format.) I would like to create a Loop that runs through my table, and adds a 3-digit number at the end: 11/22/2010001, 11/22/2010002, etc.

 It would then go to the next date and start numbering again:
11/23/2010001, 11/23/2010002, 11/23/2010003, etc.

Answer : Use Loop to create sequential numbers

Utilizing the same basic premise as fyed, I offer you this ...
----------------------------
Sub foo()

    Dim strSQL As String
    Dim strIndex As String
    Dim strTemp As String
    Dim intOffset As Integer

    'Assumes Autonumber ID field
    strSQL = "SELECT [SortDate] FROM [SomeTableName] ORDER BY [SortDate], [ID]"

    With CurrentDb.OpenRecordset(strSQL)
   
        Do Until .EOF
           
            'Extract the date portion of the SortDate taking into account that
            'this may be not be the first time the procedure has ran.
            strTemp = Split(Format(.Fields("SortDate"), "yyyy-dd-mm"), "|")(0)
           
            'Mark when a new date is encountered
            If strIndex <> strTemp Then
                strIndex = strTemp
                intOffset = .AbsolutePosition - 1
            End If
           
            'Edit the field you wish to use as the single field sort, but
            'have multiple values within that field.
            .Edit
            .Fields("SortDate") = strTemp & "|" & Format(.AbsolutePosition - intOffset, "00000")
            .Update
           
            'Go to the next record
            .MoveNext

        Loop
       
    End With
   
End Sub
---------------------

The reason I like this particular formulation of the SortDate field is that each item can be extracted by use of the separator (|).  Also, the date portion is in international format and will sort as expected and visually is easily recognized as a date.

But I have the same sentiments as Fyed and think there is a better way.  For example, the addition of a single date/time field that defaults to Now() {as I mentioned earlier}, or simply use a multi-key sort -- I know you indicated why you need the key in a single field, but you have not done so yet, soooo we'll still wonder why that requirement exists and continue to ask :)
Random Solutions  
 
programming4us programming4us