Alphabetic code Generation from Numbers

Dear Experts,
I am looking for Alphabetic code generation from Numbers in VB.
Say A-1,B-2,C-3,D-4,E-5,F-6,G-7,H-8,I-9,J-0

Now if I enter any value in one text box1-
then another text box2 should generate code for that value.

The value in Text box1 could be entered maximum upto 5 digits.?
IT should generate alphabetic code.
Say if i enter 520 in textbox1 then it should give EBJ in textbox2.?


sunil_khatnaniAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
If you are using MS Access then try this code instead:

Private codes As Collection

Private Sub Form_Load()
    ' A - 1, B - 2, C - 3, D - 4, E - 5, F - 6, G - 7, H - 8, I - 9, J - 0
   
    Set codes = New Collection
    codes.Add "A", "1"
    codes.Add "B", "2"
    codes.Add "C", "3"
    codes.Add "D", "4"
    codes.Add "E", "5"
    codes.Add "F", "6"
    codes.Add "G", "7"
    codes.Add "H", "8"
    codes.Add "I", "9"
    codes.Add "J", "0"
End Sub

Private Sub Command1_Click()
    Dim i As Integer
    Dim strText1 As String
    Dim alphabeticCode As String
   
    Text1.SetFocus
    strText1 = Text1.Text
    If Len(strText1) > 0 Then
        For i = 1 To Len(strText1)
            alphabeticCode = alphabeticCode & getLetter(Mid(strText1, i, 1))
        Next
        Text2.SetFocus
        Text2.Text = alphabeticCode
    Else
        Text2.Text = ""
    End If
    Command1.SetFocus
End Sub

Private Function getLetter(ByVal strNumber As String) As String
    On Error GoTo noSuchNumber
    getLetter = codes.Item(strNumber)
    Exit Function
   
noSuchNumber:
    MsgBox strNumber, vbCritical, "Unknown Code"
End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one approach:

Option Explicit

Private codes As Collection

Private Sub Form_Load()
    ' A - 1, B - 2, C - 3, D - 4, E - 5, F - 6, G - 7, H - 8, I - 9, J - 0
   
    Set codes = New Collection
    codes.Add "A", "1"
    codes.Add "B", "2"
    codes.Add "C", "3"
    codes.Add "D", "4"
    codes.Add "E", "5"
    codes.Add "F", "6"
    codes.Add "G", "7"
    codes.Add "H", "8"
    codes.Add "I", "9"
    codes.Add "J", "0"
End Sub

Private Sub Text1_Change()
    Dim i As Integer
    Dim alphabeticCode As String
   
    If Len(Text1.Text) > 0 Then
        For i = 1 To Len(Text1.Text)
            alphabeticCode = alphabeticCode & getLetter(Mid(Text1.Text, i, 1))
        Next
        Text2.Text = alphabeticCode
    Else
        Text2.Text = ""
    End If
End Sub

Private Function getLetter(ByVal strNumber As String) As String
    On Error GoTo noSuchNumber
    getLetter = codes.Item(strNumber)
    Exit Function
   
noSuchNumber:
    MsgBox strNumber, vbCritical, "Unknown Code"
End Function
0
 
Erick37Commented:
Not quite so fancy:

Function LetterFromNumber(ByVal num As Long) As String
    Dim chars As String
    If num < 1 Then Exit Function
    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    LetterFromNumber = Mid(chars, num, 1)
End Function
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Erick37Commented:
And modified to do what you requested:

Private Sub Text1_Change()

    Dim sOut As String
    Dim sTemp As String
    Dim i As Long
   
    For i = 1 To Len(Text1.Text)
        sTemp = Mid(Text1.Text, i, 1)
        If IsNumeric(sTemp) Then
            sOut = sOut & LetterFromNumber(CLng(sTemp))
        End If
    Next
   
    Text2.Text = sOut
       
End Sub


Function LetterFromNumber(ByVal num As Long) As String
    Dim chars As String
    If num = 0 Then num = 10
    chars = "ABCDEFGHIJ"
    LetterFromNumber = Mid(chars, num, 1)
End Function
0
 
David LeeCommented:
Here's a third alternative.

Private Sub Text1_Change()
    Dim strAlpha As String, _
        strNumber As String
    For x = 1 To Len(Text1.Text)
        strNumber = Mid(Text1.Text, x, 1)
        strNumber = IIf(strNumber = "0", "10", strNumber)
        strAlpha = strAlpha & Chr(64 + strNumber)
    Next
    Text2.Text = strAlpha
End Sub
0
 
JR2003Commented:
This function works. Don't ask me how as it's a bit obfusticated. But it works!
JR

Private Sub Command1_Click()

    Text1.Text = "520"
    Text2.Text  = ""

    Dim s As String
    Dim i As Long
    For i = 1 To Len(Text1.Text)
        Text2.Text  = Text2.Text & Chr((Asc(Mid$(Text1.Text, i, 1)) - Asc("0") + 9) Mod 10 + Asc("A"))
    Next i
    MsgBox s

End Sub
0
 
JR2003Commented:
Ignore the variable s in the last example it should be just this:

Private Sub Command1_Click()
    Text1.Text = "520"
    Text2.Text = ""
    Dim i As Long
    For i = 1 To Len(Text1.Text)
        Text2.Text = Text2.Text & Chr((Asc(Mid$(Text1.Text, i, 1)) - Asc("0") + 9) Mod 10 + Asc("A"))
    Next i
End Sub


0
 
sunil_khatnaniAuthor Commented:
I applied both the codes submitted
by Idle_Mind  &  Erick37
& i get the

following Runtime Error "2385"
which says you can't reference a property or method for a control unless the control has a focus.

Any suggestions to improve your functions work?
thanks
0
 
Ioannis ParaskevopoulosCommented:
Private Sub Command1_Click()
Text2.Text = ""
strnumbers = "1234567890"
strletters = "ABCDEFGHIJ"
For i = 1 To Len(Text1.Text)
Text2.Text = Text2.Text & Mid(strletters, InStr(1, strnumbers, Mid(Text1.Text, i, 1)), 1)
Next
End Sub
0
 
sunil_khatnaniAuthor Commented:
BlueDevilFan  & JR2003 codes
also give the same Run time error "2385" while referring a Text1.text or Text2.text
0
 
David LeeCommented:
I tested the code I posted in VB prior to posting and it worked fine.  If Idle_Mind is correct and you are using Access, then I'd like to suggest that in any future questions you make that clear up front.  
0
 
JR2003Commented:
sunil_khatnani

<<<< also give the same Run time error "2385" while referring a Text1.text or Text2.text

You just need to have 2 TextBoxes on the form named Text1 and Text2
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.