Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create function to scramble name

Status
Not open for further replies.

phweston

Programmer
May 8, 2003
38
0
0
For starters, we have this code in SQL. We are trying to convert this to Access VBA.

The purpose of the funciton is to scramble names to make this unreadble.

The function will convert a name by taking each character and moving it up one in the alphabet. For example A would becomie B in the string, B would become C, and so on. the letter Z would become A.

For example: Bob would be turned into Cpc.

Here is the code in Access:

Public Function ScrambleName(str)

Dim CharIndex As Integer
Dim Strafter As String
Dim CharBefore As String
Dim CharAfter As String


CharIndex = 1
Strafter = " "
While (CharIndex <= Len(str))

CharBefore = String(str, CharIndex)
CharIndex = CharIndex + 1
If ((CharBefore) >= "a" And (CharBefore) < "z" Or (CharBefore) >= ("A") And (CharBefore) < ("Z")) Then
CharAfter = ((CharBefore) + 1)
ElseIf (CharBefore) = ("z") Then
CharAfter = ("a")
ElseIf (CharBefore) = "Z" Then
CharAfter = "A"
Else
CharAfter = CharBefore
End
Strafter = Strafter + CharAfter
End
End If
Wend
End Function

 



How about this. You also must UNscramble...
Code:
Function Scramble(s As String, Optional DoUndo As Boolean = True)
    Dim b As String, i As Integer
    
    For i = 1 To Len(s)
        b = Mid(s, i, 1)
        Select Case b
            Case "A" To "Z", "a" To "z"
                If DoUndo Then
                    Scramble = Scramble & NextChar(b)
                Else
                    Scramble = Scramble & PrevChar(b)
                End If
            Case Else
                Scramble = Scramble & b
        End Select
    Next
End Function
Function NextChar(s As String)
    Select Case s
        Case "Z"
            NextChar = "A"
        Case "z"
            NextChar = "a"
        Case Else
            NextChar = Chr(Asc(s) + 1)
    End Select
End Function
Function PrevChar(s As String)
    Select Case s
        Case "A"
            PrevChar = "Z"
        Case "a"
            PrevChar = "z"
        Case Else
            PrevChar = Chr(Asc(s) - 1)
    End Select
End Function

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top