Solved

VBA IF Statement in MS Access Not Evaluating Properly

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

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
 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now