What could be SQL Query of a data source that is a spreadsheet, to be returned to a seperate spreadsheet? Including UDFs in the query? - sql

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

Related

Best Method to Query SQL using Variable Excel Values

There's an SQL database that I would like to query through excel without having to pull the entire SQL database into excel (5Million + rows). I have established the connection in excel. The values that I will be using to query the SQL Database are variable (typically around 150-200 cells).
End Result: The variable cells in excel are all in column A, I would like to query the Column A SQL values to retrieve the Column B SQL value and pull them back into excel. I know I could download the whole SQL database into excel and do a vlookup but my excel file will undoubtedly crash with all the SQL data.
Does anyone know where I should start? Would this best be resolved through VBA code or the advanced editor directly in excel?
Cheers,
Brandon M
You can include "?" in the query text of your connection. The first time you run the query, Excel will ask you what each of the "?" references. You can then change the values in those cells, and refresh the connection to use those new values.
Your situation is a bit unclear to me.
Do you want to perform "Select * from table where column in (Cell A)"? and then to print into Cell B?
If yes, you can use VBA code to build your SQL query and select the data.
If you don't want to use VBA, you can use some cell concatenation to build the query and can pass the query to SQL.

Excel - Off Page Reference to Microsoft Query

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.

How to query data in Excel from Visual Studio 2013?

Background:
I am transferring data from one Excel document doc0 to a templated Excel document doc1 to speed up processes at work. My only real restriction is that I cannot modify the document's formatting, so regular VBA is not an option. I can only pull data out of doc0 modify it and place it in doc1. I am using Visual Studio 2013 for doing so.
What I need to do is:
Organize doc0 numerically by Col 1 first, then Col 3 second. Then place the top 10 results in a specific cell range in doc1.
Get a count for jobs assigned to each worker and return that result to Visual Studio. Worker names are listed in Col 4.
I know how to query using SQL, but am open to using other functions/languages that can perform the same task.
Question:
How can I query the data to perform the actions above?
A simple example can be seen with the link below. The blue represents doc0, the red the results to be displayed in doc1 and the green is the results that I need to have returned to corresponding textboxes in Visual Studio.
There are a few options. ADO.NET is able to connect to your excel sheet using OleDB to read data with simple query capability. Examples can be found in KB316934.
Connect to an Excel file to read and write data:
Connection String
To access an Excel workbook by using the Jet OLE DB Provider, use a connection string that has the following syntax:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties="Excel 8.0;HDR=YES;"
Depending on your excel version, the connection string may slightly differ. Look them up here. E.g. 2013 would look like:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";
Read and write data
Use the sheet name followed by a dollar sign (for example, [Sheet1$] or [My Worksheet$]). A workbook table that is referenced in this manner includes the whole used range of the worksheet.
select * from [Sheet1$]
Use a range with a defined name (for example, [MyNamedRange]):
Select * from [MyNamedRange]
Use a range with a specific address (for example, [Sheet1$A1:B10]):
Select * from [Sheet1$A1:B10]
Writing is done in a similar matter if you're using OleDB
INSERT INTO [Sheet1$] (F1, F2) values ('111', 'ABC')
UPDATE [Sheet1$] SET F2 = 'XYZ' WHERE F1 = '111'
You may need to create a temporary copy from which you can query data, as you may be reading and writing to the documents using different techniques.
Full example at the link (unfortunately in VB.NET).
Alternative solutions
If you really want full fidelity access to the Excel file, without depending on Excel being present or running, you could also investigate:
Excel Package Plus
NPIO
Aspose Cells.NET (commercial)
These packages do not support querying, so you'll need to extract the data into objects and use Linq-to-Objects to query/sort the data before writing it back to the files.

Change select statement in SQL from Excel - end user

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)

Excel SQL data import parameter issue

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.