[RESOLVED] Getting the middle number
My problem is how can I get the value of the middle number in a textbox?
For example:
1.
Number: 563921
The Middle Number is: 39
2.
Number: 63942
The Middle Number is: 394
I want the middle number to be squared (39^2) and displayed it in a textbox.
Note:
The middle number must be atleast 2 digit number. Meaning, if the length of the number is even number, the middle number has 2 digit while if the length is odd number then the middle number is 3 digit.
Re: Getting the middle number
VB Code:
Private Sub Command1_Click()
MsgBox GetMiddleNumber("123456789")
MsgBox GetMiddleNumber("12345678")
End Sub
Private Function GetMiddleNumber(sNum As String) As Long
If Len(sNum) Mod 2 = 0 Then
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
Else
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
End If
End Function
Re: Getting the middle number
oops.. sorry:
VB Code:
Text1 = GetMiddleNumber("123456789") ^ 2
Re: Getting the middle number
What is the meaning of CLng?
Re: Getting the middle number
Convert to long...
Clng
Cint
CDate
etc.. its so it converts it to a number for calculations ;)
Re: Getting the middle number
Can I use:
sNum = Val(txtKeyValue.Text)
Since it is the user who will input the value of the number?
Re: Getting the middle number
Yes. Val Or CInt/CLng or whatever type the sNum variable is.
Re: Getting the middle number
Clng = Convert to Long
Cint = Convert to Integer
CDate = Convert to Date
??? = Convert to String
Cstr ???
Re: Getting the middle number
Yes. To check out all conversions, goto to Object Browser in your VB and input "Conversion" in the search box. It will show you the complete Conversion class (methods, functions). You also have all other classes there.
Re: Getting the middle number
I used this code but nothing happens:
Private Function GetMiddleNumber(sNum As String) As Long
sNum = CStr(Val(txtKeyValue(0).Text))
If Len(sNum) Mod 2 = 0 Then
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
Else
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
End If
End Function
Private Sub cmdCompute_Click()
y = Val(txtLKeyValue.Text) - 1
For x = 0 To y
w = Len(txtKeyValue(x).Text)
If w <> Val(txtSKeyValue.Text) Then
MsgBox "Incorrect input"
txtKeyValue(x).SetFocus
Else
lblAnswer(0).Caption = GetMiddleNumber("123456789") ^ 2
End If
Next
End Sub
Re: Getting the middle number
The code is working, hehehe! I forgot to make the lblAnswer(x) visible. Sorry!
Another Problem:
When I input 3 digit number, it only displays the square of the last 2 digit number. It should be the 3 digit.
Re: Getting the middle number
just modified Static's code:
VB Code:
Private Sub Command1_Click()
'since you are passing the value entered by user in text box
'check before hand if the number entered is single digit or not
'like this
'
'If Len(Text1.Text) = 1 then Msgbox "Number too Small"
MsgBox GetMiddleNumber("123456789")
MsgBox GetMiddleNumber("12345678")
MsgBox GetMiddleNumber("123")
MsgBox GetMiddleNumber("12")
End Sub
Private Function GetMiddleNumber(sNum As String) As Long
Select Case Len(sNum)
Case 2, 3: GetMiddleNumber = CLng(sNum)
Case Else
If Len(sNum) Mod 2 = 0 Then
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
Else
GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
End If
End Select
End Function
Re: Getting the middle number
Problem Solved!!! Thanks!
Re: [RESOLVED] Getting the middle number
what is the use of this txtLKeyValue.Text and txtSKeyValue.Text ??
so it means you have
txtKeyValue arrays
lblAnswer arrays
txtLKeyValue.Text ??
txtSKeyValue.Text ??
Re: [RESOLVED] Getting the middle number
I think it's really better to use a control array rather that
text1.text upto how many textbox you want to do ..... but in a label no need to array it .....
Re: [RESOLVED] Getting the middle number
i tried the code in post # 10
i created an control array of
txtKeyValue textboxes
lblAnswer labels
and a cmdCompute command button
like this illustration below:
txtKeyValue lblAnswer
[====] [====]
[====] [====]
[====] [====]
[Enter]
now where do i put the txtLkeyValue and txtSkeyValue ??
and what does it do ??
can anyone answer??