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
Solved

VBA IF Statement in MS Access Not Evaluating Properly

Posted on 2014-03-11
4
571 Views
Last Modified: 2014-03-11
I have some VBA Code in MS Access utilizing several IF Statements.  For some reason a couple of the IF Statements aren't evaluating properly.

Although me.cboMgrName evaluates to "Jones, T", it processes the 2nd IF Statement that is evaluating "Fakolujo, A"

ElseIf Me.cboMgrName = "Jones, T" And Me.cboDept Like "EGR*" And Me.cboRegion = "West" Or Me.cboRegion = "East-Central" Then  'for T Jones
    Do While Not rs.EOF
    If Len(varAddlEmail) > 0 Then varAddlEmail = varAddlEmail & "; "
        varAddlEmail = varAddlEmail & rs!EmailAddr_Indiv
    rs.MoveNext
    Loop
    
    Do While Not rs2.EOF
    If Len(varOE) > 0 Then varOE = varOE & "; "
       varOE = varOE & rs2!oe_email
    rs2.MoveNext
    Loop
    
    Do While Not rs3.EOF
    If Len(varMgrEmail) > 0 Then varMgrEmail = varMgrEmail & "; "
       varMgrEmail = varMgrEmail & rs3!Mgr_Email
    rs3.MoveNext
    Loop
    
    Do While Not rs4.EOF
    If Len(varEmpEmail) > 0 Then varEmpEmail = varEmpEmail & "; "
        varEmpEmail = varEmpEmail & rs4!Employee_Email
    rs4.MoveNext
    Loop
    
    Call SendEmailCompletedRptActionReqWithErrors(varEmpEmail, varMgrEmail, varOE, varAddlEmail)

ElseIf Me.cboMgrName = "Fakolujo, A" And Me.cboDept Like "EGR*" And Me.cboRegion = "West" Then  'for A Fakolujo
    Do While Not rs.EOF
    If Len(varAddlEmail) > 0 Then varAddlEmail = varAddlEmail & "; "
        varAddlEmail = varAddlEmail & rs!EmailAddr_Indiv
    rs.MoveNext
    Loop

    Do While Not rs2.EOF
    If Len(varOE) > 0 Then varOE = varOE & "; "
       varOE = varOE & rs2!oe_email
    rs2.MoveNext
    Loop

    Do While Not rs3.EOF
    If Len(varMgrEmail) > 0 Then varMgrEmail = varMgrEmail & "; "
       varMgrEmail = varMgrEmail & rs3!Mgr_Email
    rs3.MoveNext
    Loop

    Do While Not rs4.EOF
    If Len(varEmpEmail) > 0 Then varEmpEmail = varEmpEmail & "; "
        varEmpEmail = varEmpEmail & rs4!Employee_Email
    rs4.MoveNext
    Loop

    Call SendEmailCompletedRptActionReqWithErrors(varEmpEmail, varMgrEmail, varOE, varAddlEmail)

Open in new window


What am I doing wrong?

Thanks,
gdunn59
0
Comment
Question by:gdunn59
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 300 total points
ID: 39921001
Have you stepped through it to verify your variable values through the procedure?

One thing I see is that you should put this in parentheses so that it evaluates everything before this and one of these options.

(Me.cboRegion = "West" Or Me.cboRegion = "East-Central")
0
 
LVL 57
ID: 39921138
First, I would not hardcode names, departments, etc.   Consider using a table driven approach.

Second, watch where your calling the code from.  It is possible that one of the combo has not yet been updated.

Using the .Text property will get you the current value in the buffer, which may not be the same as the .Value if the control has not been updated as yet.

Third thing is a single space, punctuation mark, etc will throw this off.  Really suggest #1.

As jmiller suggest, put a STOP at the top of the procedure, then using F8 to step through.  Hover over things to get the value, or do:

? Me.cboMgrName

 in the debug window followed by a return to see the value.

Jim.
0
 
LVL 1

Author Comment

by:gdunn59
ID: 39921230
Yes, I did step through the code, that is how I determined that the me.cboMgrName was showing "Jones, T", but was skipping to the code for "Fakolujo, A".

As for using a table, this was something that I had to put together fairly quick.  As time permits, I plan on going back and setting up the table to reference.

Thanks,
gdunn59
0
 
LVL 1

Author Comment

by:gdunn59
ID: 39921234
I put in the parenthesis like jmiller1979 suggested (ID: 39921001), and they fixed the issue.

(Me.cboRegion = "East-Central" Or Me.cboRegion = "West")

Thanks,
gdunn59
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

828 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