Solved

VBA IF Statement in MS Access Not Evaluating Properly

Posted on 2014-03-11
4
573 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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