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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace 5

Status
Not open for further replies.

Vec

IS-IT--Management
Jan 29, 2002
418
US
How would I replace letters with numbers in a text box? For example if the user entered A, it is replaced by 1




-------------------------------------------------------------------------
-------------------------------------------------------------------------

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Univer
 
You have the correct term right in your question.
Something like this...although there is probably a more efficient way of doing this:

Replace(strString, "a", 1)
Replace(strString, "b", 2)
etc, etc...

Good Luck
 
It depends on when you want it replaced. Do you want the letter replace as its being typed or when some other action occurs? Just a single occurance, if it was always in upper case, could be done with asc(letter)-64. this will give a listing bettween 1-26 for A-Z.
 
I am trying to do it with a cmd button, so far I am still stuck:

Private Sub Command1_Click()
Text1.Text = Replace(strString, "a", 1)
End Sub


-------------------------------------------------------------------------
-------------------------------------------------------------------------

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Univer
 
Are you trying to replace all letters with numbers? If so are "a" and "A" counted the same or differently. What numbers do you want to assigen to what letters besides "a" to 1?
 
basically like this
Case insensitive
a = 1
b = 2
c = 3
and so on
I want it to only allow letters and spaces, no numbers or any other keyboard chars.

This is not for any big encryption or anything, I am simply tring to learn how to do this.

-------------------------------------------------------------------------
-------------------------------------------------------------------------

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Univer
 
Dear Vec,

There is no point in building big algo for doing this, as i feel, if u want to restrict the user from typing chars you dont want, simply dont allow them to type. I feel it is a bad idea to let them type what ever they want and after typing (when will this happen?) replace all the unwanted chars.

Use this... I copied this from another post of mine...

Private Function IsValidCharacter(KeyAscii As Integer) As Boolean

Const Numbers$ = "0123456789-."

IsValidCharacter = True
If KeyAscii <> 8 Then
If (InStr(Numbers, Chr$(KeyAscii)) = 0) Then
IsValidCharacter = False
Exit Function
End If
End If

End Function

and in the textboxes keypress event,

Private Sub txtFields_KeyPress(Index As Integer, KeyAscii As Integer)

If Not IsValidCharacter(KeyAscii) Then KeyAscii = 0

End Sub

Are you through?
Sunil
 
vbSun - That approach does of course have merit, but it is based on the assumption that only way the text box is populated is via keyboard entry. There are other ways, such as cut and paste, which can be used to populate a text box, and such as approach could lead to needing multiple replacements after the paste.

I would consider using a loop something along the lines of the following which you can tailor to your needs. This is not meant to be efficient, but to illustrate a technique.

NewString = vbNullString
For Idx = 1 to Len(String)
CharVal = Asc(Ucase(Mid(String, Idx, 1)))
If CharVal < Chr(&quot;A&quot;) or CharVal > Chr(&quot;Z&quot;) Then
NewString = NewString & Chr(CharVal)
Else
NewString = NewString & CStr(CharVal - 64)
End If
Next Idx


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
We are getting off track a little, the main thing I am trying to do is convert the letters to numbers.

Case insensitive
a = 1
b = 2
c = 3
and so on


-------------------------------------------------------------------------
-------------------------------------------------------------------------

&quot;Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Univer
 
I'm not sure how off track we've gone, I see three different approaches have been alluded to, each with different pros and cons.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Vec, what do you want to do with letters J and beyond? Do you want, then, to replace a single character with a two-digit number?

a=1
b=2
...
i=9
j=10
...
y=25
z=26
 
Try this:
Code:
Function GetNumbers(Source As String) As String
  Const Letters = &quot;abcdefghijklmnopqrstuvwxyz&quot;
  
  Dim I As Integer
  Dim S As String
  Dim C As String
  
  S = Source
  For I = 1 To Len(Letters)
    C = Mid$(Letters, I, 1)
    S = Replace(S, C, I, , , vbTextCompare)
  Next
  GetNumbers = S

End Function
 
ok this might be a little messy but it will do what I think you are asking for


Private Sub Command1_Click()
'holds the integer value of the character
Dim som As Integer
'holds the number values as they are being brought together
Dim compile As String
'holds the string taken from the text box
Dim temp As String

temp = Text1.Text
'take off the characters one by one checking to see
'if they are in the correct range
For i = 1 To Len(temp) Step 1
'converts to upper case to avoid checking for lower case
som = Asc(UCase(Mid(temp, i, 1)))
If som <= Asc(&quot;Z&quot;) And som >= Asc(&quot;A&quot;) Then
'if in the right range subtract 64 to get to the
'correct number range by subtracting 64
compile = compile & Str(som - 64)

End If
Next i
'feed the compiled numbers back to the text box
Text1.Text = compile
End Sub

This will ignor spaces and number characters paying attention only to converting letters to numbers. If anything is unclear just reply.
 
Thanks for all your help so far, I think I am clear, one question further, how do I make it so it removes any spaces first in the entry of Text1 and then only looks at the first letter, skips one and looks at the next, skips another and so on so that if I entered Joe Blow it would remove the space to make it JoeBlow and then only use Jelw
to create the numbers from.

Private Sub Command1_Click()
Dim som As Integer
Dim compile, temp As String
Text1.Text = Replace(Text1.Text, &quot; &quot;, &quot;&quot;)
temp = Text1.Text
For i = 1 To Len(temp) Step 1
som = Asc(UCase(Mid(temp, i, 1)))
If som <= Asc(&quot;Z&quot;) And som >= Asc(&quot;A&quot;) Then
compile = compile & Str(som - 64)
End If
Next i
Text1.Text = Text1.Text + compile
Text1.Text = Replace(Text1.Text, &quot; &quot;, &quot;&quot;)
End Sub

-------------------------------------------------------------------------
-------------------------------------------------------------------------

&quot;Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Univer
 
A slight modification to the loop I presented about 9 or 10 posts back, adding a conditional to handle the spaces, and which positions, based on the value on the index variable should do the trick.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Spaces are automatically ignored in this code so all that is needed is a boolean to tell it whether to add a number or skip adding until the next.

Code:
Dim addNum as Boolean

Private Sub Command1_Click()
Dim som As Integer
Dim compile, temp As String
Text1.Text = Replace(Text1.Text, &quot; &quot;, &quot;&quot;)
    temp = Text1.Text
For i = 1 To Len(temp) Step 1
    som = Asc(UCase(Mid(temp, i, 1)))
If som <= Asc(&quot;Z&quot;) And som >= Asc(&quot;A&quot;) Then
    If Not addNum then
        compile = compile & Str(som - 64)
        addNum = true
    Else
        addNum = false
    End If

End If
Next i
    Text1.Text = Text1.Text + compile
    Text1.Text = Replace(Text1.Text, &quot; &quot;, &quot;&quot;)
End Sub
 
You could also substitute a test of i to see if it is an even number for the boolean. This saves some code and saves declaring your boolean variable.

For i = 1 To Len(temp) Step 1
som = Asc(UCase(Mid(temp, i, 1)))
If som <= Asc(&quot;Z&quot;) And som >= Asc(&quot;A&quot;) Then
If i Mod 2 <> 0 then 'get the remainder from dividing by 2 to test to see if this is an even number
'Add to compile only if i is an odd number
compile = compile & Str(som - 64)
End If
End If
Next i

Looks like you pretty much have it!
 
I'd be carefull using the i to indicate odd letters. The i is going through the whole message which means counting spaces as well which might make the algorithum hickup.

ex. &quot;to do right&quot; -> torgt -> &quot;some numbers&quot; instead of the expected tdrgt -> &quot;some numbers&quot;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top