Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Reading data from Excel Worksheet in vb

below is the code I am using to read the column names from an excel
worksheet... its working fine except in two instances:
1) if i read from an empty worksheet then it returns F1 - how can i make it
not return anything
2) if i have multiple columns with different "types" of names then it
returns the generic (F1,F2,F3....) for example if i have 4 colums names:
"First Name", "Last Name", "1/1/2009", "City"
if these are the four coumn names(first row of the worksheet), then it
returns: "First Name", "Last Name", "F3", "City"
notice the F3 instead of the 1/1/2009 - how can i avoid this?

The user selects the excel worksheet to read from hence I dont know how many
columns are there and what kind of datatype is the column name...


Dim sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dataSource & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim conn As New OleDb.OleDbConnection(sConnectionString)
Dim columnList As New List(Of String)

da = New OleDb.OleDbDataAdapter("Select top 1 * from [" & worksheetName &
"]", conn)
da.Fill(dt)

For Each dc As DataColumn In dt.Columns
columnList.Add(dc.ColumnName)
Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Reading data from Excel Worksheet in vb

You should add sometype of unique identified into the worksheet so that you
can test for this condition in the macro. Try these changes for your othe
comments

Dim sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dataSource & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim conn As New OleDb.OleDbConnection(sConnectionString)
Dim columnList As New List(Of String)

da = New OleDb.OleDbDataAdapter("Select top 1 * from [" & worksheetName &
"]", conn)
da.Fill(dt)

if dt.rows.count 0 then
For Each dc As DataColumn In dt.Columns
columnList.Add(dc.ColumnName)
Next
else
Set dt = nothing
end if

"imranmp" wrote:

below is the code I am using to read the column names from an excel
worksheet... its working fine except in two instances:
1) if i read from an empty worksheet then it returns F1 - how can i make it
not return anything
2) if i have multiple columns with different "types" of names then it
returns the generic (F1,F2,F3....) for example if i have 4 colums names:
"First Name", "Last Name", "1/1/2009", "City"
if these are the four coumn names(first row of the worksheet), then it
returns: "First Name", "Last Name", "F3", "City"
notice the F3 instead of the 1/1/2009 - how can i avoid this?

The user selects the excel worksheet to read from hence I dont know how many
columns are there and what kind of datatype is the column name...


Dim sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dataSource & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim conn As New OleDb.OleDbConnection(sConnectionString)
Dim columnList As New List(Of String)

da = New OleDb.OleDbDataAdapter("Select top 1 * from [" & worksheetName &
"]", conn)
da.Fill(dt)

For Each dc As DataColumn In dt.Columns
columnList.Add(dc.ColumnName)
Next

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Reading data from Excel Worksheet in vb

Thanks Joel.. but it doesnt seem to work...
dt.rows.count returns 1 even if the worksheet is empty

"Joel" wrote:

You should add sometype of unique identified into the worksheet so that you
can test for this condition in the macro. Try these changes for your othe
comments

Dim sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dataSource & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim conn As New OleDb.OleDbConnection(sConnectionString)
Dim columnList As New List(Of String)

da = New OleDb.OleDbDataAdapter("Select top 1 * from [" & worksheetName &
"]", conn)
da.Fill(dt)

if dt.rows.count 0 then
For Each dc As DataColumn In dt.Columns
columnList.Add(dc.ColumnName)
Next
else
Set dt = nothing
end if

"imranmp" wrote:

below is the code I am using to read the column names from an excel
worksheet... its working fine except in two instances:
1) if i read from an empty worksheet then it returns F1 - how can i make it
not return anything
2) if i have multiple columns with different "types" of names then it
returns the generic (F1,F2,F3....) for example if i have 4 colums names:
"First Name", "Last Name", "1/1/2009", "City"
if these are the four coumn names(first row of the worksheet), then it
returns: "First Name", "Last Name", "F3", "City"
notice the F3 instead of the 1/1/2009 - how can i avoid this?

The user selects the excel worksheet to read from hence I dont know how many
columns are there and what kind of datatype is the column name...


Dim sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dataSource & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim conn As New OleDb.OleDbConnection(sConnectionString)
Dim columnList As New List(Of String)

da = New OleDb.OleDbDataAdapter("Select top 1 * from [" & worksheetName &
"]", conn)
da.Fill(dt)

For Each dc As DataColumn In dt.Columns
columnList.Add(dc.ColumnName)
Next

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Reading data from Excel Worksheet in vb

For another posting I found the return infomation from a Recordset. It says
there is a BOF and EOF propertiy. I don't know if it applies to your method.
try it. If nothing is returned I would think you are are the EOF.


When you open a Recordset, the current record is positioned to the first
record (if any) and the BOF and EOF properties are set to False. If there are
no records, the BOF and EOF property settings are True.

You can use the MoveFirst, MoveLast, MoveNext, and MovePrevious methods; the
Move method; and the AbsolutePosition, AbsolutePage, and Filter properties to
reposition the current record, assuming the provider supports the relevant
functionality. Forward-only Recordset objects support only the MoveNext
method. When you use the Move methods to visit each record (or enumerate the
Recordset), you can use the BOF and EOF properties to determine if you've
moved beyond the beginning or end of the Recordset.



"imranmp" wrote:

Thanks Joel.. but it doesnt seem to work...
dt.rows.count returns 1 even if the worksheet is empty

"Joel" wrote:

You should add sometype of unique identified into the worksheet so that you
can test for this condition in the macro. Try these changes for your othe
comments

Dim sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dataSource & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim conn As New OleDb.OleDbConnection(sConnectionString)
Dim columnList As New List(Of String)

da = New OleDb.OleDbDataAdapter("Select top 1 * from [" & worksheetName &
"]", conn)
da.Fill(dt)

if dt.rows.count 0 then
For Each dc As DataColumn In dt.Columns
columnList.Add(dc.ColumnName)
Next
else
Set dt = nothing
end if

"imranmp" wrote:

below is the code I am using to read the column names from an excel
worksheet... its working fine except in two instances:
1) if i read from an empty worksheet then it returns F1 - how can i make it
not return anything
2) if i have multiple columns with different "types" of names then it
returns the generic (F1,F2,F3....) for example if i have 4 colums names:
"First Name", "Last Name", "1/1/2009", "City"
if these are the four coumn names(first row of the worksheet), then it
returns: "First Name", "Last Name", "F3", "City"
notice the F3 instead of the 1/1/2009 - how can i avoid this?

The user selects the excel worksheet to read from hence I dont know how many
columns are there and what kind of datatype is the column name...


Dim sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
dataSource & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim conn As New OleDb.OleDbConnection(sConnectionString)
Dim columnList As New List(Of String)

da = New OleDb.OleDbDataAdapter("Select top 1 * from [" & worksheetName &
"]", conn)
da.Fill(dt)

For Each dc As DataColumn In dt.Columns
columnList.Add(dc.ColumnName)
Next

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Data from an Excel Sheet Andrew Kirkby Excel Programming 1 September 23rd 08 09:55 PM
Reading data arrays from multiple data files in excel Hankjam[_2_] Excel Discussion (Misc queries) 0 February 7th 08 08:29 PM
Reading data from PPT and purge data to Excel and image [email protected] Excel Programming 1 January 17th 08 06:04 AM
Reading data from excel P.Nishanthan Excel Programming 0 May 3rd 07 09:20 AM
Reading excel data into another aplication Sean Bartleet Excel Programming 2 October 19th 05 02:15 PM


All times are GMT +1. The time now is 08:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"