Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference Excel fields over ADODB

Status
Not open for further replies.

cthaxter

Programmer
Aug 2, 2001
71
US
I'm trying to connect to an Excel file through ADODB and manipulate it like any other database.

The following code from works just fine:

Code:
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "ADOExcel"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.ActiveConnection = objConn
objRS.CursorType = 3                    
objRS.LockType = 2                     
objRS.Source = "Select * from myRange1"
objRS.Open

The source could have also been

Code:
objRS.Source = "Select * from [myWorksheet1$]"

I can't figure out how to specify specific fields, so that I can pass parameters or create a recordset with only one or two fields. Usually, you just say "Select Name, Number From myTable" but in this case, Excel doesn't understand that. The myTable becomes [myWorksheet$], but what do Name and Number become?

Christopher
 
Thanks. Sorry, the problem I was having didn't have to do with naming, as it happens. I was just trying to use Options that weren't compatible with the type of recordset I was using 3 for Cursor, 4 for LockType, and 2 for Options, and I think the Options value was the problem. So, the following does work as the SQL for the Open method:

"Select Name, Number From [myWorksheet$]"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top