Import a single value from Access into Excel (without the column heading) - excel

I would love to know how to import a single value from my Access database into an Excel cell.
Alternatively removing the header would be a good start.
The query
SELECT SUM(DEMANDS) AS TOTAL FROM [DB PROD]
gives me a header named TOTAL and, right below it, the sum I'm interested in. I use this query from within Excel (Data > Get External Data > From Access). How can I only get the sum in a single Excel cell?

One way to accomplish your goal would be to put your existing query on a different Sheet, say Sheet2, so the column header appears in Sheet2!A1 and the value appears in Sheet2!A2. Then reference the value cell from elsewhere in the Excel document. (The formula will look something like =Table_test.accdb[TOTAL].)
If you had several queries you could presumably keep them together (side by side) on the same sheet. You could also hide the query sheet so the "magic" would be more transparent to the user.

I am not sure I understand what you are doing, but, in order to read Access data from Excel, you need to do the following:
programmaticaly open a connexion in your Excel code, pointing to your
access database (let's say an ADO connexion for example)
then execute your 'SELECT' through this connection, using the ADODB.command object. That will return the requested value
another option would be to open a local recordset in Excel,based on your SQL intruction, and read the value.
Then do not forget to close your connexion

Related

Update data by an external reference (link) to a table in another workbook

I have defined a table Table1 in source file.xlsx. When source file.xlsx is open, in target file.xlsx, I could use ='source file.xlsx'!Table1[#Data] in a cell to get the table data.
However, I realise that if I close source file.xlsx, that formula becomes ='\\Mac\Home\Downloads\source file.xlsx'!Table1[#Data] and its value becomes #REF!.
This is not the case for an external reference (link) to a cell range in another workbook; an external reference to a cell/range could still get value when the source file is closed.
Could anyone tell me how to update the table data even though when the source file is closed?
This is expected behaviour
From the Microsoft Help
Links to data tables residing in external files display #REF!
Symptoms
Consider the following scenario:
You insert a table in an Excel file, named for example source.xlsx.
In another file, named for example destination.xlsx, you insert a link to the data table which resides in source.xlsx.
You close both source.xlsx and destination.xlsx.
You open destination.xlsx and you choose to update links when prompted.
Result: the cells that are linked to the external data table display #REF!.
Cause
This is expected behavior for Excel.
New references to external workbooks that aren’t open will successfully parse without verifying the reference, but will return #REF.
When loading an external structure reference to a closed workbook, the reference is not updated by default. If you choose to calculate the formula the result is #REF! instead of the corresponding value.
Microsoft Help
As an alternative, create a query to the table. This can be refreshed from a closed workbook
To answer your question:
What you are seeing is how excel works at a cell formula level. If you type in
=[file.xlsx]Sheet1!Table1[#All]
This might be a little more of what you want, but it will give you the whole table.
For one column you can do:
=file.xlsx!Table1[[#Headers],[Date]]
=file.xlsx!Table1[Date]
You can create a defined name for Table1 as mytable then you would enter the formula =file.xlsx!mytable
All the above becomes similar to ='\\Mac\Home\Downloads\source file.xlsx'!Table1[#Data]
For an alternate method (recommended):
The very best way I have found to do this is using power query. This is not formulas in cells but it is like a database query tool. There are several people on You-tube making videos about using power query for this exact thing. I will step you through the process starting in the file where you want the data to appear. Go to:
Data > (Get & trasform Data) Get Data> From File> From Workbook...
In the popup window navigate to your file "\Mac\Home\Downloads\source file.xlsx" and click Import.
In the next window choose "Table1" (or an item that you want).
Now choose load and this data loads into your sheet.
If you want to transform the data of "Table1" then...
Data > Queries & Connections> (this pops open a "Queries & Connections")
Right click on Table1 then edit.
There are many options to choose from such as: remove columns, filter...
To calculate new columns use > Add Column which uses M function.
For Vlookup use Merge Queries,
For a Count, Sum, Average, or ... of rows (utilizing one or multiple columns) use "Group by".
After you "Close & Load" the table gets updated. If new values get entered into file.xlsx Table1, refresh the power query table and the data shows up.

How to change declared variable in OLE DB Query by editing a cell from Excel?

We have different connections and different pivot tables connected to them in our Excel sheet and we don't want to edit them each time using data-> properties-> definitions (editing) to get them updated.
It would be much greater if we had just cells to edit
For example:
And write someting like this:
In my case i get an error of course but maybe you know how avoid it?

Excel 2010 check cells in a row contain data before the Selection Change fires

I have an Excel 2010 data table which is driven by a query from MSSQL. The underlying query changes depending upon what options the user selects in the Excel workbook. I'm okay with changing the query and pulling the data.
After the data has been selected multiple users will be able to edit and append data to the Excel table and these changes will post back to the SQL database table. Due to the database table structure some of these cells within a given row are mandatory before any data can be inserted into MSSQL and/or potentially updated.
So what I'm trying to achieve is checks on whether certain columns in a row are blank after a cell is edited (I can do this via Worksheet Change) and also before they move off that row so I can bring up a message if all mandatory columns haven't been entered. I can't see any events that fire before Selection Change. My only thoughts on a workaround is to have a global variable row marker that is updated on Selection Change, i.e. it will store the previous row number. I can't use Excel's standard data validation functionality looking at blank cells because although this is fine for a currently correctly populated row that is being edited, inserting new rows or appending directly to the bottom of the table will constantly error as all those mandatory columns will, of course, be blank. Currently I am using conditional formatting to at least highlight columns/cells that require input although this doesn't force users to actually do it. Data cannot be stored within MSSQL until these columns contain data so if they don't fill them in and refresh the table for whatever reason, whatever they have entered will be lost. Obviously this is bad, m'kay. I am concerned about both the Worksheet Change and Selection Change events constantly firing and how that will affect workbook performance.
Any suggestions would be appreciated. Maybe I'm going about this all wrong so any ideas to make this more efficient would also be well received. The user base do not want to see UserForms or MS Access even though it would make this activity very easy. They are too used to the look and feel of Excel sheets.
your best way is to copy the table into 2d array or some other data structure in memory such as dictionary or collection. and than manage each change in memory. this one is very efficient but requires a lot of code. with excel the only problem you have is the key the rest is vlookup and true false questions. vlookup will find the original value and then you have current data + previous data + the logic... is the new data ok?

Excel Show Data Based on user Choice

Excel 2007: I get my data from Access tables the Excel sheet is called "Data". My Excel report cells are populated by linking to those data cells(=Data!A1) and some report cells have simple code. But my question is this; my report needs to specify one record. Example: Data record 1: Name=Joe, Birth=7/29/62, Country=USA: record 2: Name=Sam, Birth=4/13/74, Country=China... How do we control the report display to a specific record? If I add a DropDown or ListBox, how do we make only the records for the name picked show in the report. Is there a way to put the code behind the listbox, or should I scrap the list and create a macro, what is the best way for a user to pick the data to be displayed on the report? Suggestions please...
There are two approaches I would use.
When you embed a query object in Excel (which is, I assume, how you are fetching the data from Access) you have the ability to edit that query in the little Query Editor tool.
You can add criteria to that query in this tool - there's a filter icon, you select let's say the Name column, and then type "Sam" in the box below, and that will produce a SQL statement that now includes "Where Name = 'Same'" at the end.
However, that's not that helpful for reporting, since most likely we want the name to be a parameter passed in from the Excel sheet itself. If in the criteria box, you type "[enter name]" instead of "Sam", the square brackets tell the Query tool that this is a user supplied parameter. When you run the query from the tool now you get a pop up box asking you each time for the name.
Now when you exit the query tool and return to Excel, you can edit the Query Parameters for that embedded query object. The parameter you made (name) is there, and it can either take on a static value, prompt the user, or take the contents of a cell. You can even set a flag to refresh the query when the cell contents change.
The other approach goes a little further and manipulates the query object in some VBA code. If you need to do some heavier lifting, or other logic, for example, and you don't want to deal with writing it all out as spreadsheet formulas. An easy way to do that is to drop in a button on the report sheet and have the VBA code start on that button's click event.

Import a single Excel cell into SSIS

Am trying to import a number of metric values from an Excel file into SSIS.
I have named each of the cells with data and was hoping to be able to configure a Connection, that would be updated in a ForEach container, to point to each Named Range in turn, in order to bring over the data one value at a time.
I see many articles on how to connect to a Sheet or Table in Excel, but none to a Named Range? I saw one article on how to bring over one single cell, but that cell was a part of a table.
Can I setup a Connection in SSIS to a single cell, Named or otherwise, and bring back that value?
JK
I can see you implementing this in one of two ways. The first is just a straight Execute SQL Task that returns a single row. The other being a data flow with, probably a script task as your source.
With each pass through your loop, you'd probably need to modify the Excel connection manager and/or your query string to point to the correct named range
In the section To create a linked server against an Excel spreadsheet
To access data from an Excel spreadsheet, associate a range of cells
with a name. A named range can be accessed by using the name of the
range as the table name. The following query can be used to access a
named range called SalesData using the linked server set up in the
previous example.
This article also describes programmatically access Excel via C#, albeit from ASP.NET but the principal should be the same. My hazy recollection is that the worksheet name would have a $ appended to it, thus sheet1$ while accessing the named range would be without the $.
One thing we ran into with our implementation was our servers did not have the appropriate drivers on them and it required us to install the Access engine
Lots of generalities in this answer so if you run into specifics, feel free to ping me.
Take a look at the top two answers from this question:
Want to insert excel file data into table using ssis - format problem
which explains 2 different approaches to doing what you ask.
Here is how to do it http://www.mssqltips.com/sqlservertip/1930/use-ssis-to-import-one-cell-of-an-excel-file-into-sql-server/#comments.
Unfortunately it didn't help me because i want to set a single variable with the values and use it later on.

Resources