If Statement (1 Viewer)

Andy_CD

Bumbling Idiot
Local time
Today, 13:26
Joined
Feb 6, 2017
Messages
23
Hi,
I am trying to set an if statement to prevent someone from printing a label unless they have enter all the correct information, under certain conditions. So my statement below works when the condition is enabled, but it makes no difference if they enter data into the two other fields or not.
What I want to happen is if they put data in both fields for it to carry on to the printing code. Currently if me.drug.column(1)= 1 then it always goes to ULM.

If Me.Drug.Column(1) = 1 And Me.Batch_Number Or Me.Manufacturer Is Null Then GoTo ULM

ULM: MsgBox "Please enter Batch Number and Manufacturer before proceeding", vbOKOnly + vbExclamation, "Unlicensed Medicine"

End
 

moke123

AWF VIP
Local time
Today, 08:26
Joined
Jan 11, 2013
Messages
4,047
try
Code:
If Me.Drug.Column(1) = 1 And isnull(Me.Batch_Number) Or isnull(Me.Manufacturer) Then GoTo ULM
 

Andy_CD

Bumbling Idiot
Local time
Today, 13:26
Joined
Feb 6, 2017
Messages
23
Thank you so much, that worked perfectly!
 

Minty

AWF VIP
Local time
Today, 13:26
Joined
Jul 26, 2013
Messages
10,387
I would avoid getting into the habit of using Goto if you possibly can, especially in this example, as it can lead to very messy and difficult to debug programming. The only exception is error handling. In this instance your code could simply be;
Code:
If Me.Drug.Column(1) = 1 And IsNull(Me.Batch_Number) Or IsNull(Me.Manufacturer) Then 
         
        MsgBox "Please enter Batch Number and Manufacturer before proceeding", vbOKOnly + vbExclamation, "Unlicensed Medicine"

End If
 

Andy_CD

Bumbling Idiot
Local time
Today, 13:26
Joined
Feb 6, 2017
Messages
23
Hhhmmm, the problem I am now having is that moke123's code now works even when Me.Drug.Column(1) = 0.
Is there a way around this?
Thanks for the tip Minty, I have tried this but it seems to still allow the printing.
 

Minty

AWF VIP
Local time
Today, 13:26
Joined
Jul 26, 2013
Messages
10,387
I think it will in certain circumstances - you may need to put some brackets around things as the AND and OR may be evaluating not how you think. Also do you have any zero length strings ( "" ) in either the fields you are checking null for? Null is NOT =to ""

A more resilient way is to check for both

If Len(Me.BatchNumber & "") >0 Then... (It's not null)
 

Andy_CD

Bumbling Idiot
Local time
Today, 13:26
Joined
Feb 6, 2017
Messages
23
Thanks the brackets seem to have done the trick
 

Users who are viewing this thread

Top Bottom