Passing Parameter Values from Excel to SQL Server

Sdílet
Vložit
  • čas přidán 19. 08. 2024
  • In this video we will perform what should be a relatively simple task but isn't intuitive at all. I'll show you how to pass the values from Excel cells to a dynamic SQL Server query that will execute and return the values to Excel. This will make a nice little Excel data portfolio project for those of you looking to learn. Check it out! I will say there are many ways to do this, but this method is fine for personal use only, not for a distributable deliverable due to the lack of stored proc usage.
    ★☆★ Join this channel to get access to perks: ★☆★
    / @anthonysmoak
    Watch the entire playlist to learn additional techniques using stored procedures:
    • SQL Meets Excel: Advan...
    #SQL #analytics #tsql #excel #dataanalyst #data
    Download the Database used in the video: learn.microsof...
    ★☆★ FOLLOW ME BELOW: ★☆★
    Blog ► anthonysmoak.com/
    Instagram ► / @anthonysmoakdata
    Facebook ► / anthonybsmoak
    Tableau Public ► bit.ly/3JMKsLY
    1:48 Demonstrate worksheet
    3:45 SQL Query Overview
    4:45 Start worksheet Build
    5:47 Record Macro
    6:17 Connect to Data
    8:17 Explain & Build VBA
    16:06 Insert Button for Refresh
    17:16 Impromptu Debugging
    17:54 It Works!!
    18:30 Adjust Column Width Property
    19:24 Confirm Results
    20:00 Date Picker Ad-In
    20:48 Password Protect VBA
    21:28 Wrap it Up B!

