+ Reply to Thread
Results 1 to 12 of 12

SQL Transform Statement in VBA - Using Variable in the FROM clause

  1. #1
    Registered User
    Join Date
    10-20-2011
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2007
    Posts
    6

    SQL Transform Statement in VBA - Using Variable in the FROM clause

    Hi Everyone! I've run out of options in debugging and am hoping one of you lovely coders can please help me. I am taking a flat file out of a SQL query and crosstabbing it in Excel using VBA. I'm essentially doing the same thing a pivot table does, but without having to create a pivot table it reduces my overhead and makes things easier for me. However, I am running it across several workbooks and need the data range in the FROM clause to be a variable since my data ranges will change depending on what workbook I am using. Thank you SO MUCH in advance for any guidance.


    Please Login or Register  to view this content.
    '**I've tried formatting the range as a table and setting the range as a range and inserting it into the FROM clause and still can't get it to work**

    Please Login or Register  to view this content.
    '**However, if I had this in the FROM clause - "FROM [Action Sports$A1:O1112] " & _ it works perfectly fine, but then it isn't dynamic**

    '**It errors and says the Microsoft Jet Database doesn't recognize TempTable once this piece of code runs**
    Please Login or Register  to view this content.
    Last edited by Cutter; 08-05-2012 at 05:11 PM. Reason: Added code tags

  2. #2
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    @ jdorbish


    Please notice that code tags have been properly added to your post. To see instructions for applying them, click on the Forum Rules button at top of the page and read Rule #3.
    Thanks.

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    Try this, Get the string address for the sql
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-20-2011
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    My apologies for the code tags. I even read the rules and put the code (using the # key) and it for some reason still didn't work.

    Mike, I like your idea, but two things: 1) Action Sports is just one of the names of one of the tabs, there will be other tabs with different names 2) I made the Action Sports piece a variable and the Range a variable and concatenated both and it still throws an error. I'm attaching a screenshot. Thanks again for any help!

    error.jpg

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    Can you upload you workbook, 6pm is not a worksheet in your workbook. Looks like 6PM_BU should be the name.

  6. #6
    Registered User
    Join Date
    10-20-2011
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    Sorry, I should have just sent an example spreadsheet originally. FYI, in VBA References, Active X 2.6 Object Library needs enabled. Thanks!
    Attached Files Attached Files

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    Ok one question, what is the value of Metric? In the code its activecell?

  8. #8
    Registered User
    Join Date
    10-20-2011
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    I had to update one line of code for the runcount since I deleted a column before I sent it to you. Please use the attached sheet. Metric is the value in the cells from I1 to P1. It loops through the top cells setting Metric = to ORDERS, UNITS, COST, etc in each successive loop.
    Attached Files Attached Files

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    This should work for you.

    4 problems.
    1.) excel 2007 or greater use ACE 12.0 and not Jet.
    2.)When quering and excel sheet, Sheet name must have the $ symbol after, ex: Sheet1$
    3.) Remove the $ from the Range
    4.)And TRANSFORM Sum(" & Metric & ")" need to be TRANSFORM Sum(" & Metric & ") AS MySum. Where MySum could be anything you want it to be.

    Hope this help.



    Please Login or Register  to view this content.


    ---------- Post added at 09:02 PM ---------- Previous post was at 08:27 PM ----------

    Also noticed few things, You dont need to make the connection to the workbook inside the loop. Just make it before entering the loop. I cleaned up a few other things so the screens not jumping around.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    10-20-2011
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    Mike! You're a wonderful, wonderful man. I'd certainly never have known anything about the ACE instead of Jet. It works like a charm. Thanks so much for all your help!

  11. #11
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551
    No problem, remember to mark the post as solved.

    Thanks Mike
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  12. #12
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: SQL Transform Statement in VBA - Using Variable in the FROM clause

    @ jdorbish

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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