+ Reply to Thread
Results 1 to 8 of 8

Need help with a S Q L statement inside VBA

  1. #1
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    808

    Need help with a S Q L statement inside VBA

    Please Login or Register  to view this content.

    Attached table: I am looking for persons with more than 50 apples or 40 pears, but no orange. If a person does not have all those three fruits listed in Fruit field, then the person should be ignored.

    How should I write such kind of S Q L statement to get what I need.

    It is fine to return persons only, but it is even better if return result includes apple quantity/pear quantity/orange quantity associated with that person. Orange quantity could be 0 or missing.

    Thanks.
    Attached Images Attached Images
    Last edited by VAer; 05-24-2018 at 03:37 PM.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with a S Q L statement inside VBA

    Are you actually doing this in excel? If so, how?

  3. #3
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    808

    Re: Need help with a S Q L statement inside VBA

    Quote Originally Posted by Kyle123 View Post
    Are you actually doing this in excel? If so, how?
    Sorry, the system does not allow users to post with some S Q L keywords, so I have to add some extra space.

    I did it all the time. I often use VBA to pull data from database. Basically, I put S Q L statement inside CVQuery, such as: CVQuery = " Se le ct * From TableName Where ......."

    That is it.

    Dim CVConnectionString As String
    Dim CVRs As ADODB.Recordset
    Dim CVQuery As String
    Dim CVField As Field

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with a S Q L statement inside VBA

    Let me rephrase the question, are you actually querying a real database? If so which one? Or are you querying another workbook?

  5. #5
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    808

    Re: Need help with a S Q L statement inside VBA

    Quote Originally Posted by Kyle123 View Post
    Let me rephrase the question, are you actually querying a real database? If so which one? Or are you querying another workbook?
    Real database, I pull data from Sybase, which is used in my workplace. But it should be able to pull other database.

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Need help with a S Q L statement inside VBA

    Is Quantity nullable or 0?

    Will a fruit appear multiple times for the same person?
    Last edited by Kyle123; 05-25-2018 at 08:55 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Need help with a S Q L statement inside VBA

    Try this SQL logic-

    sql.png


    This will return the person only-

    sql2.png


    This will return all records, with a person meeting the qualifications-

    sql3.png
    Attached Images Attached Images
    Last edited by 1aaaaaaaaaaaaaaa; 05-25-2018 at 11:33 AM.

  8. #8
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    808

    Re: Need help with a S Q L statement inside VBA

    Quote Originally Posted by Poizhan View Post
    Try this SQL logic-

    Attachment 575184


    This will return the person only-

    Attachment 575189


    This will return all records, with a person meeting the qualifications-

    Attachment 575191
    Thank you very much!

    It is kinda complicated for me to understand.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Can You Put An IF Statement Inside a REPLACE Statement?
    By nevi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2014, 07:34 AM
  2. Using a VLOOKUP and IF statement inside an IF statement
    By Isis3 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-08-2011, 07:04 PM
  3. Using Next inside an IF statement
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2010, 10:27 AM
  4. Can a Macro be run from inside an IF statement?
    By jacnsal in forum Excel General
    Replies: 3
    Last Post: 07-10-2008, 04:00 PM
  5. If, then inside a for, next statement?
    By EnergyEngineer in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-10-2007, 05:04 PM
  6. vlookup inside an if statement?
    By carlosgdlf in forum Excel Formulas & Functions
    Replies: 51
    Last Post: 09-06-2005, 07:05 PM
  7. vlookup inside an if statement?
    By carlosgdlf in forum Excel General
    Replies: 3
    Last Post: 08-03-2005, 09:36 PM

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