Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA IF Statement in MS Access Not Evaluating Properly

Posted on 2014-03-11
4
Medium Priority
?
585 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 1200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
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 …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

618 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