Komentáře • 79

  • @rpopecpa
    @rpopecpa Před rokem +3

    Awesome, awesome, awesome!! Wow, this is the best Excel/Power Query/VBA/SQL video I've ever seen. Many thanks.

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      Thanks Ryan, you just made my day! Make sure to check out the additional videos in this series if you need a bit more flexibility. czcams.com/play/PLRNrM8SIqgQZ4u5yGSlVNeGgej1lcwLwr.html

    • @rpopecpa
      @rpopecpa Před rokem +1

      @@AnthonySmoak I love the select all feature! I often times use multiple criteria where at least one of my criteria is select all. Nice!!

  • @alpeshshah891
    @alpeshshah891 Před 9 měsíci +1

    Mindblowing, thank you for showing us how to combine vba and power query to get end result, opens up a greater scope now for me to produce fantastic reports.

    • @AnthonySmoak
      @AnthonySmoak  Před 9 měsíci

      I really appreciate this comment. Thank you!

  • @jalbert9675
    @jalbert9675 Před 9 měsíci +1

    I needed to know how to do this and got lucky enough to learn it from Smoak himself. To quote my favorite Russian comedian, "What a country!"

    • @AnthonySmoak
      @AnthonySmoak  Před 9 měsíci

      I know a J Albert who happens to be a "helluva engineer". Is the one in the same?

    • @jalbert9675
      @jalbert9675 Před 9 měsíci +1

      @@AnthonySmoak Does a Whittingham timewarp in the woods?

    • @AnthonySmoak
      @AnthonySmoak  Před 9 měsíci

      @@jalbert9675 Indeed. I'll actually be catching up with him and Terence tomorrow.

    • @jalbert9675
      @jalbert9675 Před 9 měsíci

      @@AnthonySmoak Tell those two old married farts I said hello.

  • @user-ih7ot7vl8u
    @user-ih7ot7vl8u Před 5 měsíci +1

    Thank you for this magic tutorial. Please make more videos. ❤❤

  • @user-fc8od3fx4h
    @user-fc8od3fx4h Před 6 měsíci +1

    Man you saved the day with this video! Thanks!!!

  • @matusjaniga1165
    @matusjaniga1165 Před 6 měsíci +1

    Great job Anthony, It is a briliant solution :) bravo

  • @CecLevel
    @CecLevel Před 3 měsíci +1

    Thanks! Very easy...appreciate you man.

    • @AnthonySmoak
      @AnthonySmoak  Před 3 měsíci +1

      I really appreciate the SUPER thanks!!!!!! Thank you 🤜

  • @msamysobih680
    @msamysobih680 Před rokem +1

    Thanks, I have been searching for this for a long time 🤝

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      Glad you found my video useful. Make sure to watch the other videos in the playlist for the more secure stored procedure method!

  • @chi-bucks
    @chi-bucks Před rokem +1

    Always learning something new from you. I appreciate.

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      Thank you for watching and commenting Ubani.

  • @costoncheatham7025
    @costoncheatham7025 Před 3 měsíci +1

    Very helpful - thanks!!

  • @juanmigueltobos9301
    @juanmigueltobos9301 Před rokem +1

    That was what I was looking for , thx !!!

    • @AnthonySmoak
      @AnthonySmoak  Před rokem

      Glad to hear it. Make sure to watch the 2nd video if you want to learn how to use a more secure method involving stored procedures. czcams.com/video/7GOznSVBDmc/video.html

  • @kaiser6242
    @kaiser6242 Před rokem +1

    This is fantastic!

  • @kareldenecker1989
    @kareldenecker1989 Před 4 měsíci +1

    magic! thanks!

    • @AnthonySmoak
      @AnthonySmoak  Před 4 měsíci

      Watch the rest of the series to build upon what you learned in this vid. Thanks for the comment!

  • @YorkUniversityIT
    @YorkUniversityIT Před 10 měsíci

    While does work and is GREAT for certain kinds of inputs, for the specific scenario of a monthly report you would be better off inferring the date range you need based on the current date, which can be done entirely in the SQL with no need to send an parameter.

    • @AnthonySmoak
      @AnthonySmoak  Před 10 měsíci

      Thanks for the comment. Yes, SQL is certainly capable of handling flexible dates. Think of this example as a teaching exercise to showcase the parameter capabilities between Excel and SQL if needed.

  • @MJacobsen
    @MJacobsen Před 7 měsíci

    Great video, thanks! Any ideas for taking it to the next step?
    I could really use tips on how to disable the "you are now executing code" popup, and secondly to embed username & password in the document. This way the document can be setup and then passed on to a colleague who can get new data anytime. (the SQL user embedded would of course only have readonly rights).
    Hope to hear from you, greetings from Denmark :)

  • @abielwubet2587
    @abielwubet2587 Před rokem +1

    Greatest

  • @emmanuel.aggrey
    @emmanuel.aggrey Před rokem

    please can i have this excel file to modifiy according to my need without starting from strach thank you.

  • @avaltewarehous
    @avaltewarehous Před 9 měsíci

    Thank you so much, Anthony, this technique has enabled me to make my query dynamic. However, now I am left with a new head-scratcher I wonder if you could help with.
    I used PowerQuery to pivot the SQL data set, and return the pivoted data into my workbook. But when I refresh the query using updated parameters, it wipes out the pivot, and just returns back to the unpivoted data table from the database. I am guessing this is because there's no code in the VBA macro to put the data into the pivoted form, but when I try to record a macro of me using PowerQuery to pivot the data set, Excel does not record anything. So I am kind of stuck here. Have you ever encountered this issue, or have any suggestions? Thanks!

  • @Daulet2009
    @Daulet2009 Před rokem +1

    Hi, i have a question. I have a dashboard in excel that has 20mb file size. If i switch to SQL and do not store the data in several sheets does it reduce file size significantly? I mean is it possible to create excel file that don't store all necessary data inside the file and fetches the data automatically from SQL everytime when it's opened.

    • @AnthonySmoak
      @AnthonySmoak  Před rokem

      I don't know your particular case but If you have the opportunity to use a real database like SQL Server then do so. However there is a limit to how much data can be stored in Excel. Also use Power BI or Tableau for dashboarding since they are optimized to handle large datasets. In Power BI you can connect directly to the data source without importing data by using DirectQuery. support.microsoft.com/en-us/office/create-a-memory-efficient-data-model-using-excel-and-the-power-pivot-add-in-951c73a9-21c4-46ab-9f5e-14a2833b6a70

  • @user-dy6ei2hr6p
    @user-dy6ei2hr6p Před 11 měsíci

    Thanks for the great explanation, but where are the codes? suptest

    • @AnthonySmoak
      @AnthonySmoak  Před 11 měsíci

      Thank you! Sorry I haven't posted my specific code any place but the important part is that the technique is flexible enough that you can make up your own example.

  • @abdullahquhtani4247
    @abdullahquhtani4247 Před rokem +1

    Good BUT😊!!
    I think it’ll be great if you used stored procedure instead 😏

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      Yes a stored procedure will have better protection from sql injection. It's just harder to execute in this manner. This is a quick and easy way to get it done. I'd use this technique only for personal use.

    • @abdullahquhtani4247
      @abdullahquhtani4247 Před rokem +1

      @@AnthonySmoak thank you 🙏🏻

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      @@abdullahquhtani4247 Just for you I am going to make another video showing how to make the call with a stored procedure. Same process just a different statement passed to the Query formula in VBA.

    • @abdullahquhtani4247
      @abdullahquhtani4247 Před rokem +1

      @@AnthonySmoak Thank you so much. Highly appreciated 🌹🌹👍🏼

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      As promised, make sure to like and comment! czcams.com/video/7GOznSVBDmc/video.html

  • @kewltopix
    @kewltopix Před rokem +1

    Curious why use vba when you can just pass the parameters to power query via a function to read the cell where the parameter values are located. Unless it’s SP which power query wants a select. Haven’t tested this method yet myself. Is there a performance gain?

    • @AnthonySmoak
      @AnthonySmoak  Před rokem

      I am not sure of the exact method you are referring to in order to get values back from SQL Server based upon Excel cell values (send me a link if you have one, thanks). But I do know there are multiple ways to accomplish what I've shown. It comes down to preference. I am just simply more comfortable with SQL and VBA code as opposed to DAX/M/ Power Query GUI methods. Willing to keep an open mind if I see an exact process.

    • @kewltopix
      @kewltopix Před rokem +1

      @@AnthonySmoak the way I do it is to name a cell with a named range like cellWithDate, then create a function in power query
      let
      Source = (rangeName) => Excel.CurrentWorkbook(){[Name=rangeName]}[Content]{0}[Column1]
      in
      Source
      then name the function somn like fnGetDate > call the function where the value is needed like fnGetDate("cellWithDate")
      Change the cell value, hit refresh, no vba needed

  • @VatsalSingh007
    @VatsalSingh007 Před rokem +1

    I did all the steps same but getting Run-time error '1004': Application-defined or object-defined error. Any idea what could be happening?

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      Make sure you are referring to the correct cells in the VBA code. Make sure your code is in a module as well.

    • @VatsalSingh007
      @VatsalSingh007 Před rokem +1

      @@AnthonySmoak Not sure if it is right things to do or otherwise, "ActiveWorkbook.RefreshAll" worked for me. Thanks that you took time to respond.

    • @paolo.macatangay
      @paolo.macatangay Před rokem +1

      @@AnthonySmoak I also got the same Run-time error "1004': Application-defined or object-defined error". I am able to verify via message box that it gets the cell value and also it is in module as well. Not sure why

    • @MostafaElgohary-vw6tp
      @MostafaElgohary-vw6tp Před rokem

      @@paolo.macatangay same here

    • @MostafaElgohary-vw6tp
      @MostafaElgohary-vw6tp Před rokem

      @@AnthonySmoakSorry, I still get the Run-time error "1004': Application-defined or object-defined error"

  • @workplacevids9977
    @workplacevids9977 Před 8 měsíci

    Please share with us this file. Thanks in advance.

  • @nuclear1747
    @nuclear1747 Před rokem

    Do you know how to bypass problem with data format, excel cell with date is formating to UK standard and SQL Date variable needs US one?

    • @AnthonySmoak
      @AnthonySmoak  Před rokem

      Try to change the date format settings in Windows. Or perform manipulation in VBA to format the date to your liking. Watch the 3rd video in this series where change the default formatting in a SQL stored procedure.

  • @sreedharraor
    @sreedharraor Před rokem

    Hi Anthony, well explained. can you help me with my scenario? Example - Take oracle sample databease tables. If I have an excel sheet with only employee IDs, how can I pull their department and location from database into excel next to employee ids

    • @AnthonySmoak
      @AnthonySmoak  Před rokem

      I'm not sure of a practical way here. You can import the sheet into Oracle and run the query at the source. Or try to format the data so it is usable for the SQL IN clause.

    • @sreedharraor
      @sreedharraor Před rokem

      Thank you. That was just an example. In real I have a weekly task to get details for certain range of values in excel from database. I was thinking if I can use power query and just refresh weekly instead of repeating

    • @AnthonySmoak
      @AnthonySmoak  Před rokem +1

      @@sreedharraor See if this technique would work if you want to use Power Query and start with a list of items: czcams.com/video/OTBBX485Fg8/video.html

    • @sreedharraor
      @sreedharraor Před rokem +1

      @@AnthonySmoak thank you. What a coincidence!!!! When I searched, I found this video and it really helped me. I’m able to bring the data using where clause in power query

  • @Immortal34345
    @Immortal34345 Před rokem

    hi anthony, is there a way i can choose multiple Cities?

    • @AnthonySmoak
      @AnthonySmoak  Před 11 měsíci

      Certainly, modify the CASE statement I use in this video to handle two cities in your dataset: czcams.com/video/GOvNQtfLTHo/video.html

  • @alexanderschu6933
    @alexanderschu6933 Před 4 měsíci

    This is the bad solution, it spams security warnings and is slow. Just use Microsoft Query as DataSource and bind your params directly to a cells. No need to code any line of VBA. Its tricky to get it running first as the tutorials of MS do not work like they say ;)

    • @AnthonySmoak
      @AnthonySmoak  Před 4 měsíci +1

      Make sure to watch the other two videos where we execute the SQL with a stored proc which is a bit more secure from SQL injection. Regardless, this is an ad hoc solution, not meant for serious production uses. Take it or leave it 😂

    • @alexanderschu6933
      @alexanderschu6933 Před 4 měsíci

      @@AnthonySmoak No offense meant but writing VBA with SQLs is more the Office 97 era than the current ones ;)

    • @AnthonySmoak
      @AnthonySmoak  Před 4 měsíci

      @@alexanderschu6933 Send me a link to your new high tech solution, maybe I'll make a new video for this series ;)

  • @XiZhang-j3i
    @XiZhang-j3i Před 5 dny

    have run time error 438 and the bug sits with Active.Workbook.Queries("LimitBalance").Refresh, could you please help?