[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Alphabetic code Generation from Numbers

Posted on 2004-10-31
12
Medium Priority
?
287 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
Comment
Question by:sunil_khatnani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12457200
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
ID: 12458373
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
ID: 12458422
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Expert Comment

by:David Lee
ID: 12458541
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
ID: 12459220
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
ID: 12459229
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
ID: 12462643
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
ID: 12462658
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
ID: 12462729
BlueDevilFan  & JR2003 codes
also give the same Run time error "2385" while referring a Text1.text or Text2.text
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12463034
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
ID: 12463329
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
ID: 12464140
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

656 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