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.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,501
Messages
5,832,079
Members
430,110
Latest member
Chyke_mxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top