TFCJamieFay
Active Member
- Joined
- Oct 3, 2007
- Messages
- 480
Hi All,
I'm having problems with the following bit of code.
It all works fine until it get to here...
...then I get the following message...
Run time error '5017':
Application defined or object defined error
Is it because I need to clear/reset the regular expression before setting a new one or something? I'm not really very familiar with this function so any help would be greatly appreciated.
Many thanks,
Jay
I'm having problems with the following bit of code.
Code:
Sub PostcodeChecker()
Dim RgExp As Variant
'Create the regular expression object
Set RgExp = CreateObject("VBScript.RegExp")
'Clear the function value
IsUKPostCode = ""
'Check we have value to test
If MyPC = "" Then
IsUKPostCode = "Not Supplied"
msg = MsgBox("The postcode that you inputted is " & IsUKPostCode, vbOKOnly _
+ vbCritical)
Exit Sub
End If
RgExp.Pattern = "(((^[BLMNS][1-9]\d?)|" _
& "(^(AL|B[ABDHLNRS]|C[ABFHMORTVW]|D[AEHLNTY]|E[NX]|FY|G[LUY]|" _
& "H[ADGPRUX]|I[GP]|KT|L[ADELNSU]|M[EK]|N[EGNPR]|O[LX]|" _
& "P[ELOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|" _
& "(^E[1-9])|" _
& "(^E1[W0-9])|" _
& "(^EC[5]0)|" _
& "(^EC[1][AMNRVY])|" _
& "(^EC[2][AMNPRVY])|" _
& "(^EC[3-4][AMNPRV])|" _
& "(^N1[1-9])|" _
& "(^NW1[W01])|" _
& "(^NW[1-9])|" _
& "(^SS[0-9])|" _
& "(^SW1[AEHPVWXY0-9])|" _
& "(^SW[2-9])|" _
& "(^W[2-9])|" _
& "(^W1[A-HJKSTUW0-4])|" _
& "(^WC[1][ABEHNRVX])|" _
& "(^WC[2][ABEHNR])" _
& ")(\s*)?" _
& "([0-9]))$|" _
& "(^GIR\s?0AA$)"
If RgExp.Test(MyPC) = True Then
'Application.Run "FindBranch"
MsgBox "MyPC = True"
Else
RgExp.Pattern = "(^G[1-9]\d?)|" _
& "(^ZE|KW|HS|IV|AB|PH|DD|PA|FK|KY|ML|EH|KA|DG|IM|BT|GY|JE)\d\d?|" _
& ")(\s*)?" _
& "([0-9]))$|" _
& "(^GIR\s?0AA$)"
If RgExp.Test(MyPC) = True Then ' ---------error here
MsgBox "Scottish or Non Mainland UK postcode"
Else
MsgBox "Invalid postcode"
End If
End If
End Sub
It all works fine until it get to here...
Code:
If RgExp.Test(MyPC) = True Then ' ---------error here
MsgBox "Scottish or Non Mainland UK postcode"
...then I get the following message...
Run time error '5017':
Application defined or object defined error
Is it because I need to clear/reset the regular expression before setting a new one or something? I'm not really very familiar with this function so any help would be greatly appreciated.
Many thanks,
Jay