Results 1 to 2 of 2

Thread: Gather value from SQL based on value inside a cell

  1. #1

    Unhappy Gather value from SQL based on value inside a cell



    Register for a FREE account, and/
    or Log in to avoid these ads!

    I would llike to ask anyone on how to complete this lind of code. I'm having a hard time completing the code. Actually, I would like SQL to throw data to excel based on the value placed in one of the cell (a1 for example). Here's my line of code and hope that anyone will help me. Thanks in advance anyway.

    SQLStr1 = "SELECT ObjectiveDescription from Risk where RiskId =" ______________________<----what should I type next? I'm working on this for the past 2 weeks and still cannot find the right code for this one.

    Information:

    Table name in SQL is Risk
    Primary Key is RiskId
    Cell which has a value is "G1"
    Objective description is one of the Columns inside risk table.


    Thank you so much for the help.

  2. #2
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,274
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Is RiskID a text or numerical value in the database?

    Try this:

    Code:
    SQLStr1 = "SELECT ObjectiveDescription from Risk where RiskId = '"& Worksheets("Sheet1").Range("G1").Value & "'"


    (Assumes it is on Sheet1, of course.)

    If that doesn't work, try removing both ' characters from the line of code above (one before the "& and one between the two quotes at the end.)
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

Tags for this Thread

Posting Permissions

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