+ Reply to Thread
Results 1 to 6 of 6

VBA SQL if Statement

  1. #1
    Registered User
    Join Date
    07-20-2011
    Location
    Missouri, USA
    MS-Off Ver
    Excel 2007
    Posts
    36

    VBA SQL if Statement

    I am trying to test 2 sql statements to see which has a value first and put it onto the worksheet

    Please Login or Register  to view this content.
    The main problem is that sSQL sometimes has no matches and there is null, but for some reason the if statement doesn't work at all. either that or the rs.fields(0).value doesn't even exist so it cannot even be null.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: VBA SQL if Statement

    See what value the variable contains by sending it to a message box on the one before your if statement:
    Please Login or Register  to view this content.
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Registered User
    Join Date
    07-20-2011
    Location
    Missouri, USA
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: VBA SQL if Statement

    The value for the if part doesn't seem to work. It continues to come back as '0' and does not update to the new SQL statement.

    And the message box value doesn't return if the SQL statement doesn't find anything.

    I also tried the following:
    Please Login or Register  to view this content.
    Last edited by popfan; 07-20-2012 at 01:14 PM.

  4. #4
    Registered User
    Join Date
    07-20-2011
    Location
    Missouri, USA
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: VBA SQL if Statement

    Just tried one more thing:

    Please Login or Register  to view this content.
    I don't even get the msgbox back

  5. #5
    Registered User
    Join Date
    07-20-2011
    Location
    Missouri, USA
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: VBA SQL if Statement

    I tried running both SQL scripts without any if statements and seems that the first SQL script cannot be over written by the second script.

  6. #6
    Registered User
    Join Date
    07-20-2011
    Location
    Missouri, USA
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: VBA SQL if Statement

    Would it be easier to create a second function and call each one individually and do the if then checks in the second function?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1