Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Alphabetic code Generation from Numbers

Posted on 2004-10-31
Medium Priority
291 Views
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
• 3
• 2
• 2
• +3

LVL 86

Expert Comment

ID: 12457200
Here is one approach:

Option Explicit

Private codes As Collection

' A - 1, B - 2, C - 3, D - 4, E - 5, F - 6, G - 7, H - 8, I - 9, J - 0

Set codes = New Collection
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

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

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

LVL 76

Expert Comment

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

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

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

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

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

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

Mike Tomlinson earned 500 total points
ID: 12463034
If you are using MS Access then try this code instead:

Private codes As Collection

' A - 1, B - 2, C - 3, D - 4, E - 5, F - 6, G - 7, H - 8, I - 9, J - 0

Set codes = New Collection
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

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

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

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â€¦
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asseâ€¦
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â€¦
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
Course of the Month10 days, 10 hours left to enroll