How to use ADO and VBA to Read from a Database

Опубликовано: 07 Май 2021
на канале: Excel Macro Mastery
43,901
1.4k

👉 Ready to master VBA?
Check out my full courses: https://courses.excelmacromastery.com/
Subscribe to the channel here: https://bit.ly/36hpTCY
-Want to download the source code for this video? Go here: https://bit.ly/3kkY2KP
(Note: If the download page doesn't work then make sure to turn off any popup blockers)

How to use ADO and VBA to Read from a Database

In this video I show you how to read from a database using ADO(ActiveX Data Objects) with VBA. We will look at how to retrieve the data to a RecordSet using and query and then how to write that data to a worksheet. We'll also take a look at how to close the connection correctly and late binding.


#VBAADO #VBADATABASE #ExcelVBAADO #ExcelVBADatabase

Useful VBA Shortcut Keys
========================

Debugging:
Compile the code: Alt + D + C OR Alt + D + Enter
Run the code from the current sub: F5
Step into the code line by line: F8
Add a breakpoint to pause the code: F9(or click left margin)

Windows:
View the Immediate Window: Ctrl + G
View the Watch Window: Alt + V + H
View the Properties Window: F4
Switch between Excel and the VBA Editor: Alt + F11
View the Project Explorer Window: Ctrl + R

Writing Code:
Search keyword under cursor: Ctrl + F3
Search the word last searched for: F3
Auto complete word: Ctrl + Space
Get the definition of the item under the cursor: Shift + F2
Go to the last cursor position: Ctrl + Shift + F2
Get the current region on a worksheet: Ctrl + Shift + 8(or Ctrl + *)
To move lines of code to the right(Indent): Tab
To move lines of code to the left(Outdent): Shift + Tab
Delete a Line: Ctrl + Y(note: this clears the clipboard)

Table of Contents:

00:00 - Introduction
00:30 - What is ADO?
00:50 - How to read from a Database
01:20 - Writing the connection code
02:41 - How to add the Query
04:45 - How to return the field headers
05:35 - Alternative way to use Recordset Open
06:35 - How to close the connection correctly
08:59 - How to use Late Binding