Solved

VBA IF Statement in MS Access Not Evaluating Properly

Posted on 2014-03-11
4
578 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
[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
  • 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 58
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
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…

688 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