473,788 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Column Type

Hi,

I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
integer and 2 & 4 are text).
Now i want to export the Table to an Excel workbook.
the next code i used is working (almost) fine.

///Code
'Create a Recordset from all the records in the Orders table.
Dim sNWind As String
Dim conn As New ADODB.Connectio n()
Dim rs As ADODB.Recordset
conn.Open("Prov ider=Microsoft. Jet.OLEDB.4.0;D ata Source=" & _
sNorthwind & ";")
conn.CursorLoca tion = ADODB.CursorLoc ationEnum.adUse Client
rs = conn.Execute("O rders", , ADODB.CommandTy peEnum.adCmdTab le)

'Create a new workbook in Excel.
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.Add
oSheet = oBook.Worksheet s(1)

'Transfer the field names to Row 1 of the worksheet:
'Note: CopyFromRecords et copies only the data and not the field
' names, so you can transfer the fieldnames by traversing the
' fields collection.
Dim n As Int32
For n = 1 To rs.Fields.Count
oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A 2").CopyFromRec ordset(rs)

'Save the workbook and quit Excel.
oBook.SaveAs(sS ampleFolder & "Book3.xls" )
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

'Close the connection
rs.Close()
conn.Close()
///Code

There is one thing that the code does not. And that is, copying the
columnpropertie s(integer, text) to Excel.

Does anyone know how i can acomplish this.

Thanks in advance
Nov 21 '05 #1
14 1783
Niels,

Why are you using Adodb wherefore you use interop while there is ADONET.

Here is in my idea a complete sample how to do what you want using ADONET.

http://support.microsoft.com/default...b;EN-US;316934

I hope this helps?

Cor

"Niels" <ni****@holland ridderkerk.nl>
Hi,

I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
integer and 2 & 4 are text).
Now i want to export the Table to an Excel workbook.
the next code i used is working (almost) fine.

///Code
'Create a Recordset from all the records in the Orders table.
Dim sNWind As String
Dim conn As New ADODB.Connectio n()
Dim rs As ADODB.Recordset
conn.Open("Prov ider=Microsoft. Jet.OLEDB.4.0;D ata Source=" & _
sNorthwind & ";")
conn.CursorLoca tion = ADODB.CursorLoc ationEnum.adUse Client
rs = conn.Execute("O rders", , ADODB.CommandTy peEnum.adCmdTab le)

'Create a new workbook in Excel.
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.Add
oSheet = oBook.Worksheet s(1)

'Transfer the field names to Row 1 of the worksheet:
'Note: CopyFromRecords et copies only the data and not the field
' names, so you can transfer the fieldnames by traversing the
' fields collection.
Dim n As Int32
For n = 1 To rs.Fields.Count
oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A 2").CopyFromRec ordset(rs)

'Save the workbook and quit Excel.
oBook.SaveAs(sS ampleFolder & "Book3.xls" )
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

'Close the connection
rs.Close()
conn.Close()
///Code

There is one thing that the code does not. And that is, copying the
columnpropertie s(integer, text) to Excel.

Does anyone know how i can acomplish this.

Thanks in advance

Nov 21 '05 #2
Niels,

Why are you using Adodb wherefore you use interop while there is ADONET.

Here is in my idea a complete sample how to do what you want using ADONET.

http://support.microsoft.com/default...b;EN-US;316934

I hope this helps?

Cor

"Niels" <ni****@holland ridderkerk.nl>
Hi,

I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
integer and 2 & 4 are text).
Now i want to export the Table to an Excel workbook.
the next code i used is working (almost) fine.

///Code
'Create a Recordset from all the records in the Orders table.
Dim sNWind As String
Dim conn As New ADODB.Connectio n()
Dim rs As ADODB.Recordset
conn.Open("Prov ider=Microsoft. Jet.OLEDB.4.0;D ata Source=" & _
sNorthwind & ";")
conn.CursorLoca tion = ADODB.CursorLoc ationEnum.adUse Client
rs = conn.Execute("O rders", , ADODB.CommandTy peEnum.adCmdTab le)

'Create a new workbook in Excel.
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.Add
oSheet = oBook.Worksheet s(1)

