Solved

Oldedb Excel create table command is converting hyphens into underscores

Posted on 2010-08-25
6
1,383 Views
Last Modified: 2013-11-17
Hi,

I'm creating an excel file with vb.net and ole db.  The table (worksheet) name has hyphens in it.  The hyphens (-) are being converted to underscores (_) when I execute the create table command.

Here is the code:
mcliOLEDBCn = New System.Data.OleDb.OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & astrPathAndFile & ";Mode=ReadWrite;Extended Properties=""Excel 8.0;HDR=YES"")

mcliOLEDBCom = New OleDbCommand("CREATE TABLE [AR-08-25-2010] ([ITEMNO] MEMO , [QUANTITY] int)", mcliOLEDBCn)
mcliOLEDBCn.Open()                
mcliOLEDBCom.ExecuteNonQuery()

The worksheet inside the excel file ends up named as AR_08_25_2010 instead of AR-08-25-2010.

I am using visual studio 2008, vb, and windows 7.  I tried it with the ace provider for excel 2007 and got the same result.

How do I insert hyphens into the table name?

Any help would be appreciated.  Thanks!

-William
0
Comment
Question by:WilliamAK
  • 2
  • 2
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33526204
Frankly, I don't believe that Excel supports table names with dashes, since they would be easily confused with the subtract operator.  I don't know of any way to tell the OLEDB provider to allow that kind of name.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33526549
Have you tried using a back quote?

mcliOLEDBCom = New OleDbCommand("CREATE TABLE [`AR-08-25-2010`] ([ITEMNO] MEMO , [QUANTITY] int)", mcliOLEDBCn)
mcliOLEDBCn.Open()                
mcliOLEDBCom.ExecuteNonQuery()
0
 

Author Comment

by:WilliamAK
ID: 33531839
Thanks for the replies.

TheLearnedOne, if I rename the sheet in excel to AR-08-25-2010 and then query it with lcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing) it shows as 'AR-08-25-2010$' but if i try to create table using single quotes it still converts the hyphens to underscores (but also changes the last quote to an underscore and sheet is unopenable, very strange.)  And if i try using single quotes and the dollar symbol I get a syntax error in CREATE TABLE.

Calacuccia, if I use a back quote i get a syntax error in CREATE TABLE.

I'm not really sure what the $ is for, maybe it means a temporary sheet or an instance sheet or something.
0
 
LVL 17

Accepted Solution

by:
calacuccia earned 500 total points
ID: 33532007
The $ is the real sheet name and needed to read an Excel table correctly.
And Excel perfectly accepts hyphens for sheet names, as you could test for yourself.

The $ symbol should not be use to Create a new table.

You found yourself that you need [] to correctly read a file with hyphens....

I'm afraid the hyphen being converte to underscores is part of OLEDB syntax ...I've been googling a long time yesterday without any idea how to get around this.

Is it possible to change the name of the worksheet from your VB Project once it's created?

myWB.Worksheets("AR_08_25_2010").Name = "AR-08-25-2010"


0
 

Author Comment

by:WilliamAK
ID: 33541771
Yes, I'll have to come up with some kind of workaround.  The app runs on a server that does not have office installed.

Thanks for the replies Calacuccia.  :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now