+ Reply to Thread
Results 1 to 29 of 29

Populating Cells With SQL Server Data

  1. #1
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Populating Cells With SQL Server Data

    I need to run multiple queries and insert their results into Excel cells. Each query will return only one result (a number, or a null). What is the best way to accomplish this?

    Below is an example of the code I'm currently using. It does return data to the cell specified, but I'm guessing there's an easier way to populate multiple cells with the results of multiple queries.

    Please Login or Register  to view this content.
    Last edited by Salty Sea Dog; 07-29-2009 at 04:49 PM. Reason: Added Code

  2. #2
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    Wow... this must be a lot more difficult than I thought...

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Populating Cells With SQL Server Data

    Without knowing in what way the queries are different, or how (or if) they relate to any Excel data, it's impossible to answer really. It may be as easy as returning one recordset from your SQL server and then dropping the relevant records into particular cells, or you may be better off sticking with what you have.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    The query will be exactly the same with one exception: The criteria in the last line will change.

    For example, the first query looks for "AND s.sys_loc_code = 'K2'"
    The second will be "AND s.sys_loc_code = 'MW25'"
    The third "AND s.sys_loc_code = 'MW6'"

    Etc.

    When I try to just copy & paste the query and put the results in different cells, I get no data back at all. No errors or anything, just no data.

    Does that make a little more sense?

  5. #5
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Assign Value to Variable

    How do I assign the result of a query (SQL Server) to a variable? Pesudo:

    Please Login or Register  to view this content.
    Once the variable value is set, I would like to assign that value to a cell in Excel, in this case let's just say A1.
    Last edited by Salty Sea Dog; 07-30-2009 at 01:13 PM. Reason: A little more info

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Populating Cells With SQL Server Data

    Yep. I'd probably use one query with an IN clause containing all the codes rather than lots of separate ones.

  7. #7
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    Quote Originally Posted by romperstomper View Post
    Yep. I'd probably use one query with an IN clause containing all the codes rather than lots of separate ones.
    That's what I was doing initially. But how can I use the IN clause and populate multiple cells? I'm guessing I can assign a range:

    Please Login or Register  to view this content.
    But then the issue of spaces comes up. The cells I'm going to populate have various blank rows inserted for formatting purposes. How can I get around that?

    I was thinking something along the lines of assigning the query results to an array, and then populating individual cells with the values from that array. Would that work? If so, how would I do it? (I don't do a whole lot of VBA)

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Assign Value to Variable

    Although only hypothetical code please add CODE tags around your pseudo-code in your first post.

    Set is only used with Objects (Workbook, Worksheet, Range etc...), Let for non-objects (strings, numeric types etc) which is implicit in VBA and thus does not need to be specified:

    Please Login or Register  to view this content.
    Of course you could just assign the result to the cell directly

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Assign Value to Variable

    Quote Originally Posted by DonkeyOte View Post
    Although only hypothetical code please add CODE tags around your pseudo-code in your first post.

    Set is only used with Objects (Workbook, Worksheet, Range etc...), Let for non-objects (strings, numeric types etc) which is implicit in VBA and thus does not need to be specified:

    Please Login or Register  to view this content.
    Of course you could just assign the result to the cell directly

    Please Login or Register  to view this content.
    Ok, added CODE tags.

    What is the actual code to assign the query result? I have posted my entire code as it currently stands below.

    Please Login or Register  to view this content.

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Assign Value to Variable

    I believe you've asked that question (albeit in a slightly different flavour) elsewhere...

    http://www.excelforum.com/excel-prog...rver-data.html

    FYI: the member assisting you on your other thread is one of the "real" gurus so you will be hard pushed to find superior advice / assistance from anyone else... I would suggest you persevere on the above thread.

  11. #11
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Assign Value to Variable

    Ok, however I'm trying to learn different ways of accomplishing the same task(s) as one may be more efficient than the other.

    I've stated that I'm not all that familiar with VBA. I'm just trying to explore my options. I also figured that asking in a different manner may bring more opinions and advice to the table. After all, if I can simply figure out how to assign the query result to a variable, I may very well be able to figure the rest out on my own.

    If having two posts that are simliar is a problem, feel free to delete this one.
    Last edited by Salty Sea Dog; 07-30-2009 at 01:26 PM.

  12. #12
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Assign Value to Variable

    I will merge the two... what you're doing is not trivial per se so kudos for trying it, once you get the hang of the approach rest assured you'll be flying.

  13. #13
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Assign Value to Variable

    Quote Originally Posted by DonkeyOte View Post
    I will merge the two... what you're doing is not trivial per se so kudos for trying it, once you get the hang of the approach rest assured you'll be flying.
    Thanks for the merge. And I appreciate the help

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Assign Value to Variable

    You've got two basic choices as I see it:
    1. Return the querytable to one contiguous range, then use formulas in your cells to lookup the relevant item for each; or
    2. Use VBA and ADO to return a recordset containing all your data, then loop through populating the cells from individual records.

    Option 1 is easier, option 2 is neater. I'm on a phone right now, so don't have code to hand for 2, and I won't do until next week probably, but let us know which you prefer.

  15. #15
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    I prefer code whenever possible. Option 2 sounds like the way to go.

  16. #16
    Registered User
    Join Date
    03-05-2009
    Location
    Vail, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    83

    Re: Populating Cells With SQL Server Data

    I would agree with everyone above, there are several ways to do this.
    Now, in my case, I was just using an Access front-end linked to SQL Server tables. This is one lengthy method that I use for updating about 14 Query Defs with SQL code, then calling on a Excel Template with 14 named ranges to just transfer the data. When the customer wants the report reformatted, it is often just a matter of changing the Excel Template instead a lot of code.
    Another way, I like to use SQL Pass-Through queries. All the processing is done by SQL Server - only the results come across the network.

    If this is not useful and too large, please feel free to delet it:

    Please Login or Register  to view this content.
    Last edited by RxMiller; 07-30-2009 at 03:37 PM.

  17. #17
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Populating Cells With SQL Server Data

    Here's some sample code for retrieving a recordset from a SQL server db and dumping it into a worksheet. We would need a bit more detail about what goes where to customise it for your situation:
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    romperstomper,

    That may be the way to go. I haven't tried getting the data back using an ADODB recordset.

    I will give this a shot.

    Thanks for your help!

  19. #19
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    I'm getting an error saying "Login failed for user". Is there a way I can set up the connection to use the current Windows user credendials?

  20. #20
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    Nevermind, got it.

  21. #21
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Populating Cells With SQL Server Data

    Yep - replace the User ID bit with:
    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    Ok, so this works. But I'm running into the same problem I ran into initially. I have roughly 40 or so queries I need to run. If I do the above 40 times and run each sub one-at-a-time I get my data back fine. However, if I attempt to run all of them, I only get data back for the last query (the last Sub in the editor). Is there a way around this?

  23. #23
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    Ah, if I don't use a new Sub each time it looks like the results may be different. Trying now.

  24. #24
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Populating Cells With SQL Server Data

    Yes, that's the plan - run it once to get back ALL of the data, then loop through the recordset putting each bit in the right cell.

  25. #25
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Populating Cells With SQL Server Data

    This looks like it's going to do the trick. Thank you! I appreciate the help.

  26. #26
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Populating Cells With SQL Server Data

    Glad to help!

  27. #27
    Registered User
    Join Date
    06-27-2012
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Populating Cells With SQL Server Data

    I know this is an old thread, but I just wanted to say thanks to Romper Stomper for a simple and elegant solution to the problem. I've been working on the same problem for about 10 hours and reading the code was like turning on a light - kudos!!

  28. #28
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Populating Cells With SQL Server Data

    You're welcome.

  29. #29
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Populating Cells With SQL Server Data

    Quote Originally Posted by gwunta View Post
    I know this is an old thread, but I just wanted to say thanks to Romper Stomper for a simple and elegant solution to the problem. I've been working on the same problem for about 10 hours and reading the code was like turning on a light - kudos!!
    Gwunta, I have to agree. I don't know how many times Romperstomper's code has helped me out, and helped me learn, which is why I always make a habit to subscribe to his threads ( Did not mean to talk about you Romper as if you were not here).
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

+ 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