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

Find Alpha in a string and replace with Zeros

Status
Not open for further replies.

ynnepztem

Programmer
Aug 2, 2001
54
US
I need help with a probably simple issue. I have a 6 character field that should only contain numbers. When the field is filled VIA the keyboard, stroke by stroke there is no problem because I do a check on every KeyUp. However, a value may be pasted from another app and it may contain alpha characters. I need help reading the string one character at a time changing each alpha to a Zero.

Thanks for any help you can provide.
 
Validate the field using something like this:


Dim Test As String
Dim Test2 As String
Test = "92MW1r"
For i = 1 To Len(Test)
If Not IsNumeric(Mid$(Test, i, 1)) Then
Test2 = Test2 & "0"
Else
Test2 = Test2 & Mid$(Test, i, 1)
End If
Next
MsgBox Test & " = " & Test2 Swi
 
Off the top of my head I'd say loop through the string using the mid$ function and evaluate each character with the isnumeric function. If you find a character thats not numeric use the replace function.

strLen = 0
strLen = Len(string)
a=1
do until a > strLen
eachchar = Mid(string,a,1)
If not isnumeric(eachchar) then
Replace(string,eachchar,0,a,1[,compare]]])
end if
a=a+1
loop

I know there will be 50 post saying there is a better way but that's quick and dirty.
 

>I need help reading the string one character at a time changing each alpha to a Zero

Why would you want to do that?

On the text box Change event, just check if the new value in the text box IsNumeric.
If not, then either clear the text box, or use the text box's UnDo feature (API function, or use a Static variable).

[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top