How to extract data from a database and populate a sheet in Excel - vba

I am storing data in a backend database (PostgreSQL) which is running on a Linux machine. I want to be able to fetch data from the database, and populate a sheet in an excel workbook, so that I can carry out analysis in Excel.
It has been quite a while since I wrote anything in VBA, so I would appreciate some help (or links) in getting started. I would like to know the best way to approach this:
Pure VBA solution OR
Mixture of C# or other .Net language for data extraction logic and VBA for manipulating Excel objects (sheet data population etc)
Any ideas, tips, snippets and/or links that can help me get started on the twin objectives:
fetching data from a backend database (PostgreSQL) into Excel
populating a specified sheet in Excel with the columnar data retreived from the database
will be much appreciated.

If you are just trying to import data, Excel can do that without additional code. Just set up your windows client to connect to your postgreSQL server thru ODBC. (Here's one way guide to setting that part up: enter link description here
Then in Excel (Use a modern version, like 2007 or greater) from the "Data" tab, click on "Existing Connections" to connect to the ODBC connection you set up and pick the tables/data to import into excel. Once the spreadsheet has loaded the data, you can just click the "Refresh All" button to update the data.

Related

How do I to build and then populate a multi-tab excel spreadsheet using an SSIS package?

I currently have a package that creates an excel spreadsheet with multiple tabs, and it names each column in each tab. Later in the package, I have excel connections that select my data using queries and populates each column in each tab based on the mappings I define. This has worked for a long time, but since a driver update, my OLE DB connections wont let me create the excel spreadsheet anymore. The key here is that the package needs to be able to be called by a scheduling system and be totally hands off and self sufficient. Also, the excel spreadsheet is created brand new with every execution of the package so we can keep an archive of old reports.
I've tried to use the existing excel connections to also build the spreadsheet, but I don't see a way to populate multiple tabs in the same spreadsheet with excel connections.
Is there a better way to do this without having to do a C# script.

Export SQL code from Excel into SQL

My skill level: basic user of VBA, know next to nothing about SQL (though I’ve recently learned how to execute code, etc).
I’ve created a user interface in an Excel worksheet to allow user input of report variables (dates, etc). These pass into a second Excel worksheet that, through the use of various Excel statements, creates the necessary SQL code. My macro then copies this worksheet and opens Microsoft SQL Server 2008 Rx via a shell command. Currently, I then manually click “connect” on the resultant Microsoft SQL Server 2008 R2 connect-to-server window (which does not need a password, just requires me to click “connect”), I create a new query, I paste the code in and then execute in SQL. So it is these latter steps (opening SQL, click “connect”, copy/paste code, execute) that I’m trying to automate via VBA.
To offer more context, once the SQL code has run, I then have a second macro that uses the Excel Data Connection wizard to bring the resultant SQL data back into my worksheet. This is working well.
I’ve researched this for several days on various boards. None seem to observe my approach of creating the code in Excel (probably because it is an inelegant approach). Or the post responses are beyond my skill level to understand. But I do fear that I’m asking a question that has been answered before (apologies in advance).
Thanks for whatever advice/time can be offered.
I’m using:
Excel/Office 2010
Microsoft SQL Server2008 R2 (is this the same as Microsoft SQL Server Management Studios? My SQL application shows both names)
Microsoft ActiveX Data Objects 6.1 Library (this is NOT currently checked off. One post I saw referenced the need for this. I have multiple prior AciveX Library versions available as well.)
What you're doing is very ingenious, but it's probably not necessary. Excel has a number of features for importing external data.
I would look at the Data tab in Excel and the From Other Sources section of the ribbon. You have at least three options: the From SQL Server, From Data Connection Wizard, and From Microsoft Query options.
All of these will set up a connection to a database server and execute SQL against it. They return data to your spreadsheet in different ways.
Where VBA will come into play again is in automating the generation and parameterization of the queries these will execute.
But I would set up something that works, with constants for your parameter values, first, so you get an idea of the possibilities, and then read and research dynamically setting your SQL with VBA.
You might also investigate PowerPivot, by the way.

Updating a database from an Excel spreadsheet in .NET

Can Teradata tables be updated in an VB.NET application by reading an Excel spreadsheet and pulling certain values from certain columns? If so how?
Yes.
This link shows how to read from a server database and update Excel, via ADO.NET and two OleDb connections.
It should be easy to modify that code to do the reverse. ; you'd just need to swap the connection strings shown in that code, so that the source is Excel, and the destination is your Teradata db.
There are some Excel-specific things in the queries, but you get the idea.
NB: This is not Excel Automation. It uses ADO.NET to read/update an Excel sheet. You don't need Excel to be installed on the machine in order to do these queries.

Writing data back to SQL from Excel sheet

I know it is possible to get data from a SQL database into an excel sheet, but i'm looking for a way to make it possible to edit the data in excel, and after editing, writing it back to the SQL database.
It appears this is not a function in excel, and google didn't come up with much usefull.
If you want to have the Excel file do all of the work (retrieve from DB; manipulate; update DB) then you could look at ActiveX Data Objects (ADO). You can get an overview at:
http://msdn.microsoft.com/en-us/library/ms680928(VS.85).aspx
You want the Import/Export wizard in SQL Management Studio. Depending on which version of SQL Server you are using, open SSMS (connect to the SQL instance you desire), right click on the database you want to import into and select Tasks.. "Import Data".
In the wizard, click Next (past the intro screen) and from the Data Source drop list select "Microsoft Excel". You specify the path and file name of the Excel spreadsheet, whether you have column headings or not.. then press Next. Just follow the wizard through, it'll set up the destination (can be SQL Server or another destination) etc.
There is help available for this process in SQL Server Books Online and more (a walkthrough) from MSDN.
If you need something deployable/more robust (or less wizard driven) then you'd need to take a look at SQL Server Integration Services (for a more "Enterprise" and security conscious approach). It's probably overkill for what you want to accomplish though.
There is a new Excel plug-in named "MySQL for Excel" : http://www.mysql.com/why-mysql/windows/
I just had a need to do this, and this thread has been quiet for a long time, so I thought it might be useful to supply a recent data point.
In my application roving salespeople use a copy of an Excel workbook that tracks the progress of a prospect through a loan application. The current stage of the application needs to be automatically saved back to a remote SQL database so that we can run reporting on it.
Rejected methods for updating the database from Excel:
SSIS and OpenRowSet are both methods for allowing SQL Server to pull the data from Excel, and don't work very well when the Excel workbook is sitting in an undefined location on a user's computer, and certainly not when the workbook is currently open in Excel.
ADO is now, if not actually deprecated, nevertheless looking very long in the tooth. Also, I wanted the solution to be robust in the face of the user possibly not being connected to the internet.
I also considered running a web API on the destination server. Macros in the Excel workbook connect to the web API to transfer data. However, it can sometimes be painful to allow a web API to talk to the outside world. Also, the code to make it robust in the face of temporary loss of internet connection is painful.
The adopted solution:
The solution I plan to adopt is low-tech: email. Excel emails the data to an address hosted on an Exchange server. Everyone in the company has Outlook installed, so the emails are sent by programmatically adding them to the Outlook Outbox. Outlook nicely handles the case when the user is offline. At the server end, a custom C# executable, fired up at regular intervals by the Task Scheduler, polls the inbox and processes the emails.
You could use try these add-ins :
www.QueryCell.com (I created this one)
www.SQLDrill.com
www.Excel-DB.net
You can use the OPENROWSET function to manipulate Excel data from a T-SQL script. Example usage would be:
UPDATE OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;DATABASE=c:\MySpreadsheet.xls',
'Select * from MyTable')
SET Field1='Value1' WHERE Field2 = 'Value2'

Exporting Excel cell data to database via Excel macro?

Can I create a macro written in C# for Excel that allows me to export a cell's data to a SQL Server or Access database?
Basically, I'd like to create a button in Excel that saves the data in certain cells to a database. Can this be done?
Instead of creating a UI from scratch for a program, I've decided using Excel as the user environment would be better for business needs.
How to automate Excel by using Visual C# to fill or to obtain data in a range by using arrays would be a good starting point.
Be aware, you're going to find it frustrating to control what people attempt to enter into the Excel sheet, as the inherit flexibility of the data works again your efforts to write it to a database destination.