'Transfer the field names to Row 1 of the worksheet:
'Note: CopyFromRecords et copies only the data and not the field
' names, so you can transfer the fieldnames by traversing the
' fields collection.
Dim n As Int32
For n = 1 To rs.Fields.Count
oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A 2").CopyFromRec ordset(rs)

'Save the workbook and quit Excel.
oBook.SaveAs(sS ampleFolder & "Book3.xls" )
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

'Close the connection
rs.Close()
conn.Close()
///Code

There is one thing that the code does not. And that is, copying the
columnpropertie s(integer, text) to Excel.

Does anyone know how i can acomplish this.

Thanks in advance

Nov 21 '05 #3
On Thu, 11 Nov 2004 10:23:32 +0100, "Cor Ligthert" <no************ @planet.nl> wrote:

¤ Niels,
¤
¤ Why are you using Adodb wherefore you use interop while there is ADONET.
¤

CopyFromRecords et requires an ADO or DAO Recordset object. It doesn't support ADO.NET.
Paul ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #4
On Thu, 11 Nov 2004 10:23:32 +0100, "Cor Ligthert" <no************ @planet.nl> wrote:

¤ Niels,
¤
¤ Why are you using Adodb wherefore you use interop while there is ADONET.
¤

CopyFromRecords et requires an ADO or DAO Recordset object. It doesn't support ADO.NET.
Paul ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #5
>
CopyFromRecords et requires an ADO or DAO Recordset object. It doesn't
support ADO.NET.

I know that, did I write that somewhere Paul?

Cor
Nov 21 '05 #6
>
CopyFromRecords et requires an ADO or DAO Recordset object. It doesn't
support ADO.NET.

I know that, did I write that somewhere Paul?

Cor
Nov 21 '05 #7
On 10 Nov 2004 23:23:55 -0800, ni****@hollandr idderkerk.nl (Niels) wrote:

¤ Hi,
¤
¤ I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
¤ integer and 2 & 4 are text).
¤ Now i want to export the Table to an Excel workbook.
¤ the next code i used is working (almost) fine.
¤
¤ ///Code
¤ 'Create a Recordset from all the records in the Orders table.
¤ Dim sNWind As String
¤ Dim conn As New ADODB.Connectio n()
¤ Dim rs As ADODB.Recordset
¤ conn.Open("Prov ider=Microsoft. Jet.OLEDB.4.0;D ata Source=" & _
¤ sNorthwind & ";")
¤ conn.CursorLoca tion = ADODB.CursorLoc ationEnum.adUse Client
¤ rs = conn.Execute("O rders", , ADODB.CommandTy peEnum.adCmdTab le)
¤
¤ 'Create a new workbook in Excel.
¤ Dim oExcel As Object
¤ Dim oBook As Object
¤ Dim oSheet As Object
¤ oExcel = CreateObject("E xcel.Applicatio n")
¤ oBook = oExcel.Workbook s.Add
¤ oSheet = oBook.Worksheet s(1)
¤
¤ 'Transfer the field names to Row 1 of the worksheet:
¤ 'Note: CopyFromRecords et copies only the data and not the field
¤ ' names, so you can transfer the fieldnames by traversing the
¤ ' fields collection.
¤ Dim n As Int32
¤ For n = 1 To rs.Fields.Count
¤ oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
¤ Next
¤
¤ 'Transfer the data to Excel.
¤ oSheet.Range("A 2").CopyFromRec ordset(rs)
¤
¤ 'Save the workbook and quit Excel.
¤ oBook.SaveAs(sS ampleFolder & "Book3.xls" )
¤ oSheet = Nothing
¤ oBook = Nothing
¤ oExcel.Quit()
¤ oExcel = Nothing
¤ GC.Collect()
¤
¤ 'Close the connection
¤ rs.Close()
¤ conn.Close()
¤ ///Code
¤
¤ There is one thing that the code does not. And that is, copying the
¤ columnpropertie s(integer, text) to Excel.
¤
¤ Does anyone know how i can acomplish this.
¤

Excel determines the data types based upon the pattern of the incoming data. It does not consider
the data type of each column in the source.

I don't know how you are checking the column properties but each column should have a data type that
was determined at the time of import.
Paul ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #8
On 10 Nov 2004 23:23:55 -0800, ni****@hollandr idderkerk.nl (Niels) wrote:

