Solved

Alphabetic code Generation from Numbers

Posted on 2004-10-31
239 Views
Last Modified: 2010-05-02
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.?


0
Question by:sunil_khatnani
    12 Comments
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
     
    LVL 32

    Expert Comment

    by:Erick37
    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
     
    LVL 32

    Expert Comment

    by:Erick37
    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
     
    LVL 76

    Expert Comment

    by:David Lee
    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
     
    LVL 18

    Expert Comment

    by:JR2003
    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
     
    LVL 18

    Expert Comment

    by:JR2003
    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
     

    Author Comment

    by:sunil_khatnani
    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
     
    LVL 23

    Expert Comment

    by:Ioannis Paraskevopoulos
    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
     

    Author Comment

    by:sunil_khatnani
    BlueDevilFan  & JR2003 codes
    also give the same Run time error "2385" while referring a Text1.text or Text2.text
    0
     
    LVL 85

    Accepted Solution

    by:
    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
     
    LVL 76

    Expert Comment

    by:David Lee
    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
     
    LVL 18

    Expert Comment

    by:JR2003
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now