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!

VB6 export to & import from Excel (text vs. numeric format cells)

Status
Not open for further replies.

UBfoolin

Programmer
Nov 29, 2001
32
US

I can not read data that I have written into Excel from VB, back into VB after it is
edited in Excel by the user. It appears that the numeric data is going into
Excel as a text cell. It is a CHAR 7 defined field in mySQL. When the cell
is edited in Excel, the format changes to numeric and when I read the data
back into Excel, the value of the cell is "". If there is no user editing
in Excel, and the cell retains it's text formatting, I can read it back
into VB with no problem.

Also, when the data is first opened in Excel, the number is left justified
in the cell, as text should be. Upon editing the cell, the number becomes
right justified, as a number should be, and the import back into VB fails.
If I change the properties of the cell from numeric to text, the import
picks up the edited data.

If the user enters the data like this '123 (with a leading single quote)
then Excel treats it as text and the VB import works. Also, if the user
enters V123 it imports fine too.

I am using Excel 97 to edit the data and I can not have my users entering
numerics with the leading single quote.


My code to read from Excel is:

Dim csql As String
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; " + _
"Data Source=c:\eSheet.xls;Extended Properties=Excel 5.0;"
cnn.Open

Dim adoPrimaryRS As Recordset
Set adoPrimaryRS = New Recordset

csql = "SELECT [Sheet1$].* From [Sheet1$]"
adoPrimaryRS.Open csql, cnn, adOpenStatic, adLockOptimistic


My code to write to Excel is:

Dim strSQL As String
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
xlSheet.Name = "Sheet1"

'Insert column headings
xlSheet.Cells(1, 1).Value = "Range Start"
xlSheet.Cells(i, 1).Value = rsData("Method Code")

xlSheet.SaveAs "c:\eSheet.xls"
xlApp.Quit


Can anyone see my error or point me in the right direction?
Thank you VERY much.


 
Read the data the way you write to it. Don't use the ODBC Excel drivers. There are large problems with it. If Excel assumes a column to be numeric (because the first row is) then finds a number but in textual format it will return blank.

I gave up on the ODBC driver long ago because it is to flakey. Better to go in via code and just get the cell().value and turn it to a number myself if I want.
 
i've run into this before, my soln was to format the column to the data type i knew it had to be prior to accessing any of the data in the worksheet
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top