Results 1 to 9 of 9

Thread: Repeating Numbers

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    Repeating Numbers

    Do anyone have any code that could tell if a series of nine numbers are repeating? I am checking to see if a SSN is all repeating numbers. For example, a person's SSN is 999999999.

    Thanks
    Shannon

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Repeating Numbers

    VB Code:
    1. Const TESTDATA = "999999999"
    2.     Dim lngIndex As Long
    3.     Dim bDifferent As Boolean
    4.    
    5.     For lngIndex = 1 To 9
    6.         If Mid$(TESTDATA, lngIndex, 1) <> Left$(TESTDATA, 1) Then
    7.             bDifferent = True
    8.             Exit For
    9.         End If
    10.     Next
    11.    
    12.     If bDifferent Then
    13.         MsgBox "They are not the same"
    14.     Else
    15.         MsgBox "They are the same"
    16.     End If

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    Re: Repeating Numbers

    But it could be any numbers from 0-9. Would you have to create a function for each? I thought about testing positions 1 & 2 to see if they are the same and if they aren't exit the function and if they are test position 2 & 3 and then repeat the process. Would that work?

    Thanks
    Shannon

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Repeating Numbers

    Just convert it to a string, and loop through checking to see if all of the characters are the same.

    VB Code:
    1. Private Function IsRepeating(lNumber As Long) As Boolean
    2.  
    3.     Dim lTemp As Long, sTest As String, iCount As Integer
    4.    
    5.     IsRepeating = True
    6.     lTemp = Left$(CStr(lNumber), 1)
    7.     sTest = CStr(lNumber)
    8.     For iCount = 1 To Len(sTest)
    9.         If Mid$(sTest, iCount, 1) <> CStr(lTemp) Then
    10.             IsRepeating = False
    11.             Exit For
    12.         End If
    13.     Next iCount
    14.    
    15. End Function

    EDIT: Looks like MartinLiss beat me to it.
    Last edited by Comintern; Nov 7th, 2005 at 09:58 AM. Reason: Ooops!

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Repeating Numbers

    Quote Originally Posted by odamsr
    But it could be any numbers from 0-9. Would you have to create a function for each? I thought about testing positions 1 & 2 to see if they are the same and if they aren't exit the function and if they are test position 2 & 3 and then repeat the process. Would that work?

    Thanks
    Shannon
    I just took what Martin posted, and put it into a Function. Just enter the number string you wish to test in text1.text
    VB Code:
    1. Option Explicit
    2.  
    3. Private Function AreNumbersAlike(plngCheckNum As Long) As Boolean
    4. Dim lngIndex As Long
    5. Dim bDifferent As Boolean
    6.    
    7.     For lngIndex = 1 To 9
    8.         If Mid$(plngCheckNum, lngIndex, 1) <> Left$(plngCheckNum, 1) Then
    9.             bDifferent = True
    10.             Exit For
    11.         End If
    12.     Next
    13. AreNumbersAlike = bDifferent
    14. End Function
    15.  
    16. Private Sub Command1_Click()
    17. If AreNumbersAlike(Val(Text1.Text)) Then
    18.    MsgBox "They are different"
    19. Else
    20.    MsgBox "They are all the same"
    21. End If
    22. End Sub
    Modify as necessary.

  6. #6
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Re: Repeating Numbers

    Just glanced over this thread..but if you are checking for valid SSN's to see if someone is just putting in bogus numbers...you could also just check the two center numbers. Aren't they the State? Those numbers could only be fifty different numbers. Sorry if I missed the point.

  7. #7
    Member
    Join Date
    Aug 2005
    Posts
    37

    Re: Repeating Numbers

    Quote Originally Posted by birthjay
    Just glanced over this thread..but if you are checking for valid SSN's to see if someone is just putting in bogus numbers...you could also just check the two center numbers. Aren't they the State? Those numbers could only be fifty different numbers. Sorry if I missed the point.

    No that's not true, so it wouldn't work.

  8. #8
    Addicted Member
    Join Date
    May 2005
    Posts
    168

    Re: Repeating Numbers

    VB Code:
    1. dim i as byte
    2.  
    3. for i=1 to 9
    4.    if sSSN = string$(9, cstr(i)) then
    5.       msgbox "Bogus SSN"
    6.       exit for
    7.    end if
    8. next i

  9. #9

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