?
Solved

Coding question

Posted on 2014-10-23
11
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 400 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 38

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 38

Accepted Solution

by:
PatHartman earned 1200 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 400 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
 

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 38

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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 …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

752 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