Results 1 to 9 of 9
  1. #1
    Carmine is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2019
    Posts
    11

    VBA Multiple Condition IF statement

    I am trying to create a VBA “if “statement with more than one condition. The following statement works fine:



    If DLookup("SynthD", "DensityT") = 0 Then MsgBox "SyntheticDensity = 0, is this correct?"


    BUT I want to include a second condition to evaluate “SynthPC”. Therefore, in order for the message box to appear, “SynthD” must = 0 and “SynthPC” must be >0.

    I tried the following which does not work:

    If DLookup("SynthD", "DensityT") = 0 and if DLookup("SynthPC","MasterTable") > 0
    then MsgBox "SyntheticDensity = 0, is this correct?"



    So what am I doing wrong?



    Thanks,
    Carmine

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    What does ‘ does not work’ mean?

  3. #3
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    Skip the second if. So
    Code:
    If DLookup("SynthD", "DensityT") = 0 and DLookup("SynthPC","MasterTable") > 0
    Groeten,

    Peter

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    DLookup will return Null if there is no match.

    Why is there no WHERE argument criteria? Does DensityT have multiple records? What you have will probably always pull from the same record.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Carmine is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2019
    Posts
    11
    Yes! Apparently al I had to do is remove the second "IF". It all works fine. Thank you all for your comments.

  6. #6
    Carmine is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2019
    Posts
    11
    Quote Originally Posted by CJ_London View Post
    What does ‘ does not work’ mean?
    "Does not work" means it gave me a compile error.
    Another participant suggested that I remove the second "IF".
    I did so and it works.

    Thank you for your response,

    Carmine

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Did you read post 4?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    suggested that I remove the second "IF".
    changing the and to a then would also work. Your code does not look like it has been copy/pasted so the extra if could just be a typo.

    Not using criteria in your dlookup implies each table only has, and will only ever have, one record. This may be the correct situation but if not it could return the wrong result which can also be described as ‘not working’.

    Hence the reason for my asking for clarification,

    for the future ‘not working’ means nothing- use code tags around copy/pasted code and provide the error code if that is the issue

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by Carmine View Post
    "Does not work" means it gave me a compile error.
    Another participant suggested that I remove the second "IF".
    I did so and it works.

    Thank you for your response,

    Carmine
    Would have been more helpful if you had said that in the first place.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 06-30-2023, 12:33 PM
  2. Replies: 7
    Last Post: 05-12-2018, 01:13 PM
  3. Replies: 4
    Last Post: 06-18-2015, 09:37 PM
  4. If Statement Condition - Opening Report
    By geraldk in forum Reports
    Replies: 3
    Last Post: 07-10-2013, 10:26 AM
  5. SDS 3 condition if statement
    By anonix in forum Access
    Replies: 1
    Last Post: 09-07-2012, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums