Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB 6 - If Condition Statement not working as desired

Status
Not open for further replies.

sunnydreams

Technical User
Oct 30, 2005
5
US
Software Used: VB 6
Database: SQL Server

PROBLEM DESC:

I have the following code as an example: (This code needs to work in a generic scenario also, I have just taken some example here like "Employeeid =123", but in general any Employeeid should work and cannot be hard coded)

If szSenderID = "sss" And dict_L11.Item("employeeIdQualifier") = "zz" And IsNumeric(Left(dict_L11.Item("employeeId"), 1)) = True Then
dict_L11.Item("employeeId") = "AB" & dict_L11.Item("employeeId")

When I execute this code, I get the following results:

IF Employeeid= 123 it gives result as "AB123" ----> Works as desired
if employeeid= 1g3 it gives result as "AB1g3" ----> Works as desired
if employeeid= g12 it gives result as "g12" ---------> PROBLEM AREA, PREFIX IS NOT GETTING ADDED here
IF employeeid =aa2 it gives result as "aa2" ----->Works as desired

DETAILED PROBLEM DESC:

I have a strange criteria that I am facing now. ( In this code for everything we will add a prefix "AB" )


Suppose if they provide employee ID : 123, then it should add prefix "AB " and it will become "AB123" ---> Works as desired

Suppose if they provide the employee id as "1g3", then also it should add a prefix "AB1g3" ---> Works as desired

Suppose if they provide the employee id as "g12" then also it should add a prefix "ABg12" ---> THIS IS THE STEP IT IS NOT WORKING

Suppose if they provide an employee id as "aa2", then it should not add any prefix to the result and result should be "aa2" ----> Works as desired.

WHAT I AM LOOKING FOR:

What changes should I make to the code condition in order for the 3rd criteria to work?


Can u pls help me.

Thx in advance
 
The problem you are having is because for that employeeid the leftmost character is not numeric, and that is one of your checks. I can't see any test similar to that which you are performing that will match just the first three and not the fourth, unless you can check that at least one of the first two characters are numeric:
Code:
If szSenderID = "sss" And dict_L11.Item("employeeIdQualifier") = "zz" And [red]([/red]IsNumeric(Left(dict_L11.Item("employeeId"), 1 [red]or IsNumeric(Mid(dict_L11.Item("employeeId"), 2, 1)))[/red] Then
dict_L11.Item("employeeId") = "AB" & dict_L11.Item("employeeId")
Note that I also took out the "= True" part of the "IsNumeric" condition. It is completely redundant. InNumeric returns a Boolean value. Saying [tt]If True = True[/tt] is the same as saying [tt]If True[/tt] and [tt]If False = True[/tt] is the same as [tt]If False[/tt].

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Not sure but I suspect that the IsNumeric is playing a significant role here.

 
Let us try this one..

am checking whether the first two chars in the string are numeric or not..

Code:
[blue]
Text2.Text = IIf((Not IsNumeric(Left(Text1.Text, 1)) And Not IsNumeric(Mid(Text1.Text, 2, 1))), "", "AB") & Text1.Text
[/blue]

to check this code, put it in a command button's click event, put two textboxes in the form. Type your string in the first text box and click the command button. Watch the result in the second textbox.

My results..

[red]
123 = "AB123"
1g3 = "AB1g3"
g12 = "ABg12"
aa2 = "aa2"
[/red]

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Yeah, I agree with vbSun. Looks like your logic can be most easily expressed as "add the string prefix if either or both of the first two characters are numeric." Although personally I would pull the Not out of the expression:
Code:
IIf(Not(IsNumeric(Left(Text1.Text, 1)) And IsNumeric(Mid(Text1.Text, 2, 1))), "", "AB")
Whether you do so or not is a matter of style, but I prefer using and not here to using or as tracy does. Again, that's also a matter of style, no criticism of tracy's way of doing things intended.

Clearly, though, your code isn't checking the second character, so you're not getting the behavior you want when only the second character is numeric.

Bob

Bob
 
Or, of course, the RegExp solution ;-)
Code:
[blue]Private Function Prefix(ByVal strExp As String) As String
    With New RegExp
        .Pattern = "((^\d\w{2}$)|(^\w\d\w$))"
        Prefix = .Replace(strExp, "AB$1")
    End With
End Function[/blue]
 
Bob, I agree. And that will be the optimized solution. Because in the previous one, two expressions needs to be calculated and needs to be NOTed. In the solution that you had proposed, only once the expression is getting NOTed.

But then, (with the combined NOT solution) I was not getting the char|num|char (ex : 1g3) format to work as expected.. That is why I NOTed both the expressions.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Very careless of me, and I apologize. I should have tested that!
Code:
IIf(IsNumeric(Left(Text1.Text, 1)) Or IsNumeric(Mid(Text1.Text, 2, 1)), "AB", "")
(in other words, tracy's solution) is what I came up with. strongm's code works too (of course), and has the added bonus of restricting the string to 3 characters.

Just for regular expression practice, I'm going to see if I can break down strongm's post correctly. Perhaps he will be so good as to correct any errors I make. Here's the entire pattern again:
Code:
((^\d\w{2}$)|(^\w\d\w$))

First, the construct patternA|patternB means "either patternA or patternB." I'll start with the left pattern:
Code:
^\d\w{2}$
^ = beginning of string
\d = any digit
\w = any alphanumeric
{2} = previous character filter times 2, so here any two alphanumerics
$ = end of string
Therefore, a string of 3 characters, consisting of any digit followed by any two alphanumerics.

Given the above, the right pattern
Code:
^\w\d\w$
can be seen to denote a string of three characters, the first and third of which are any alphanumeric, and the second of which is a digit.

So, the whole pattern is "3 alphanumeric characters, either the first or second of which must be a digit."

Now, for the replace function: the replace string "AB$1" replaces the input string, if it matches the pattern, with the literal "AB" followed by the input string. $1 denotes the first (in this case the only) group of pattern match characters in parentheses. As such, in this case it denotes any string matching the entire pattern.

I removed the inner parens
Code:
(^\d\w{2}$|^\w\d\w$)
and found no change to the behavior. I messed around a bit, and find that $2 corresponds to the first inner paren group, and $3 to the second one. So, for example, the replace string "AB$2" replaces "2qx" with "AB2qx", but the string "q2x" with simply "AB". This is because "q2x" does match the pattern, but does not match the pattern defined in $2.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top