Help and Support
 

powered byLive Search

HOWTO: How to Use ODBC to Run SQL SELECT on Microsoft Excel Data

Article ID:130137
Last Review:February 22, 2005
Revision:2.2
This article was previously published under Q130137

SUMMARY

This article gives example SQL SELECT statements that you can use against Microsoft Excel versions 4.0 and 5.0 worksheets in Visual FoxPro by using ODBC.

MORE INFORMATION

You should already have created data sources to your Microsoft Excel files. For more information about how to do this, please see the following article in the Microsoft Knowledge Base:
130732 (http://support.microsoft.com/kb/130732/EN-US/) How to Create Data Source to Microsoft Excel Files
To execute an SQL SELECT against a Microsoft Excel version 5.0 workbook, you may need to execute the SQLTABLES() function to get the names of the worksheets that reside in the workbook. This will build a cursor that you can then browse to see the actual names of the worksheets to use. Here is an example use of the SQLTABLES function:
   handle  = SQLCONNECT(<data source>,<username>,<password>)
   success = SQLTABLES(handle)
				
The cursor created by this code has five fields, the third of which contains the table names. In the case of Microsoft Excel version 5.0, these table names are actually the names of the worksheets within the workbook to which you have connected. They all have a dollar sign ($) as the last character, and you must be sure to include the dollar sign when accessing the worksheet.

Here is an example of an SQL SELECT to a Microsoft Excel version 5.0 file:

   handle  = SQLCONNECT('Excel 5.0 data source','','')
   success = SQLEXEC(handle,'select * from "sheet1$"')
				
Note that the sheet name is surrounded by double quotation marks, with the entire select statement inside single quotation marks. This is the required syntax.

Here is an example of an SQL SELECT to a Microsoft Excel version 4.0 file:
   handle  = SQLCONNECT('Excel 4.0 data source','','')
   success = SQLEXEC(handle,'select * from sheet1')
				
Note the absence of double quotation marks surrounding the sheet name.

APPLIES TO
Microsoft Visual FoxPro 3.0 Standard Edition
Microsoft Visual FoxPro 6.0 Professional Edition
Microsoft Data Access Components 2.5

Back to the top

Keywords: 
kbcode kbhowto KB130137

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.