Solved

open Database connection on excel vba example

Posted on 2010-08-23
3
776 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

896 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now