Solved

How do I set the table by redirecting to a different query

Posted on 2016-07-29
7
59 Views
Last Modified: 2016-08-10
How do I change queries for data source for a table - not pivot. I am trying to create a template invoice tool, and when the use selects the customer name I want to set the data source for the table by redirecting to a different query. What is the proper vba syntax for changing data source for existing table?

I am looking for a VBA code syntax to be able to re direct the table data source.
0
Comment
Question by:Karen Schaefer
  • 4
  • 2
7 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41735427
HI,

if your source is a source is an ODBC data source. You could try something like
Set qtQtrResults = _ 
 Workbooks(1).Worksheets(1).QueryTables(1) 
With qtQtrResults 
 .CommandType = xlCmdSQL 
 .CommandText = _ 
 "Select ProductID From Products Where ProductID < 10" 
 .Refresh 
End With

Open in new window

Regards
0
 

Author Comment

by:Karen Schaefer
ID: 41735878
some of my data sources are sql, some are excel (csv) downloads from external websites, are these both usable with your suggestions.  I plan on creating the queries with the various data sources, i was wondering how do I change the table data source to point to the necessary query within my Data Models, depending on a selection from a drop down list of company names?

ie. user selects the company = "ABC Company" and my current workbook has a dozen queries included, but only 1 with name of QryABCCompany.  I want to change the tblInvoice to use QryABCCompany from previous selection of "EFG Corp",(qryEFGCorp).  Is there a method in VBA to reset the datasource of tblInvoice?
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41736265
to change the data source you will have to change the connection

'ODBC connection
Worksheets(1).QueryTables(1) _ 
 .Connection:="ODBC;DSN=96SalesData;UID=Rep21;PWD=NUyHwYQI;"
'Text file
Worksheets(1).QueryTables(1) _ 
 Connection := "TEXT;C:\My Documents\19980331.txt"

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:Karen Schaefer
ID: 41736708
sorry, still not sure this will work with internal power query (dax).  The connection string is within the internal query, there aren't any ODBC connection to be used.
0
 
LVL 16

Accepted Solution

by:
Jerry Paladino earned 500 total points
ID: 41737235
Karen,

If you are using Power Query to retrieve your data files then you are working with M-Language and not DAX.   I found a good tutorial on using VBA to modify the Connection String and query logic here: Use VBA to automate Power Query in Excel 2016.  If you are using Excel 2016, the VB Editor will now record steps and has some Intellisense assistance.  It might be worth turning on the Excel Macro recorder and building a couple of Power Queries to see the syntax it builds.    Hope this helps...  

Thanks - Jerry
0
 

Author Comment

by:Karen Schaefer
ID: 41743108
please keep open, haven't had time to test this out, hope to get to it next week.
0
 

Author Closing Comment

by:Karen Schaefer
ID: 41750949
thanks for your input, however, no longer on project
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question