Solved

Oldedb Excel create table command is converting hyphens into underscores

Posted on 2010-08-25
6
1,372 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

708 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

13 Experts available now in Live!

Get 1:1 Help Now