Brothersoft.comWindows|Mac|Mobile|Games

|Message

Solve software problem quickly Share experience to help others Improve the ability of solving problem
How to Run an SQL Statement Against an Excel Spreadsheet

11/03/2011 01:25 by wawaslope

First answer posted by SandraMerz at 11/03/2011 01:25
Add Your Answer
10~1000 characters in length CAPTCHA:
2 Answers
  • SandraMerz
    1. Start Microsoft Excel 2007 and type "Column1" in "A1", "Column2" in "B1."

      Type "A" in "A2", "B" in "A3", "C" in "A4", "D" in "A5."

      Type "1" in "B2", "2" in "B3", "3" in "B4", "4" in "B5."

      Save your spreadsheet to "C:\" as "book1.xls."

    2. Start Microsoft Office Access 2007 and click "Blank Database," then select "Create." Click "Database Tools" and select "Visual Basic." Click the "Insert" field and select "Module."

    3. Type the following to create the "runSQLExcel" subroutine:

      Private Sub runSQLExcel()

      Press "Enter."

    4. Type the following to define your variables:

      Const adOStatic = 3

      Const adLkOpt = 3

      Const adCmdString = &H1

    5. Type the following to define and open connections to the Excel spreadsheet:

      Set objectConn = CreateObject("ADODB.Connection")

      Set objectRst = CreateObject("ADODB.Recordset")

      objectConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _

      "Data Source=C:\Book1.xls;" & _

      "Extended Properties=""Excel 8.0;HDR=Yes;"";"

    6. Type the following to run a SQL statement and open a new Recordset:

      objectRst.Open "SELECT * FROM [Sheet1$]", _

      objectConn, adOStatic, adLkOpt, adCmdString

    7. Type the following to loop through your Recordset and display results for both of the columns in your spreadsheet:

      Do Until objectRst.EOF

      MsgBox "Colum1:" & objectRst.Fields.Item("Column1").Value & " " & _

      "Colum2:" & objectRst.Fields.Item("Column2").Value

      objectRst.MoveNext

      Loop

      Press "F5" to run your subroutine.

  • 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:00
Add Your Answer
10~1000 characters in length CAPTCHA:
Related Questions
Q:How to Create a Zero Length String in SQL Server

A: Click the Windows "Start" button and select "All Programs." Click "SQL Server," then click "SQL Server Management Studio." The Management St...(more)

Q:How to Insert an SQL Statement in Access

A: Click the "Create" tab and select "Query Design." Select the tables you wish to query as displayed in the "Show Table" window, and click "Ad...(more)

Q:How to Create PLSQL

A: Start your script with a "DECLARE" block. The "DECLARE" block contains the variables, types and other definitions needed for your script. Ea...(more)

Q:How to Use LIMIT in SQL

A:Use LIMIT in MySQL Open up MySQL Monitor and log in. Select the database your wish to query by entering the following command: use databas...(more)

Q:How to Write SQL Statements in Microsoft Access

A: Open Access, then open the database that you want to query. Click on "Objects" and "Queries." Select "Create Query," then click "SQL Vie...(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

×