Results 1 to 16 of 16

Thread: [RESOLVED] Getting the middle number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Resolved [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.
    Last edited by Hack; Aug 25th, 2006 at 08:23 AM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark
    To become a PROFESSIONAL,
    Start from SCRATCH...

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Getting the middle number

    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox GetMiddleNumber("123456789")
    3.     MsgBox GetMiddleNumber("12345678")
    4. End Sub
    5.  
    6. Private Function GetMiddleNumber(sNum As String) As Long
    7.     If Len(sNum) Mod 2 = 0 Then
    8.         GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
    9.     Else
    10.         GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
    11.     End If
    12. End Function
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Getting the middle number

    oops.. sorry:

    VB Code:
    1. Text1 = GetMiddleNumber("123456789") ^ 2
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting the middle number

    What is the meaning of CLng?
    To become a PROFESSIONAL,
    Start from SCRATCH...

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Getting the middle number

    Convert to long...
    Clng
    Cint
    CDate

    etc.. its so it converts it to a number for calculations
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    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?
    To become a PROFESSIONAL,
    Start from SCRATCH...

  7. #7

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting the middle number

    Clng = Convert to Long
    Cint = Convert to Integer
    CDate = Convert to Date
    ??? = Convert to String

    Cstr ???
    To become a PROFESSIONAL,
    Start from SCRATCH...

  9. #9
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    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
    To become a PROFESSIONAL,
    Start from SCRATCH...

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    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.
    To become a PROFESSIONAL,
    Start from SCRATCH...

  12. #12
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Getting the middle number

    just modified Static's code:
    VB Code:
    1. Private Sub Command1_Click()
    2.     'since you are passing the value entered by user in text box
    3.     'check before hand if the number entered is single digit or not
    4.     'like this
    5.     '
    6.     'If Len(Text1.Text) = 1 then Msgbox "Number too Small"
    7.    
    8.     MsgBox GetMiddleNumber("123456789")
    9.     MsgBox GetMiddleNumber("12345678")
    10.     MsgBox GetMiddleNumber("123")
    11.     MsgBox GetMiddleNumber("12")
    12. End Sub
    13.  
    14. Private Function GetMiddleNumber(sNum As String) As Long
    15.    
    16.     Select Case Len(sNum)
    17.         Case 2, 3: GetMiddleNumber = CLng(sNum)
    18.         Case Else
    19.             If Len(sNum) Mod 2 = 0 Then
    20.                 GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 2))
    21.             Else
    22.                 GetMiddleNumber = CLng(Mid(sNum, Len(sNum) / 2, 3))
    23.             End If
    24.     End Select
    25. End Function
    Show Appreciation. Rate Posts.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    Re: Getting the middle number

    Problem Solved!!! Thanks!
    To become a PROFESSIONAL,
    Start from SCRATCH...

  14. #14
    Junior Member
    Join Date
    Aug 2006
    Posts
    19

    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 ??

  15. #15
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    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 .....

  16. #16
    Junior Member
    Join Date
    Aug 2006
    Posts
    19

    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??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width