Solved

Conditional logic not working

Posted on 2015-02-24
27
97 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40628450
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
ID: 40628460
They are Variants.... values come from a text box on a form.
0
 

Author Comment

by:thandel
ID: 40628467
If set as a integer I get a type mismatch error when setting the value for SupplyODCheck  and SupplyOSCheck
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 26

Expert Comment

by:Nick67
ID: 40628476
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40628481
@thandel

did you use the code i posted above?
0
 

Author Comment

by:thandel
ID: 40628489
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
ID: 40628491
Can then change the value from a string to an number? Or is there a better solution?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40628507
<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
ID: 40628513
I'm sorry did you mean:

Option Explicit
Dim SupplyODCheck as String

Let me try.....
0
 

Author Comment

by:thandel
ID: 40628522
Using Option Explicit causes other errors in the code.  Am I understanding correctly?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40628534
see my post at http:#a40628450
0
 

Author Comment

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

Author Comment

by:thandel
ID: 40628572
Sorry I'm slow.... just a moment let me try your full solution.....
0
 

Author Comment

by:thandel
ID: 40628576
Still having a type mismatch
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40628580
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
ID: 40628584
Its a type mismatch when the text box is null
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40628590
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
ID: 40628596
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
ID: 40628658
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
ID: 40628664
Actually not null but "".
0
 

Author Comment

by:thandel
ID: 40628667
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
ID: 40628672
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40628678
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
ID: 40628686
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
ID: 40628691
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
ID: 40628707
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
ID: 40628737
Thanks that makes sense (now)  :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

685 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