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
Printable View
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
VB Code:
Const TESTDATA = "999999999" Dim lngIndex As Long Dim bDifferent As Boolean For lngIndex = 1 To 9 If Mid$(TESTDATA, lngIndex, 1) <> Left$(TESTDATA, 1) Then bDifferent = True Exit For End If Next If bDifferent Then MsgBox "They are not the same" Else MsgBox "They are the same" End If
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
Just convert it to a string, and loop through checking to see if all of the characters are the same.
VB Code:
Private Function IsRepeating(lNumber As Long) As Boolean Dim lTemp As Long, sTest As String, iCount As Integer IsRepeating = True lTemp = Left$(CStr(lNumber), 1) sTest = CStr(lNumber) For iCount = 1 To Len(sTest) If Mid$(sTest, iCount, 1) <> CStr(lTemp) Then IsRepeating = False Exit For End If Next iCount End Function
EDIT: Looks like MartinLiss beat me to it.
I just took what Martin posted, and put it into a Function. Just enter the number string you wish to test in text1.textQuote:
Originally Posted by odamsr
Modify as necessary.VB Code:
Option Explicit Private Function AreNumbersAlike(plngCheckNum As Long) As Boolean Dim lngIndex As Long Dim bDifferent As Boolean For lngIndex = 1 To 9 If Mid$(plngCheckNum, lngIndex, 1) <> Left$(plngCheckNum, 1) Then bDifferent = True Exit For End If Next AreNumbersAlike = bDifferent End Function Private Sub Command1_Click() If AreNumbersAlike(Val(Text1.Text)) Then MsgBox "They are different" Else MsgBox "They are all the same" End If End Sub
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.
Quote:
Originally Posted by birthjay
No that's not true, so it wouldn't work.
VB Code:
dim i as byte for i=1 to 9 if sSSN = string$(9, cstr(i)) then msgbox "Bogus SSN" exit for end if next i
odamsr, you might wajht to take a look at this thread.