¤ Hi,
¤
¤ I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
¤ integer and 2 & 4 are text).
¤ Now i want to export the Table to an Excel workbook.
¤ the next code i used is working (almost) fine.
¤
¤ ///Code
¤ 'Create a Recordset from all the records in the Orders table.
¤ Dim sNWind As String
¤ Dim conn As New ADODB.Connectio n()
¤ Dim rs As ADODB.Recordset
¤ conn.Open("Prov ider=Microsoft. Jet.OLEDB.4.0;D ata Source=" & _
¤ sNorthwind & ";")
¤ conn.CursorLoca tion = ADODB.CursorLoc ationEnum.adUse Client
¤ rs = conn.Execute("O rders", , ADODB.CommandTy peEnum.adCmdTab le)
¤
¤ 'Create a new workbook in Excel.
¤ Dim oExcel As Object
¤ Dim oBook As Object
¤ Dim oSheet As Object
¤ oExcel = CreateObject("E xcel.Applicatio n")
¤ oBook = oExcel.Workbook s.Add
¤ oSheet = oBook.Worksheet s(1)
¤
¤ 'Transfer the field names to Row 1 of the worksheet:
¤ 'Note: CopyFromRecords et copies only the data and not the field
¤ ' names, so you can transfer the fieldnames by traversing the
¤ ' fields collection.
¤ Dim n As Int32
¤ For n = 1 To rs.Fields.Count
¤ oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
¤ Next
¤
¤ 'Transfer the data to Excel.
¤ oSheet.Range("A 2").CopyFromRec ordset(rs)
¤
¤ 'Save the workbook and quit Excel.
¤ oBook.SaveAs(sS ampleFolder & "Book3.xls" )
¤ oSheet = Nothing
¤ oBook = Nothing
¤ oExcel.Quit()
¤ oExcel = Nothing
¤ GC.Collect()
¤
¤ 'Close the connection
¤ rs.Close()
¤ conn.Close()
¤ ///Code
¤
¤ There is one thing that the code does not. And that is, copying the
¤ columnpropertie s(integer, text) to Excel.
¤
¤ Does anyone know how i can acomplish this.
¤

Excel determines the data types based upon the pattern of the incoming data. It does not consider
the data type of each column in the source.

I don't know how you are checking the column properties but each column should have a data type that
was determined at the time of import.
Paul ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #9
On Thu, 11 Nov 2004 17:08:08 +0100, "Cor Ligthert" <no************ @planet.nl> wrote:

¤ >
¤ > CopyFromRecords et requires an ADO or DAO Recordset object. It doesn't
¤ > support ADO.NET.
¤ >
¤ I know that, did I write that somewhere Paul?
¤
¤ Cor
¤

You asked him why he was using ADODB when he could be using ADO.NET.

If you're suggesting he abandon both ADO *and* Excel automation then you are correct in that he can
use ADO.NET instead. Otherwise, if he needs to use Excel automation then he has no choice other than
to use ADO.

Does that make sense?
Paul ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1680
by: aspnetpal | last post by:
Hi Every1, I'm getting this error. 'Cannot specify a column width on data type smalldatetime.' I'm using SQL Server & I've check the data type in the db & it's same as smalldatetime. Any suggestions would be highly appreciated. Thanks <Error> Column or parameter #8: Cannot specify a column width on data type
4
10100
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception since DBNull cannot be converted to int. I wrote the following method that looks up the column's data type and if it is a ValueType, returns the default value for the ValueType.
6
3248
by: Robert Schuldenfrei | last post by:
Dear NG, After being away from C# programming for a spell, I am trying my hand at what should be a simple task. I have been hitting my head against the wall this morning. I have a simple order entry application. The code below gets line items from a SQL Server database and returns them to a datagrid by way of a DataTable called lineTable. As long as I am just displaying columns in the SQL database everything works well. I now want...
3
7489
by: Bob Day | last post by:
VS 2003, sql How do you determine the data type of a column if its value is DBNull? 1)Table: Column1 STRING non-nullable 2) Fill to a DataSet via DataAdapter 3) dim Data_Type_Is as object
4
7254
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which - "Modified" of type DateTime - is hidden since it should not be edited by a user. The system handles the update for this column. So, I have hidden (Visible=false) this column on the grid. In order to access the value in this field, I have created a...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.