We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Fine name of sheet/Table of uploaded XLS File (ASP)

Medium Priority
494 Views
Last Modified: 2013-11-19
I have script which uploads a xls document and inserts them into an access database by asp.
I select the records from the xls document with the statement:

set ExcelRS = ExcelConn.Execute ("SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM Contacts")

This works fine if the sheet is called Contacts.  But crashes if it is not.  Is it possible to find out the sheet name when the file is uploaded.
Comment
Watch Question

Ardhendu SarangiSr. Project Manager
CERTIFIED EXPERT

Commented:
Since you haven't given your complete code.. this is just a guess...

try the following code:

xlsName = thisworkbook.name
set ExcelRS = ExcelConn.Execute ("SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM" &xlsName)

and see if this works..

Ardhendu

Author

Commented:
My code is:
		
Set ExcelConn = Server.CreateObject("ADODB.Connection")
Set ExcelRS = Server.CreateObject("ADODB.Recordset")
ExcelConn.Provider = "Microsoft.Jet.OLEDB.4.0"
ExcelConn.Properties("Extended Properties").Value = "Excel 8.0"
ExcelConn.Open filepath & "\" & filename
 
set ExcelRS = ExcelConn.Execute ("SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM Contacts")

Open in new window

Ardhendu SarangiSr. Project Manager
CERTIFIED EXPERT

Commented:
Ok, can you try this and see if this works for you?

- Ardhendu
Set ExcelConn = Server.CreateObject("ADODB.Connection")
Set ExcelRS = Server.CreateObject("ADODB.Recordset")
ExcelConn.Provider = "Microsoft.Jet.OLEDB.4.0"
ExcelConn.Properties("Extended Properties").Value = "Excel 8.0"
ExcelConn.Open filepath & "\" & Filename
 
Set ExcelRS = ExcelConn.Execute("SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM " & Filename)

Open in new window

Author

Commented:
No getting the following error:

SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM csv-sample(3).xls
Microsoft JET Database Engine error '80040e14'

Syntax error in FROM clause.

/contacts/add/addcsv.asp, line 87

Set ExcelConn = Server.CreateObject("ADODB.Connection")
Set ExcelRS = Server.CreateObject("ADODB.Recordset")
ExcelConn.Provider = "Microsoft.Jet.OLEDB.4.0"
ExcelConn.Properties("Extended Properties").Value = "Excel 8.0"
ExcelConn.Open filepath & "\" & filename
 
'get data from sheet
'set ExcelRS = ExcelConn.Execute ("SELECT FirstName, LastName, [EmailAddress], Country, Company FROM [csv-sample$]")
sSQL="SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM " & filename
response.Write("<br>" & sSQL)
set ExcelRS = ExcelConn.Execute (sSQL)     'Line 87

Open in new window

Ardhendu SarangiSr. Project Manager
CERTIFIED EXPERT

Commented:
Can you try replacing my previous post with these lines below?

- Ardhendu.
Set ExcelConn = Server.CreateObject("ADODB.Connection")
Set ExcelRS = Server.CreateObject("ADODB.Recordset")
ExcelConn.Provider = "Microsoft.Jet.OLEDB.4.0"
ExcelConn.Properties("Extended Properties").Value = "Excel 8.0"
ExcelConn.Open filepath & "\" & Filename
 
Set ExcelRS = ExcelConn.Execute("SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM " & Left(Filename, Len(Filename) - 4))

Open in new window

Author

Commented:
No still gettin the error:

SELECT FirstName, LastName, EmailAddress, HomeCountry, Company FROM csv-sample(4)
Microsoft JET Database Engine error '80040e14'

Syntax error in FROM clause.

Ardhendu SarangiSr. Project Manager
CERTIFIED EXPERT

Commented:
Hi Harris,
Is "cvs-sample(4)" the exact name of excel file where you want to run the Query on? I am not sure why this is erroring out when the original file Contact.xls is working fine... I am not sure what I am missing here...

- Ardhendu
Hi,
Yes that is the correct filename.  Is it something to do with the Contacts Exported from outlook having a "named Range"?

I have changed the driver and it works with that by a different method.  Maybe this is a poor sql statement.  What do you think?
Set ExcelConn = Server.CreateObject("ADODB.Connection")
Set ExcelRS = Server.CreateObject("ADODB.Recordset")
ExcelConn.Open "DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1; HDR=NO; "&_
    "Excel 8.0; DBQ=" & filepath & filename & "; "
 
sSQL = "SELECT * FROM A1:BL10000"
 
response.Write("<br>" & sSQL)
set ExcelRS = ExcelConn.Execute (sSQL)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.