Solved

Conditional logic not working

Posted on 2015-02-24
27
91 Views
Last Modified: 2015-02-24
I have the following code that is not working.  I can't figure out why:

            SupplyODCheck = Replace(Nz(Me.ODSupply, 0), " Month", "")
            SupplyOSCheck = Replace(Nz(Me.OSSupply, 0), " Month", "")

            If SupplyODCheck >= 12 Or SupplyOSCheck >= 12 Then
                Me.YearSupply = True
            Else
                Me.YearSupply = False
            End If

When: SupplyODCheck  = 6, SupplyOSCheck  = 0

However each time it executes its hitting the first line and setting year supply True when its false.
0
Comment
Question by:thandel
  • 16
  • 6
  • 5
27 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
how did you declare the variables "SupplyODCheck", "SupplyOSCheck"

dim SupplyODCheck as integer, SupplyOSCheck as integer

SupplyODCheck = cint(Replace(Nz(Me.ODSupply, 0), " Month", ""))
SupplyOSCheck = cint(Replace(Nz(Me.OSSupply, 0), " Month", ""))
0
 

Author Comment

by:thandel
Comment Utility
They are Variants.... values come from a text box on a form.
0
 

Author Comment

by:thandel
Comment Utility
If set as a integer I get a type mismatch error when setting the value for SupplyODCheck  and SupplyOSCheck
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
You don't say what is in Me.ODSupply or Me.OSSupply
That doesn't make matters easy!
But you are using Replace
SupplyODCheck = Replace(Nz(Me.ODSupply, 0), " Month", "")
and that's a STRING function
Replace(expression, find, replace[, start[, count[, compare]]])
So, you are saying replace " Month" with ""
That still leaves you with a string of some sort, but I can't say because you haven't supplied the original values of Me.ODSupply or Me.OSSupply
Now you are checking against integers
If SupplyODCheck >= 12 Or SupplyOSCheck >= 12 Then

But the values in SupplyODCheck  and SupplyOSCheck are -- because of Replace -- strings
Option Explicit should be at the top of each module and each variable declaration should have a type
Dim SupplyODCheck as String

But, supply more code and more detail of the values involved
If set as a integer I get a type mismatch error when setting the value for SupplyODCheck  and SupplyOSCheck
Absolutely.
Replace is a string function.
What are you replacing?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
@thandel

did you use the code i posted above?
0
 

Author Comment

by:thandel
Comment Utility
The text boxes have values like "XX Month" where XX and be a value in .5 increments from 1 to 24.  I was just trying to pull the value from the text box to get the number by removing the " Month"... it is returning the correct value but I guess as a string???
0
 

Author Comment

by:thandel
Comment Utility
Can then change the value from a string to an number? Or is there a better solution?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
<Can then change the value from a string to an number? Or is there a better solution? >

@thandel

did you use the code i posted above?
0
 

Author Comment

by:thandel
Comment Utility
I'm sorry did you mean:

Option Explicit
Dim SupplyODCheck as String

Let me try.....
0
 

Author Comment

by:thandel
Comment Utility
Using Option Explicit causes other errors in the code.  Am I understanding correctly?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
see my post at http:#a40628450
0
 

Author Comment

by:thandel
Comment Utility
Did you want them set as an integer?  I still have an error that was as well.
0
 

Author Comment

by:thandel
Comment Utility
Sorry I'm slow.... just a moment let me try your full solution.....
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:thandel
Comment Utility
Still having a type mismatch
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Using Option Explicit causes other errors in the code
It doesn't cause them, it exposes them.
What Option Explicit does, is it REQUIRES you to declare all variables before you use them

works without Option Explicit:
'------------------------
sub MySub
x = 1
Msgbox x
end sub

works with Option Explicit
'--------------------
Option Explicit
sub MySub
Dim x as Integer 'declaration is required!
x = 1
Msgbox x
end sub

'Another post coming shortly
0
 

Author Comment

by:thandel
Comment Utility
Its a type mismatch when the text box is null
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Now--to your problem
The text boxes have values like "XX Month"
Ok, replace is fine
dim SupplyODCheck as integer
dim  SupplyOSCheck as integer
SupplyODCheck = CInt(Replace(Nz(Me.ODSupply, 0), " Month", ""))
SupplyOSCheck = CInt(Replace(Nz(Me.OSSupply, 0), " Month", ""))

CInt will coerce the string from your Replace to an integer
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Its a type mismatch when the text box is null
Where?
Nz(Me.ODSupply, 0) should take care of ODSupply (or OSSupply with its Nz) being null
0
 

Author Comment

by:thandel
Comment Utility
Yes that is there the error is occuring, of type mismatch.  I know doesn't make sense given the NZ function.
0
 

Author Comment

by:thandel
Comment Utility
Actually not null but "".
0
 

Author Comment

by:thandel
Comment Utility
I tried this and it seems to be working:

            If Len(Me.ODSupply) > 1 Then SupplyODCheck = CInt(Replace(Nz(Me.ODSupply, 1), " Month", ""))
            If Len(Me.OSSupply) > 1 Then SupplyOSCheck = CInt(Replace(Nz(Me.OSSupply, 1), " Month", ""))
0
 

Author Comment

by:thandel
Comment Utility
In fact this works with SupplyOD/OSCheck as a string:

            If Len(Me.ODSupply) > 1 Then SupplyODCheck = Replace(Nz(Me.ODSupply, 0), " Month", "")
            If Len(Me.OSSupply) > 1 Then SupplyOSCheck = Replace(Nz(Me.OSSupply, 0), " Month", "")
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
Revising my codes

dim SupplyODCheck as integer
dim  SupplyOSCheck as integer
SupplyODCheck = Cint(IIF(Me.ODSupply & ""="",0,Replace(Me.ODSupply, " Month", ""))
SupplyOSCheck = Cint(IIF(Me.OSSupply & ""="",0,Replace(Me.OSSupply, " Month", ""))
0
 

Author Comment

by:thandel
Comment Utility
I'll use your code as an Int and using Cint as that seems the better  way of coding  :)
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Actually not null but "".
Now, look at your Nz()
Nz(Me.ODSupply, 0)
You are supplying 0, not "0"
And when you feed 0 into Replace - which is looking for a string! -- bad things happen, yes?
And look at the documentation for Replace

Return Values

Replace returns the following values:

If Replace returns
expression is zero-length Zero-length string ("")
expression is Null An error.
find is zero-length Copy of expression.
replace is zero-length Copy of expression with all occurences of find removed.
start > Len(expression) Zero-length string.
count is 0 Copy of expression.

So, yes, if you feed Replace "", you are going to get ""
And then you are going to try to coerce that to integer
Cint("") and that goes bang!
0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
When you are taking user-generated input, and doing something with it, no matter what platform, it is ALWAYS a good idea to test what the user gives you for validity!

Your textboxes may need AfterUpdate events to kick the user in the teeth if they enter garbage, or this procedure you've got should kick out if the inputs are bad.  GIGO.
0
 

Author Comment

by:thandel
Comment Utility
Thanks that makes sense (now)  :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now