How can I change where Query results go in Excel VBA? - sql

I have created a Query in Excel using the wizard and can successfully manipulate the query itself from VBA, but now I want to change where the results are displayed among multiple sheets. Is there a "method" built in to manipulate this?

You can record a macro of what you just did to create the query and then change the parameters you want in VBA. It should just be a matter of first selecting the cell where you want the results to land (prior to executing the query) "Range("A1").Select".
Once you have this code if you are still having problems changing the "target cells", edit this question and add the code.

If you created a query in Excel using the wizard, then it most-likely created a new connection in the workbook. If so, go to the worksheet you want the data to appear on again, then go to the Data tab -> Get External Data group -> Existing Connections -> Double-click the connection -> choose your option for data delivery (Table, PivotTable, PivotTable and PivotChart).

Related

How to Edit the Macro that is created using "after Update" option in access

I created a macro using the 'after update' feature in access to capture some fields into table B (from table A) when the date in a particular column in table A is changed. This is working well.
However, I found out that I wanted to copy one more column to the table B from table A when the date is changed. In order to do this, I need to provide another 'Set Field' option to the original macro . But this macro is neither visible in the navigation pane nor in the VBE.
I am a new user to access and VBA.
Open up the table in design mode.
from the ribbon, you can use this dialog:
If there is a existing macro, say like before change, then you see in the above list that the given macro will be "highlighed".

VBA to send a SQL string to a Tableau Connection

I have an excel work book that allows users to update multiple SQL statements at once based on data entered into cells.
Then the users copy the updated SQL and go into Tableau and paste it into the corresponding custom SQL data source in tableau and refresh it.
Is there a way to send the updated SQL code directly to the corresponding connection in Tableau?
If the table name is changing, that presents a challenge.
I have two suggestions.
Modify the twb xml.
A Tableau workbook file is simply an XML file. One part of the xml contains the connection information, including your custom SQL. VBA has some libraries for manipulating XML. You can write some custom VBA code to modify the XML that contains the custom SQL.
Use VBA to create/alter a view in teradata
Teradata allows database views. With VBA, you can connect to teradata and create/alter a view that changes based on your parameters from Excel. The key is to keep the view name constant, then Tableau will not need to be changed each time the underlying view definition is changed.
Of the two, my first choice would be the second option. It's cleaner and doesn't require distributing a new workbook file each time.

Excel 2016- Load power query directly into power pivot

Is there a way to load power query data directly into power pivot without creating an excel table as an intermediary step?
All the examples I've found reference Excel 2010 and 2013. Although the instructions are similar, it does not work in 2016.
In following the steps found. when I go to "Existing Connections" in Power Pivot and try to open the Power Query connection. I get a message:
"The connection you’re trying to open was created with Power Query. To change this connection, use Power Query."
Is it possible to clean/transform data using power query and load it directly to power pivot in excel 2016?
I would go to the Data Ribbon and choose Show Queries. Depending on your Office/Excel update schedule, this may be changed to Queries & Connections. Either way, you are trying to open the Workbook Queries pane (appears on the right).
Within the Workbook Queries pane, right-click each Query and choose Load To. Ensure the first option is set to Only Create Connection, and that the Add this data to the Data Model option is checked.
With those options set, Load performance should be a lot faster, and you can exceed Excel's million row per table limit.
On the Excel Workbook Ribbon:
Option 1:
Go to: Power Pivot ↦ Add To Data Model
Option 2:
Go to Data ↦ Queries & Connections
right-click over the query you want to add to PowerPivot ↦ Edit
On the Power Query Editor: File ↦ Options & Settings ↦ Query Options ↦ Check Load to Data Model.
Finally: File ↦ Close & Load

How to connect an Excel file to an Access Database

I have created a query in an Access database and exported the query result into an Excel file. Now, I want to connect that Excel file to the Access database (or to that query) so that whenever some fields are updated in database, those changes would be automatically updated in the exported Excel file (report). What would be the best way to do it?
Thanks
You can create a link between Excel and an Access table or query by going to the Data tab on Excel and clicking the "From Access" icon.
Alternatively if there is a trigger inside access that you can use to update data (or if you don't need real time updates just hourly or daily ones), you could have access programmatically re-export.
If you want to work with data in Access, but still maintain the data in Excel, you need to link to the data rather than import it. Follow these steps:
Create a blank database or open an existing file in Access.
Select File, Get External Data, Link Tables.
Select Microsoft Excel as the file type.
Select a worksheet or named range to import, and then
click Next. You can import only one worksheet or named range at a
time, and each one will become an Access table.
In the next dialog box, select or deselect the check box First Row
Contains Column Headings, depending on whether your worksheet has
headings. Then click Next.
Enter a name for the table (or accept the default name that Access
suggests), click Finish, and click OK.
Now you have an Access table that looks almost exactly like the imported table. The advantage is that it maintains a live link to the Excel worksheet and can be edited in either application.

Cell format in SQL Data Connection within Excel Workbook

My job has tools that require us to mass upload data through Excel workbooks, so I'm trying to embed my SQL results into my files to save me a step. I've created the data connection, but haven't quite figured out the code format in the 'Definition' box yet.
Right now I have:
select custcontnum from "CONTACT"."dbo"."Incont" where custcode='SNH' and username = '[Custcontnums$G2]'
But it's pulling back blank. (If I define the username criteria itself between single quotes, it does work) Eventually I would like to create an "in" statement for the username criteria, so something like:
select custcontnum from "CONTACT"."dbo"."Incont" where custcode='SNH' and username in ('[Custcontnums$G2]','[Custcontnums$G3]','[Custcontnums$G4]')
What is the correct format for an excel reference in the data connection feature for Excel?