Brothersoft.comWindows|Mac|Mobile|Games

|Message

Solve software problem quickly Share experience to help others Improve the ability of solving problem
How to Connect Excel to SQL Using Visual Basic

11/03/2011 01:25 by KCornell

First answer posted by JavyTechGuy at 11/03/2011 01:25
Add Your Answer
10~1000 characters in length CAPTCHA:
2 Answers
  • JavyTechGuy
    1. Open Excel, then type a list of names in one column and ages in an adjacent column. Label the tops of these columns with the text "Names" and "Ages." This step creates a database of sample data for your SQL program to fetch. You can create a table with different data if you'd like.

    2. Drag a selection region around the table, then type "MyTable" in the text box to the left of the formula bar. This creates a range name for the sample data, which your program will need to access the data.

    3. Save the file as "C:\MyDatabase.xlsx," then close the file.

    4. Press "Control," followed by "N" to create a new spreadsheet. You'll store your SQL program in this sheet.

    5. Press "Alt," followed by "F11" to enter the VB development environment, commonly abbreviated as the "IDE." Developers write and sometimes execute VB programs in this IDE.

    6. Click the "Insert" menu, then click the "Module" item. This action creates a new code window for you to enter your SQL program listing.

    7. Click the "Tools" menu heading, then click "References." Place a check in the checkbox labeled "Microsoft ActiveX Data Objects." This action makes visible the database objects that Excel needs to run the SQL query.

    8. Paste the following program into the new code window.

      Sub sqlVBAExample()

      Dim objConnection As ADODB.Connection

      Dim objRecSet As ADODB.Recordset

      Set objConnection = New ADODB.Connection

      objConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myDatabase.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

      objConnection.Open

      Set objRecSet = New ADODB.Recordset

      objRecSet.ActiveConnection = objConnection

      objRecSet.Source = "Select * From myTable"

      objRecSet.Open

      Range("D10").CopyFromRecordset objRecSet

      objRecSet.Close

      objConnection.Close

      Set objRecSet = Nothing

      Set objConnection = Nothing

      End Sub

    9. Click one of the program's statements, then press "F5" to run the program.

    10. Press "Alt," and then "F11" to return to the Excel spreadsheet. The spreadsheet will display the results of the SQL connection that your program established. The results include the database table you created earlier.

  • Was this answer helpful? 00 · 11/03/2011 01:25
  • vikiying
  • Speaking of excel or word. .i have to admit that it has  brought us great convenience. so does excel barcode.i used it frequently. you can have a try. http://www.keepautomation.com/products/excel_barcode/

  • Was this answer helpful? 00 · 01/18/2012 23:01
Add Your Answer
10~1000 characters in length CAPTCHA:
Related Questions
Q:How to Connect Visual Basic to SQL

A: Launch Microsoft Visual Basic Express and click "New Project..." on the left pane of your screen then select "Console Application." Click "O...(more)

Q:How to Add Linefeeds to SQL Statements on the Oracle 10g

A: Connect to the Oracle SQL*Plus, click "Start," "All Programs," then click "SQL *Plus." Log onto the Oracle SQL*Plus Dialog Box, enter "Use...(more)

Q:what's The Easiest Way to Learn SQL?

A: Download a free SQL program editor. RazorSQL is but one of the many free options available (see References). Unless you've previously done a...(more)

Q:How to Use VBS to Return a Recordset

A: Type "Option Explicit" on your code's first line. This forces explicit declaration of every variable in your program. Type "Dim objConnectio...(more)

Q:How to Find the Date Difference in SQL

A: Log into the SQL server. Type the statement "DATEDIFF(day, 2010-01-01, 2012-03-02)" to calculate the number of days between January 01, 20...(more)

1 people are following

Ask a question now
Write a title for your question

Navicat MySQL Manager Lite

Navicat (MySQL Manager - a GUI for MySQL admin and Access to MySQL conversion)
Latest version:8.2.19
License:Freeware
OS:Windows 7/Vista/2003/XP/2000/2008
Total downloads:71,596
Rank:5 in Database Management
Download It

Share with friends

New features is coming

  • 1. Providing high-quality answers to be a verified Q&A expert
  • 2. Ask and answer questions to get Brothersoft credits

Added Successfully!

×

Are you sure to delete your answer?

NoYes

×

Are you sure to choose it as the best answer?

NoYes

×

Voted Successfully!

×

You can't vote for yourself

×

You can't choose your own answer

×