I tried to find the answer myself but not knowing how to word the question caused problems :).
I have an excel workbook that I use to pull data from SQL Server 2005 using a stored procedure that accepts a parameter. I am using Microsoft Query in Excel. I am trying to get Excel to grab the parameter from a cell so that the users will not have to edit the connection. If I were to do this as SQL, I would replace the value with a ? and point it to a cell without issue. Since this is a SP, I get a strange response.
This works:
exec [GTI_mainframe].[proc_mf_forecast_authorizations] .07
This:
exec [GTI_mainframe].[proc_mf_forecast_authorizations] ?
Gives me the following message box:
[Microsoft][ODBC SQL Server Driver]Invalid parameter number
I control the SP and the excel workbook so can impliment what I need to. I had one person suggest a vba approach to reference the cell value directly. I could do that but my choice would be to not have to do it in macros. I would love to just be able to use the "refresh all" from the data tab in the ribbon bar. Any thoughts?
I recommend you XLReport; it allows you to connect to almost any database, create queries with parameters.
You can download the trial here.
Related
I am utilizing Microsoft Query in Excel to tap into an ERP table structure like Crystal would do.
In writing the SQL, is there a way to have a filter pulled from the active Excel worksheet that is embedded in the SQL instead of prompting and editing the query?
My main problem is a Like [Prompt]% in the Excel GUI for the users to change like order numbers.
Is it possible to do an off page reference from MS Query to Excel?
If by "Microsoft Query", you're talking about the window that looks like it was coded for Windows 95, stop using it. This is provided for retro-compatibility.
Anyway, if you've displayed the criteria bar in MS query, you can type a name between brackets e.g. [Something] and MS query will prompt you to fill a value.
Not what you want yet but getting close. When you return to Excel and refresh the query, the prompt will now offer you the possibility to use a cell instead of a value you need to type every type.
In the more modern connection utility accessible via menu data > Connections (+ available even if you created your table via MS Query btw), you can achieve that by using question marks in the WHERE clause.
For instance, instead of SomeField = 'SomeValue', write SomeField = ?
Then, click on the Parameters button and you'll see all the parameters you've set, each of them can be attached to a cell's value.
I have easily created a custom SQL Query to populate an Excel from an SQL database.
This was quite straightforward and easy (Data/from External sources/Microsoft SQL - and then edited the command text section of the connection properties)
But now, I would like to use a WHERE clause with a parameter from a cell.
From what I read on every tutorials and similar SO questions, I should simply have to use a question mark and Excel will understand it and allow me to define a cell.
Sources:
How to add parameters to an external data query in Excel which can't be displayed graphically?
That's what I did:
Unfortunately I got this dialog telling me that some info are missing:
You should use Microsoft Query first , and then change the Command Text later.
Refer from this: Excel: Send multiple values in "Command text"
I have a decent size SQL statement that I have connected to an Excel worksheet and it runs fine. The question I have is would it be possible to have the end user enter a list of values in an excel sheet or somewhere else and have those values added to a WHERE clause in my SQL to limit the results per the users needs without the user having to go into connection and alter the SQL etc.? Thank you.
Yes. I do it as part of a VB script. I'm not a VB expert and I didn't set it up- I just know enough to tweak some changes that the users require.
Conceptually- provide an area in th Excel template for users to enter the parameters, use VB to get the values of those parameters, then pass them to the SQL statement.
You can create a Data Connection in Excel that utilizes a parameter. Whether you are calling a Stored Procedure on the DB or sending raw SQL, you can replace part of the statement with a ?. Tie that parameter to a specific cell. Now all your user needs to do is enter/change the value in that cell and it will re-query the database using that value as the parameter.
(I don't have Excel in front of me, otherwise I'd walk you through the exact steps)
I currently have a data source of a large table, sitting in workbook1. From workbook2, which is currently empty, I wish to set up a DSN connection to workbook1, so that I can query it from workbook 2.
In the SQL query result, I wish to display extra columns which are calculated using User-Defined VBA functions, the arguments of which will be other fields from the data source.
Example:
Workbook1 is Field1, F2, F3 and F4. I wish to query this and display all records, but additionally I wish to have F5=UDF(F3,F4).
I have been advised already that the solution to this is:
SELECT UDF(F3,F4) as F5
FROM \SourceWorkBookLocation\SourceWorkBook
IN ACCESS:
The problem I am having in access is not at the top of my list right now, relates to data types and trying to determine if a number in a string is <25. But the main problem is in MS Query:
IN EXCEL/MS QUERY:
The function is just not recognized; "undefined function"
I am not sure how to get it to see the function? My end goal here is to build a front end in excel, and have vba querying appropriately using user input variables passed to the queries. The querying will be done on a separately updated workbook.
Any ideas on how to get MS Query to see my UDF and accept what I am doing? Could it be a driver issue? There are a range of excel drivers to choose from.
Thanks
Looking at the info you have provided, you have tried to use two Excel workbooks as tables to query using Excel VBA UDF. Now I assume you are going to use these workbooks as your tables but in MS Access.
All most all databases is able to read standard SQL. See the thing is that each database is able to handle functions writting in their own space. In your case please write your UDF in Access VBA. Then try to execute to the same.
This is a common issue sometimes people do face, either tyring to access MS Access UDFs from Excel or vise versa. In a nutshell, when you're running MS Access, queries can call back into VBA. But when you're going through ODBC or ADO, the JET engine doesn't have the whole VBA model to draw on because it's simply not running.
You could try to do something like this:
Dim objExcl As Object
Set objExcl = CreateObject("Excel.Application")
objExcl.OpenCurrentDatabase "ExcelFileName/Path"
objExcl.Run ("UDFName")
objExcl.CloseCurrentDatabase
Set objExcl = Nothing
Frankly I prefer moving the UDF in to Acces..
References:
Create Access UDF
http://www.sqlexamples.info/SQL/inlineudf.htm
Full error message: "[Microsoft][ODBC Excel Driver] The LEVEL clause includes a reserved word or argument that is mispelled or missing, or the punctuation is incorrect"
I get this when trying to execute a query in one excel document against a table in another excel document. My SQL doesn't include LEVEL at all, and I can't find anything useful on Google.
Before anyone asks: Yes, I know excel isn't a database. I can't find a better way to get this data from sheet to sheet.
Because I like my job, I've obfuscated my SQL; otherwise it includes company confidential data. I am certain that my syntax is right on the things that I am removing, because it was written my Microsoft Query.
SELECT `Table1$`.`Field1`, <Other Fields from Table1>,
`Table2$`.`FieldA`, <Other Fields from Table2>,
`Table3$`.`FieldX`,
`Table3$`.`FieldY`
FROM `<network path starting with \\>\<filename>.xlsb`.`Table1$` `Table1$`,
`<network path starting with \\>\<filename>.xlsb`.`Table2$` `Table2$`,
`<network path starting with \\>\<filename>.xlsb`.`Table3$` `Table3$`
WHERE `Table2$`.`Join1Field` = `Table1$`.`Join1Field`
AND `Table3$`.`Join2Field` = `Table1$`.`Join2Field`
AND `Table1$`.`Weeks Ago` < 14;
Note: All of these tables are in the same Excel file, and I'm using Excel 2007.
According to this page on MSDN you entered an SQL statement that has an invalid reserved word or incorrect punctuation. As #Ken White points out, it's hard to tell from your edited SQL what is going on.
IIRC when you use the GUI tool in MS Query it writes ODBC syntax: perhaps there is a problem in translation to native Access (ACE, Jet, whatever) syntax at the Excel end. If you can write SQL without GUI tools, or can use the Access QBE thing to do it for you, IIRC you can paste native Access SQL syntax into MS Query's SQL window and it will 'pass through' to Excel unchanged.