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!

Stumped on a simple conditional 2

Status
Not open for further replies.

spamjim

Instructor
Mar 17, 2008
1,368
US
I am picking up a VB6 project. My experience is limited to PHP/Python.

The following code is confusing me. Everything is evaluating to be true and the ItmRun variable is changed to 49.57 and then 55.52 when stepping through with the following variables:
ItmRun = 10.00
gType = "BBB "
gSeries = " 12"
gPDim = ""
gStep = "B2 "


Code:
  If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And gPDim < 0.062 And Trim(gStep) = "B2" Then
    ItmRun = 49.57
  End If
  If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And gPDim < 0.062 And Trim(gStep) = "G2" Then
    ItmRun = 55.52
  End If

Even though the fed variables should not evaluate to true for any of these statements, I end up with 55.52 at the end. Can someone point out what's wrong?

VB6 syntax is completely foreign to me. Are spaces and tabs at the start of the line, or unusual line endings/returns, the culprit here?
 
Solved, but still uncertain how.

I pulled the "gPDim < 0.062" out from each conditional statement and applied it to the group of them. It works now.
 
I am uncertain as well. If I run the following (which is just the code you have shown us), I get the expected result.
Code:
[blue]Public Sub test()
    ItmRun = 10#
    gType = "BBB "
    gSeries = " 12"
    gPDim = ""
    gStep = "B2 "
    
    If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And gPDim < 0.062 And Trim(gStep) = "B2" Then
        ItmRun = 49.57
    End If
    If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And gPDim < 0.062 And Trim(gStep) = "G2" Then
        ItmRun = 55.52
    End If
    MsgBox ItmRun
End Sub[/blue]

This suggests that there may be more code than you have shown us. In particular I suspect that you have a) DIMmed the variables and b) have an On Error Resume Next[sup]1[/sup] line. Most specifically I suspect that you have Dimmed as follows:

[tt]Dim gPDim as String[/tt]

whereas it looks like you should really

[tt]Dim gPDIM as Double[/tt]



[sup]1[/sup]WHilst error handling is important, I would argue that On Error Resume Next is NOT error handling, it is ignoring errors, and should be used sparingly and with extreme caution.
 
That's why it would be nice to know the [red]red[/red] portion of your code...

Code:
Option Explicit

Private Sub test()
    Dim ItmRun As [red]???[/red]
    Dim gType As [red]???[/red]
    Dim gSeries As [red]???[/red]
    Dim gPDIM As [red]???[/red]
    Dim gStep As [red]???[/red]

    ItmRun = 10.00
    gType = "BBB "
    gSeries = " 12"
    gPDIM = ""
    gStep = "B2 "
    
    If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And gPDIM < 0.062 And Trim(gStep) = "B2" Then
        ItmRun = 49.57
    End If
    If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And gPDIM < 0.062 And Trim(gStep) = "G2" Then
        ItmRun = 55.52
    End If
    MsgBox ItmRun

End Sub


---- Andy

There is a great need for a sarcasm font.
 
Thanks for the insight. I assumed that gPDim is a string as the (empty) value displayed at the breakpoint was wrapped in doublequotes. The ItmRun was without doublequotes so that must have been a true number type.

Digging around, I found:

Public ItmRun As Currency
Public gType As String
Public gSeries As String
Public gPDim As String
Public gStep As String

As a string, maybe it was evaluating if gPDim was less than everything to the right ("0.062 And Trim(gStep) = "B2" Then") and failing.

To best evaluate if "gPDim < 0.062" should I multiply gPDim by one to feed into a real number datatype variable, and then check if it is less than 0.062?

Whatever I did by moving the condition outside of the others appears to be working, but I don't trust it to be the ideal solution.
 
>As a string, maybe it was evaluating if gPDim was less than everything to the right

Nope

>Whatever I did by moving the condition outside of the others appears to be working

Erm, did you ignore my post? :-(
 
Event if you have this code, it cannot run because of the error "Type mismatch" because of the code in [red]red[/red]

Code:
Option Explicit
Public ItmRun As Currency
Public gType As String
Public gSeries As String
Public gPDim As String
Public gStep As String

 Sub test()
    ItmRun = 10#
    gType = "BBB "
    gSeries = " 12"
    gPDim = ""
    gStep = "B2 "
    
    If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And [red]gPDim < 0.062[/red] And Trim(gStep) = "B2" Then
        ItmRun = 49.57
    End If
    If Trim(gType) = "VVV" And (Trim(gSeries) = 18 Or Trim(gSeries) = 25) And [red]gPDim < 0.062[/red] And Trim(gStep) = "G2" Then
        ItmRun = 55.52
    End If
    MsgBox ItmRun

End Sub

Do you have a sample of your code that would compile, run, and produce the result you described?


---- Andy

There is a great need for a sarcasm font.
 
No, your post was understood and much appreciated.

The problem is that I'm picking up a mess. I don't want to change much as it will likely break many other things. This nearly 2-decade old program is due to retire in a few months so I'm trying not to rock the boat.

"On Error Resume Next" appears in many locations. Even with my infant knowledge, I can see the use is inappropriate. The program was designed by monkeys.
 
Here's the reason for the insanity.

The Public Sub for the stuff I'm reviewing had a "On Error GoTo" the following...

Code:
...
Case 13 'type mismatch
  Resume Next
...

Previous developers knew about type mismatches and just gave up. I think I should too and tell the boss to retire this program now. [bigsmile]
 
You don't realize how well I understand your (unfortunate) situation [wink]

What I usually do in those situations is:
1. Put a break point before a questionable code and run my code to that point
2. In VB IDE I go to Tools - Options - General tab, and set Error Trapping to "Break on All Errors" option
3. Step thru the code. You will stop on all errors this way.
4. If all is good, I re-set the Error Trapping to any other 2 options and continue going.

Good luck (and curse those monkeys) [thumbsup2]

This may (?) fix your Type Mismatch problem:
[tt][blue]Val([/blue]gPDim[blue])[/blue] < 0.062 [/tt]

---- Andy

There is a great need for a sarcasm font.
 
>should I multiply gPDim by one to feed into a real number datatype variable, and then check if it is less than 0.062?

Fails with simple edge case (the one we are currently looking at, in fact, where gPDim is an empty string)

>This may (?) fix your Type Mismatch problem Val(gPDim) < 0.062

Fails on the same edge condition.

It is the edge condition that is causing the problem. If gPDim contains a string representing a number of any sort, then VBA will happily coerce it to a numeric type (of course, this is also just hiding the problem) and the conditional will work

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top