|
-
Apr 11th, 2004, 06:18 PM
#1
Thread Starter
New Member
Or....
Hey,
I've got to validate a value in access, which I am doing in VB.
Basically I'm doing something Like this
Code:
If LeftP = "A" or "B" or "C" .....etc then
Some values like 0 and I will not be accepted since they are not allowed ( Car registation Number ) .
Anyway is there anyway to make Vb check more than one character in a If statement. Because I know how to do a simple
If Leftp= "A" then ...
Hope you understand.
-
Apr 11th, 2004, 06:22 PM
#2
There are few ways to do that:
VB Code:
If LeftP = "A" Or LeftP = "B" Or ... Then
Else
End If
Select Case LeftP
Case "A", "B", "C", ...
'...
Case Else
'...
End Select
-
Apr 11th, 2004, 06:34 PM
#3
Thread Starter
New Member
I guess I could do it via an array too, using a loop right... Then declare the numbers/ letters which could be accepted at the beginnning?
-
Apr 11th, 2004, 06:47 PM
#4
You may certainly use arrays or array of UDT... I don't, however, know what you are after.
-
Apr 11th, 2004, 07:04 PM
#5
Thread Starter
New Member
I think I'll just declare at the beginning something like.
Dim AcceptedValues 0 to 22 as String
Then delcare the accepted values like
AcceptedValues(0)= "A" and so on.
Then for the checking line
For I = 0 to 24
If LeftP= AcceptedValues(I) then
Msgbox= "Correct Value"
Else
Msgbox = "Entered incorrect Value"
End If
Next I
I know that code isn't 100% right.. I cant get the loop to work
-
Apr 11th, 2004, 07:18 PM
#6
I think you want "pattern matching". The Like operator might be what you need.
The following code indicates the registration number must
have a length of 6
first character can only be A, B, C
2nd can only be F,N,R
3rd X,Y,Z
4th, 5th and 6th - must be digits and cannot be 0
VB Code:
Dim strRegistrationNumber
strRegistrationNumber = "ARY173"
If strRegistrationNumber Like "[A-C][FNR][X-Z][1-9][1-9][1-9]" Then
Debug.Print "Reg # is valid"
Else
Debug.Print "Reg # is invalid"
End If
-
Apr 11th, 2004, 07:30 PM
#7
Thread Starter
New Member
Thanks brucevde + RhinoBull.
I think that should do the trick. I might have to edit the
"[A-C][FNR][X-Z][1-9][1-9][1-9]" to what I need it to be ( I have to do it in the Pre 2001 UK format which is slighty different to todays format.
-
Apr 11th, 2004, 08:10 PM
#8
Before you go off on that array solution, take a good look at RhinoBull's second suggestion in his first post. Using the Select Case statement can put all the options into one statement. Whether this is actually faster than looping through the arrays, would have to be tested, since I don't know how VB would implement the Select, but I would expect it to be faster.
-
Apr 12th, 2004, 08:43 AM
#9
I would say that Select Case statements are alot faster than looping through and using If...Then statements.
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Apr 12th, 2004, 04:58 PM
#10
It really depends on size of your array - you wouldn't notice difference if it's a small one but theoretically Select Case should be faster due to indexing.
-
Apr 12th, 2004, 06:54 PM
#11
Thread Starter
New Member
I would use case but I don't really understand how it works.
-
Apr 12th, 2004, 06:57 PM
#12
It isn't much harder to understand than If...Then statements
VB Code:
Dim str As String
str = "Hello World!"
Select Case str
Case "Hello World!": 'If str = "Hello World!" Then
MsgBox "Hello World!"
Case "Hello World 2!": 'If str = "Hello World 2!" Then
MsgBox "Hello World 2!"
End Select
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Apr 12th, 2004, 07:09 PM
#13
Thread Starter
New Member
Thanks. That makes it easier to understand.
Basically now I have three blocks of text
First is a block of 2 characters which is letter block ( No Numbers)
I can validate that it is simply letters by using access validation. But what I need to do is make sure certain letters are not used.
IE. O, l ..etc . Is there anyway I can scan the LeftP ( Left Part) for these characters and if they are contained in this part flag it up.
Thanks once again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|