VBA to read from Database?

zlac017

New Member
Joined
Sep 1, 2005
Messages
3
Hi, I'm quite new to excel macro/vba programming and need some guidence.

I need to write a macro that will (probably) use an ODBC DSN to connect to a database and select two columns COLA and COLB from TABLE1. I want it to place the contents of COLA into column A in my spreadsheet and COLB in column B in my spreadsheet.

Does this sound possibble? if so could anyone give me a good starting point?

I have already created an ODBC connection within Windows - is it possible to use this?

Thank you very much in advance.
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
As with most VBA the best idea is to start by recording a macro of the process as you do the job manually. The code can then be adapted later.

In this case the code may not really be necessary because once we have made the ODBC link it is only necessary to Refresh the data in the worksheet when required.

I usually find that it is best to keep a special sheet for the data alone and link to that for further analysis. That is not an absolute rule, but it does prevent us accidentally doing something that night screw up the Refresh.
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
And get the data into that sheet by using MS Query via: Data>Import External Data>New Database Query and follow the wizard prompts.

Richard
 

Watch MrExcel Video

Forum statistics

Threads
1,119,002
Messages
5,575,497
Members
412,670
Latest member
Khin Zaw Htwe
Top