?
Solved

Routine to Create a New Revision

Posted on 2006-05-20
3
Medium Priority
?
255 Views
Last Modified: 2010-05-19
Hello Experts,

I'm coding in VB6.

Does anyone know of a function where I can create a new sequential Alpha revision/version after passing a current revision (imagine a document control system where document revisions are roled upon checking in a new doc version.) For example, say the current revision of a document is 'B' and the function would have to return the next revision which would be 'C'. That part is simple enogh. (I expect the ASC/CHR functions would be used as part of this function).

However, the tricky part comes in being able to handle when the current Rev is 'Z'. it needs to return the next revision as 'AA'. Then, subsequent revisions would be 'AB', 'AC', 'AD' ...etc. Then, once again on 'AZ, we need to return 'BA', 'BB', 'BC' etc. There should be no practical limit to the uppermost revision (e.g. a revision may exists as 'AAA' ). This will no doubt call for some type of recursion.

I've assigned a high point value to reflect the urgency of the request, which is high.

Thanks much in advance.
0
Comment
Question by:KentKirimli
3 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16725510
Hi KentKirimli,

I have used this method a couple of years ago for Excel columns:

' *****************************************************************
' ***
' *** Retourne la colonne suivante dans Excel
' *** Eric Moreau 1998.09.14
' ***
' *****************************************************************
Public Function NextNomColumn(ByVal pstrCol As String) As String
Dim strFirst As String
Dim strLast As String

    If Len(pstrCol) > 1 Then strFirst = Left$(pstrCol, 1)
    strLast = Right$(pstrCol, 1)
   
    If strLast = "Z" Then
        If strFirst = "" Then
            strFirst = "A"
        Else
            strFirst = Chr$(Asc(strFirst) + 1)
        End If
        strLast = "A"
    Else
        strLast = Chr$(Asc(strLast) + 1)
    End If
   
    NextNomColumn = strFirst & strLast
End Function

Cheers!
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16725997
Here is a generic routine that can handle any size revision and is easily configurable with respect to the characters included and the order of those characters:

Option Explicit

Private Sub Command1_Click()
    Text1.Text = NextRevision(Text1.Text)
End Sub

Private Function NextRevision(Optional ByVal curRevision As String) As String
    Dim i As Integer
    Dim char As String
    Dim chars As String
    Dim index As Integer
    Dim charArray() As String
   
    ' modify the line below to include the characters you
    ' want in the sequence
    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
   
    ' blank revision passed in, return first letter in sequence
    curRevision = Trim(UCase(curRevision))
    If curRevision = "" Then
        NextRevision = Left(chars, 1)
        Exit Function
    End If
   
    ' make sure current revision has only valid characters in it
    ' otherwise...return first letter in sequence
    For i = 1 To Len(curRevision)
        char = Mid(curRevision, i, 1)
        If InStr(chars, char) = 0 Then
            MsgBox "Returning: " & Left(chars, 1), vbCritical, "Invalid Starting Revision: " & curRevision
            NextRevision = Left(chars, 1)
            Exit Function
        End If
    Next i
   
    ' build a character array from the current revision
    ReDim charArray(Len(curRevision) - 1)
    For i = 1 To Len(curRevision)
        charArray(i - 1) = Mid(curRevision, i, 1)
    Next i
   
    ' see if the just the last letter needs to be "incremented"
    char = charArray(UBound(charArray))
    index = InStr(chars, char)
    If index < Len(chars) Then
        charArray(UBound(charArray)) = Mid(chars, index + 1, 1)
        NextRevision = Join(charArray, "")
        Exit Function
    End If
               
    ' last letter "rolled over"
    ' propagate the "carry over" as far as it needs to go
    charArray(UBound(charArray)) = Left(chars, 1)
    For i = UBound(charArray) - 1 To 0 Step -1
        char = charArray(i)
        index = InStr(chars, char)
        If index < Len(chars) Then
            index = index + 1
            charArray(i) = Mid(chars, index, 1)
            NextRevision = Join(charArray, "")
            Exit Function
        Else
            charArray(i) = Left(chars, 1)
        End If
    Next i
   
    ' "carry over" propagated all the way thru sequence
    ' add first letter of sequence to the beginning of the revision
    NextRevision = Left(chars, 1) & Join(charArray, "")
End Function
0
 

Author Comment

by:KentKirimli
ID: 16794135
Thanks Idle Mind, thats exactly what I was looking for.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

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