Solved

open Database connection on excel vba example

Posted on 2010-08-23
3
782 Views
Last Modified: 2012-05-10
Hi,
I need help to guide me how could I open connection, recordset from excel vba
what I looking is ODBC connection with recordset and paste records on excel cells....
That's !
Thank you!
0
Comment
Question by:fsze88
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
MSmax earned 500 total points
ID: 33508071
hi, try below code:
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset

cnn.Open "YourDSN", "YourUsername", "YourPassword"
rst.ActiveConnection = cnn
rst.CursorLocation = adUseServer

rst.Source = "SELECT * FROM TABLE WHERE FIELD='data'"
rst.Open
variable = rst.Fields("FIELD") ' Do whatever you need to the data
rst.Close

Set rst = Nothing
Set cnn = Nothing

Open in new window

0
 
LVL 15

Author Comment

by:fsze88
ID: 33508185
hi, thank you

but It said User-defined type not defined.

Could you help?
Untitled.png
0
 
LVL 7

Assisted Solution

by:MSmax
MSmax earned 500 total points
ID: 33508212
That's because you haven't got references to ADO libraries.
To add them go to VBA Editor-Tools-References and chose Microsoft ActiveX Data Objects 2.x Library
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question