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

Code 2

Status
Not open for further replies.

DUF

Technical User
Mar 10, 2001
107
IE
I'am new to VB6 and need help.
I want to enter up to 5 Numbers into a Text Box and when that Box has Lost focus it will convert the numbers into characters from the word "INSCRUTABX".
as in : I=1,N=2,S=3,C=4,R=5,U=6,T=7,A=8,B=9,X=0
I am not sure how to go about this and would apreciate any help i just started VB6.

thanks

Duf
 
Private Function NumConvert() As String
Dim cResult As String
Dim cTemp as String
Dim i As integer

For i = 0 to Len(TextBox1.Text)-1
cTemp = cResult & Mid(TextBox1.Text,1,1)
Select Case (cTemp)
Case 1
cResult = cResult & "I"
Case 2
cResult = cResult & "N"
Case 3
cResult = cResult & "S"
Case 4
cResult = cResult & "C"
Case 5
cResult = cResult & "R"
Case 6
cResult = cResult & "U"
Case 7
cResult = cResult & "T"
Case 8
cResult = cResult & "A"
Case 9
cResult = cResult & "B"
Case 0
cResult = cResult & "X"
End Select
Next i
NumConvert = cResult
End Function

This should do the trick. Keep in mind that you will want to do some data validation on the textbox, as well as limit the input to your 5 digits.

Good Luck,
Mike Take Care,
Mike
 
Or, as an alternative (MichaelBronner's comments about data validation etc. still hold good) :

Private Function MatchNumber(strNumber As String) As String
Dim Converter() As String
Dim lp As Long

Converter = Split("X I N S C R U T A B", " ")
For lp = 1 To Len(strNumber)
MatchNumber = MatchNumber & Converter(CLng(Mid(strNumber, lp, 1)))
Next
End Function

Private Sub Text1_LostFocus()
Text1.Text = MatchNumber(Text1.Text)
End Sub
 
Hats off to you once again StrongM :)

Thanks for that nifty little trick. I don't understand it all the way, but I'll look up the Split command and see how it works :) Take Care,
Mike
 
Thanks for the code guy's
But I should have explained better, as in when I input 2356 into the text box the result is NCRU or 10000 the result is IXXXX or 23 the result is NS.
MichaelBronner code works but only for the first character and strongm code I get a run time error 9 (subscript ou of range)
Can you help again please I am able to understand part of the codes as I am not upto that stage yet I need some more help

Thanks

Duf
 
Duf,

Did you do a straight copy-and-paste of my code? Or did you retype it? If you retyped it, examine the following line very carefully:

Converter = Split("X I N S C R U T A B", " ")

The second parameter here is a space character; it you have managed to accidentally change it to:

Converter = Split("X I N S C R U T A B", "")

(i.e. second parameter is a null string) then you will see the error that you are describing.
 
IF you're still interested inmy code, change

cTemp = cResult & Mid(TextBox1.Text,1,1)

to this

cTemp = cResult & Mid(TextBox1.Text,i,1)


However, StrongM's Code is much better. Hopefully you can get his to run. Take Care,
Mike
 
ack... sorry, here's the correct answer:

change

cTemp = cResult & Mid(TextBox1.Text,1,1)

to this

cTemp = Mid(TextBox1.Text,i,1)

Take Care,
Mike
 
Hi,
That's Done the Job I must have Typed it in for some reason
Thanks ever much lads it give me two ways to study the action.
Now another problem, can the code be adjusted to handle a decimal point as in 12.99 or 1.99 or 0.99 but ignore it in the result. Also I need to Divide the Number I enter into Text Box1 by 0.787654 and convert that result the same way and put the result into a text box 5.
Once again thanks it's a great help.
 
Not that you need it, but this code is a little more efficient...

strMasterString = "XINSCRUTAB"
For intCount = 1 To Len(strInput)
intConvert = (Mid(strInput, intCount, 1) + 1)
strMatch = strMatch & Mid(strMasterString, intConvert, 1)
Next
 

Thanks Shepherd,
As I am Learning there are a lot of different ways to go about a action, It's another one to study.Any idea on the decimal point??

Duf
 
Private Function NumConvert() As String
Dim cResult As String
Dim cTemp as String
Dim i As integer
Dim cString As String

cString = "XINSCRUTAB"
For i = 1 To Len(TextBox1.Text)
cTemp = Mid(TextBox1.Text, i, 1)
If(cTemp = ".") Then
cResult = cResult & "."
Else
cResult = cResult & Mid(cString, i, 1)
Next i
NumConvert = cResult
End Function

I adopted shpeherds code. (I was thinking it over last night and thought an array might work better, but shepherd had beat me to it.) :) Take Care,
Mike
 
Thanks
any ideas on the secound text box??

Duf
 
Change the convert function to:

Private Function NumConvert(cText As String) As String
Dim cResult As String
Dim cTemp as String
Dim i As integer
Dim cString As String

cString = "XINSCRUTAB"
For i = 1 To Len(cText)
cTemp = Mid(cText, i, 1)
If(cTemp = ".") Then
cResult = cResult & "."
Else
cResult = cResult & Mid(cString, i, 1)
Next i
NumConvert = cResult
End Function

Then add the following:

Private Sub TextBox1_LostFocus()
TextBox2.Text = TextBox1.Text / 0.787654
TextBox1.Text = NumConvert(TextBox1.Text)
TextBox2.Text = NumConvert(TextBox2.Text)
End Sub


That should do it. Take Care,
Mike
 
Mike
I have cut and pasted you code into my form and ran it and I got an error "Compile error Next without For"
What am I doing wrong????


Duf
 
ack, add this line before the line "Next i":

End If Take Care,
Mike
 
Actually - since we seem to be in a code efficiency battle, and it may be an area worth worth exploring - Sheperd's code is actually less efficient. It uses two Mid$ in the loop, and Mid$ is an 'expensive' function.

As for handling the decimal point, just change my routine to (and, on the basis of my comments on mid$ I have unrolled that particular cost):

Private Function MatchNumber(strInput As String) As String
Dim Converter() As String
Dim lp As Long
Dim strTest As String

Converter = Split("X I N S C R U T A B", " ")
For lp = 1 To Len(strInput)
strTest = Mid(strInput, lp, 1)
If strTest = "." Then
MatchNumber = MatchNumber + "."
Else
MatchNumber = MatchNumber & Converter(CLng(strTest))
End If
Next
End Function
 
(on the other hand I've been evil by mixing the usage of '+' and '&')
 
Hi Guy's,
It's working fine now. Thanks to MichaelBronner and strongm and to Sheperd's for all the different code it'a interesting to note all the different ways to tackle a problem and one way more efficient then the other.

If there is an efficiency battle on let the best Programmer win '''''''THERE IS A PRIZE''''''''' To the winner.

Thanks it works ok all I have to do now is figure out how to send the information in textbox 1,2 to the printer.
Any ideas guy's????

Duf
 
That's fairly simple:

Printer.Print "1st Number: " & TextBox1.Text
Printer.Print "2nd Number: " & TextBox2.Text
Printer.EndDoc Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top