
Originally Posted by
ScotExcel
Hi there,
Is it possible to get SQL to run directly from an Excel cell. What I mean is is it possible to say have a prewritten SQL statement in cell A1 that when the spreadsheet is opened or refreshed the values generated are entered into cell A2?
Thanks in advance.
It's unbelievably possible. I'd need more details but I'll assume you've done nothing on it yet.
For this code to work you'll need to add a reference to "Microsoft ActiveX Data Objects [2.8] Library" (in VBA go to Tools > References, find it, tick it). 2.8 is in my reference list, if it's not in yours just use another one.
You'll have to figure out those XXXXXXXXXs yourself. I'm not sure if you're using sql server or mysql etc. Google "connection string" for whatever you're using (e.g. "sql server connection string"). Obviously you'll have to know your username and password as well.
I called the sheet "Data", you can change that to anything you want.
You can call that function from wherever, e.g. when the workbook opens.
(http://www.ozgrid.com/VBA/auto-run-macros.htm)
Edit:
More information on ADO, http://support.microsoft.com/kb/257819, http://msdn.microsoft.com/en-us/libr...2(SQL.80).aspx
Bookmarks