Solved

Coding question

Posted on 2014-10-23
11
201 Views
Last Modified: 2014-10-28
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
0
Comment
Question by:urjudo
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 100 total points
ID: 40400081
I'd think you'd need this:

     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
    End If
    
    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
    
    stDocName = "rptshowtest"
    DoCmd.OpenReport stDocName, acNormal

Open in new window

Essentially, you're exiting the Sub after each "check", so the only way you'd get to that OpenReport call would be if all the previous checks validate.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40400093
The third If starting with  --  If (C8 = True  -- will never be executed since it immediately follows an Exit sub.
0
 

Author Comment

by:urjudo
ID: 40400166
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.
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 300 total points
ID: 40400227
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
End If

If C9 = True or C8 = True Then
    If IsNull(IncLoc) Then
        MsgBox "Please enter Location before proceeding."
        Me.IncLoc.SetFocus
        Exit Sub
    End If
End If

If C8 = True Then
    If IsNull(IncDate) Then
        MsgBox "Please enter Inc Date before proceeding."
        Me.IncDate.SetFocus
        Exit Sub
    End If
End if    

Open in new window


It isn't clear whether these checkboxes are mutually exclusive or not.  If they are, they should be a single Option Group control with 9 options rather than 9 separate checkboxes.
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 100 total points
ID: 40400287
try this


 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 1."
       Exit Sub
       
       ElseIf (C8 = True And IsNull(IncDate) Or IsNull(IncLoc)) Or (C9 = True And IsNull(IncLoc)) Then
          MsgBox "Please enter all informations before proceeding 2."
          Exit Sub
       'End If
    Else
        stDocName = "rptshowtest"
       DoCmd.OpenReport stDocName, acNormal
     
    End If
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:urjudo
ID: 40400426
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.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40400485
did you notice that I changed the message, there is 1 or 2 at the end of the message?

which message are you getting?
0
 

Author Comment

by:urjudo
ID: 40400523
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
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40400582
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

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40400616
<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.
0
 

Author Comment

by:urjudo
ID: 40400649
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 .
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

705 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

20 Experts available now in Live!

Get 1:1 Help Now