Link to home
Start Free TrialLog in
Avatar of urjudo
urjudoFlag for United States of America

asked on

Coding question

Hi Experts,
I have a question regarding the message when user try to print a report.  I have a pop up form for user to enter the criteria in order to print out a report, below is my coding:

  If C1 = False And C2 = False And C3 = False And C4 = False And C5 = False And C6 = False And C7 = False And C8 = False  And C9 = False Then
        MsgBox "Please select a type."
        Exit Sub
     End If
   
    If IsNull(DNo) Or IsNull(showDate) Then
       MsgBox "Please enter all informations before proceeding."
       Exit Sub
       
       If (C8 = True And IsNull(IncDate) Or IsNull(IncLoc)) Or (C9 = True And IsNull(IncLoc)) Then
          MsgBox "Please enter all informations before proceeding."
          Exit Sub
       End If
    Else
       stDocName = "rptshowtest"
       DoCmd.OpenReport stDocName, acNormal    
    End If

I have check boxes fro c1 to c9, however, from c1 to c7, user only need to enter "DNo" and "ShowDate", c8 user need to enter "DNo, showdate, IncDate, IncLoc" and c9 user need to enter "Dno, showDate, IncLoc".  The code I have above, it skip the second message, if I enter DNo and ShowDate then check C8, the message did not pop up and the report was printing.  can someone help?  Thanks
SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The third If starting with  --  If (C8 = True  -- will never be executed since it immediately follows an Exit sub.
Avatar of urjudo

ASKER

I tried Scott McDaniel's suggestion,
  If (C8 = True And IsNull(IncDate) Or IsNull(IncLoc)) Or (C9 = True And IsNull(IncLoc)) Then
        MsgBox "Please enter all informations before proceeding."
        Exit Sub
    End If
work for C8
but
If IsNull(DNo) Or IsNull(showDate) Then
        MsgBox "Please enter all informations before proceeding."
        Exit Sub
    End If
is not work for from C1 to C7, even I entered the ShowDate, it still gave the message.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of urjudo

ASKER

I tired PatHartman, it works, I also tried Rey Obrero, for some reason, the  
 If IsNull(DNo) Or IsNull(showDate) Then
        MsgBox "Please enter all informations before proceeding 1."
        Exit Sub
  is not working, even I enter both fields, it still gave me the message.
did you notice that I changed the message, there is 1 or 2 at the end of the message?

which message are you getting?
Avatar of urjudo

ASKER

Yes, I did.  it was
MsgBox "Please enter all informations before proceeding 2."

when I tried to print the C1 to C7. (I entered the DNo and did not enter the ShowDate, then I got the message1, the I entered the ShowDate and I got the message 2
I guess I'm confused.  You said the code I rewrote worked.  It is also logically simpler than your or Rey's versions.

If you separate the If's as I did, you don't need to keep repeating the condition as you did in your code and as Rey did in his version.  If the first or second condition fails, you exit the sub so by the time you get to the third and fourth conditions, you only have the final additional fields to check.

Also notice in the last two conditions, the user gets a more specific message and I position the cursor to the field I want him to change.  You can only do that by isolating each condition as a separate expression.

If this code is in the Form's BeforeUpdate event (as it should be), you should also be cancelling the event before you exit so the record won't accidentally get saved with bad data.

So, in my applications, each individual edit check looks like:
If some condition is false then
        MsgBox "Please enter something before proceeding."
        Me.txtSomeField.SetFocus
        Cancel = True
        Exit Sub
End If 

Open in new window

<when I tried to print the C1 to C7. (I entered the DNo and did not enter the ShowDate, then I got the message1, the I entered the ShowDate and I got the message 2 >

so, the coding is correct.

if you think the coding is not correct,

post the rules or logic that need to be followed.
Avatar of urjudo

ASKER

for pathartman, i appreciated your answer, it works.  but i like to test different way so that in the future, i can use different way to program. also i put this code on click coz my form has the dno field, showdate field, incdate field and incloc field.
for Rey obrero,  the problem is from c1 to c7 user only need to enter dno and showdate, like i said, i entered dno abd left the showdate, it gave me the message1 which is correct but if i enter the dno and showdate and click "ok" , it should print the report but it did not print the report instead gave me message 2, the message 2 should be for c8 or c9 .