VSTO & .NET & Excel

February 4, 2012

The Great Microsoft Office Portal

Filed under: .NET & Excel, .NET Books, Excel, Valentina DB, Valentina Office Server, VSTO & Excel, XLLs — Dennis M Wallentin @ 4:46 pm

As most of us already know, Microsoft has for the last couple of years built up an enourmus  giant knowledge base about Microsoft Office on the internet. As a consequence it has also become more difficult to navigate around and find the wanted information we are looking for.

However, today I discovered the Great Portal to Microsoft Office knowledge Base. Instead of just saving the URL to my local computer I thought I would make it more available by publishing it here:

Office Development Site Map

One of the key people for the ongoing publishing of Microsoft Office knowledge is Erika Erhli. I have previously related to her so this is a (very good) reminder:

Adventures with Office Products & Technologies

Do You remember how it was during the 90’s? At that time it was rather easy to be updated on a numbers of softwares from Microsoft. Today the softwares have become much more, more complex and more advanced and on top of that the release cycles have become faster.

So I’m glad that I, at least, can keep myself updated of Microsoft Excel. However, in the future we may only be able to keep up with the rapid development for one platform that Microsoft Excel is used on. What do You think?

Kind regards,
Dennis

January 30, 2012

Best Practices Export Data to Excel

If You’re a VBA Developer You should know what best practices is to export data to MS Excel. Two expressions summarize it; Microsoft ActiveX Data Object Library and CopyFromRecordset.

However, if You’re a .NET developer and face a situation where You must find a solution to export data to MS Excel then there is a chance that You don’t know the best practices. In general, working on the .NET platform means that we use .NET classes to solve various tasks. When it comes to data acquiring two of the more popular approaches are to use ADO.NET and LINQ. However, as this blog article shows using classic ADO is the fastest way to dump data into a worksheet.

When lurking around various public Q&A forums I have noticed that the question “how to export data to MS Excel” is asked over and over. This article is the answer to that question and it will provide You with the best approach when it comes to speed.

Initially we need to set a reference to “Microsoft ActiveX Data Object x.x Library” where “x.x” refers to the version number in our .NET solution. Next, we must explicit add a reference to the Object Library which is done in the below code. Don’t forget to add the library to the prerequisites or copy the required files to the project.

Imports ADODB
Imports System.Text
Imports excel = Microsoft.Office.Interop.Excel

Public Class frmMain

Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) _
Handles btnExport.Click

Const stcon As String = _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\NorthWind.accdb"

Const stsql As String = "SELECT * FROM Invoices"

Dim cnt As New ADODB.Connection

Dim rst As New ADODB.Recordset

Dim fld As ADODB.Field

'Open the database connection.
cnt.Open(stcon)

'Open the Recordset.
With rst
.CursorLocation = CursorLocationEnum.adUseClient
.Open(stsql, cnt, ADODB.CursorTypeEnum.adOpenForwardOnly, _
ADODB.LockTypeEnum.adLockReadOnly, _
ADODB.CommandTypeEnum.adCmdText)
.ActiveConnection = Nothing
End With

'Closing the database connection.
cnt.Close()

'Variables for Excel and the created workbook.
Dim xlapp As New excel.Application
Dim xlwbook As excel.Workbook = xlapp.Workbooks.Add( _
excel.XlWBATemplate.xlWBATWorksheet)
Dim xlwsheet As excel.Worksheet = _
CType(xlwbook.Worksheets(1), excel.Worksheet)

Dim xlrange As excel.Range = xlwsheet.Range("A2")
Dim xlcalc As excel.XlCalculation

'Temporarily turning off the auto calculation.
With xlapp
xlcalc = .Calculation
.Calculation = excel.XlCalculation.xlCalculationManual
End With

Dim ifieldcounter As Integer = Nothing

'Writing the field names to the worksheet.
For Each fld In rst.Fields
xlrange.Offset(0, ifieldcounter).Value = fld.Name
ifieldcounter = ifieldcounter + 1
Next

'Dump the recordset into the worksheet.
xlrange.Offset(1, 0).CopyFromRecordset(rst)

'Closing the recordset.
rst.Close()

With xlapp
.Visible = True
.UserControl = True
'Restore the calculation mode.
.Calculation = xlcalc
End With

fld = Nothing
rst = Nothing
cnt = Nothing
xlrange = Nothing
xlwsheet = Nothing
xlwbook = Nothing
xlapp = Nothing
End Sub
End Class

That’s it!

Kind regards,
Dennis

December 21, 2011

Real Studio – The Perfect Companion for SMEs

Filed under: Real Basic Developing, SQLite, Valentina DB — Tags: , — Dennis M Wallentin @ 1:43 am

Introduction
In the beginning of this year I decided to explore Real Studio (formerly Realbasic, RS) which is a full featured cross-platform software development tool. With it we can create a wide range of applications, from small tiny utilities to enterprise applications that target Mac OS X, Windows, Linux and the web.

Initially I had an idea of just testing RS for some days, write a blog entry and then go back to .NET & MS Excel. Well, it turned out to something unexpected, I started to use it more and more. I even bought some third-party’s controls. Now I’m considering creating some standalone commercial tools on the RS platform.

Before we move on please put off  Your Excel hat and while doing so please also put off Your VS.NET hat. Also, bear in mind that while Microsoft is the largest software corporate in the world the company behind Real Studio, Real Software, is indeed a small corporate. What I try to say is; try to avoid the mistake to compare Real Studio vs Microsoft’s softwares and also Real Software vs Microsoft.

Real Studio
Real Software is the corporate behind RS and it was founded in 1996. The first version of RS was released in 1998. So after more than 10 years on the market RS should indeed be considered as a matured software and development platform.

RS is a unique software and development platform as it actually is targeting four different platforms, Mac OS X, Windows, Linux and the web. I find it amazing that we can create softwares for more than one platform in a rather smooth way and at the same time. To me it was a wow-factor when I saw my first application running on Ubuntu 11.10 (a Linux distribution) developed on Windows 7.

For me RS will always have a special place in my heart because RS has brought back the joy to develop applications! Yes, it’s true and I find myself smiling when doing some hobby project with RS. This may sound extraordinary but I have been developing solutions for MS Excel for more than 20 years using VBA, VB6 and VB.NET. I needed a reboot and RS was the tool for it.

What was it that made me to change from an opinion that just test it to really like RS and appreciate it as I actually do? The first thing that comes to my mind is simplicity, RS is really simpel to use yet so powerful. The second is the tight integration for databases in RS and how it interacts with databases. The third and final thing is how easy it’s to deploy it. It reminds me how easy it was to work with VB6. So for future release I have only one wish; please keep it clean and simple.

The first screen shot shows the start page when using the Enterprise version on Ubuntu 11.10:

The second screen shot shows the RS’s IDE in “visual mode”. For some it may look primitive but actually I have noticed that I have become more productive with this “simple” and clean interface as I find my ways relative fast (Enterprise version and Ubuntu 11.10):

RS is a strong supporter of Object Oriented Programming (OOP) techniques. But don’t be afraid, RS does not require that we must be OOP-gurus. In other words, RS offers us to use it but we are not forced to accept the offer.

One of the corner stones of RS is its Database class which allows us to connect and interact with several common databases. Not only that, since version 2005 RS is shipped with its own database, Real SQL Database. RS own database is built around SQLite, which is quite good to use for most of the projects. At least, that’s my opinion. The RealSQLDatabase is a subclass of the Database class. Take Your time to explore this part of RS and You’ll be surprised for what it actually can achieve. I find the Database part to be the best part in RS due to the tight integration in RS, it’s simplicity and the fact it doesn’t include only the RealSQLDatabase.

Of course, RS can also interact with other commercial databases such as Valentina DB. The following screen shot shows the built-in options for using databases (Enterprise version on Window 7 x86):

RS is also shipped with a built-in report generator that can be used to create reports to present data from databases. Just for fun You may check out the following two databases that work with RS as well:

What may surprise You, I was very surprised, is that RS is not shipped with any native Grid control or with a Chart control. Personally I have difficulties to accept it. OK, it exist some good commercial third-party controls (see below) but that shouldn’t be an excuse for not providing at least two basic controls with RS. After all, the missing components belong to the core of data presentation, linked to underlying databases, and therefore should be considered as a “must”.

For those of us who come from the VB world will find out that learning Realbasic has a short learning curve and that the learning curve is not steep. Realbasic can be described as a Visual Basic almost-clone but much better. It’s quite impressive that we here have a development platform that use Real Basic to develop solutions for four different platforms.

Personally I’m very pleased with it as it saves hours of learning. If You just for a minute place the VB-hat back on Your head;  the following code is actually written in Real Basic and it’s difficult to separate it from VB(A)-code, right?

Dim FItem as FolderItem
Dim iCounter as Integer
FItem = GetFolderItem("")
For iCounter = 1 to fitem.Count
Listbox1.AddRow str(iCounter)
Listbox1.Cell(ListBox1.LastIndex,1)=FItem.Item(iCounter).AbsolutePath
Next iCounter

Now You can take off the VB-hat again.

Real Software has a 90 days release cycle, that’s four times per year. What is notable is that no patches or service packs are released between the new versions of RS. Of course, the release cycle has its pros and cons. RS can be improved rather quickly and users know when new versions are expected. [Edit] If Real Software find a major bug then they will rather immediately release a new version of RS.

What may be a problem for RS developers is that some part(s) of RS can be deprecated between two versions without any notification. So before using newer versions RS developers must make sure that what is in use is not dropped in coming versions. This may sound as rather tough sits for developers. However, Real Software keep them in the framework up to one year or so and developers are warned when they open a solution that includes deprecated items etc. Developers are also notified about the changes in the release notes.

A side effect of the release cycle and the applied policy is that no books can actually be written as they can be outdated before they are even released. This makes it more difficult to learn to use and leverage RS, at least if we are looking for printed sources. I will later discuss the available channels for learning RS.

Because RS is a cross-platform development software, i.e. it target four platforms, Real Software must always apply the lowest denominator approach when adding new features. This is per design but Real Software also add OS-specific features. A typical case is the Windows registry.

What should also be notable is that RS does not support x64. However, it’s something that will be implemented in RS within a foreseeable future.

RS comes in several versions, but they are all targeting either the Desktop or/and the web. RS is shipped in several various versions as the list below shows:

  • Personal
  • Professional
  • Enterprise
  • Web

For more information about present versions, pricing and terms please see Compare Editions.

For a presentation of the company behind RS please see Real Software.

For an introduction to RS please see Real Studio.

Technically Support for RS
Real Software offers support for owner of Enterprise and Professional licenses. But the best resources for support are the public Q&A forums Real Software runs and they also maintenance mailing lists. RS has a strong support from the group of independed developers and many of them have been around since RS was shipped for the first time. Real Software maintenances also a wiki documentation site for RS, RealbasicWiki. There You can find updated and new technically information.

In my own experience I can say that the support given by the RS community is very good and that the community is indeed friendly. The forums are great places to start with and the knowledge is both deep and wide. The mailing lists are good but they can be more technically advanced. For learning they are great to monitor and follow.

Resources for learning RS
Because of the high pace of releasing new RS versions it’s impossible to write a book that is up to date. However, it exist some books that can be useful, especially for new users, although they are getting outdated. But the best resources can therefore be found on the web.

Real Studio Developer Magazine
Real Studio Developer Magazine is a monthly magazine with news, reports, reviews and code series. If You want to be serious about RS then a subscription of this magazine is a must. Please see RS Developer Magazine for more information.

RBLibrary.com
RBLibrary.com is an online store that offers smaller articles about specific subjects. Among the titles we found some interesting series for OOP, databases and automating MS Excel. Please see RSLibrary.com for more information.

Books
One of the books that is a must to read is REAL OOP with REALBASIC book written by Guyren G Howe and it’s available in the PDF file format. Please also check out Mark S. Choate’s book REALbasic – Cross-Platform Application Development which is a great book to learn various code techniques in RS. If available, You may also be interested in Matt Neuburg’s book Realbasic: TDG: The Definitive Guide. The last book I would like to recommend is not a book per se, it consist of blog articles all written by Aaron Ballman. The title is Ramblings on Realbasic.

Resource for Real Studio developers
For some years ago some high profiled RS consultants decided to set up an organization for professional RS developers. The organization was named Association of Realbasic Professionals (ARBP). If You intend to be a professional RS developers then I recommend You to apply for a membership of ARBP. But please take some time to navigate around on their site and get an opinion of Yourself about it.

Third Party Addons
It exist a group of vendors that provides plug-ins and visual controls etc. for RS. I decided to list those vendors I have good experience from and who offers highly professional tools, controls and classes.

  • CyphersTECH Consulting: GraffitiSuite, a collection of high quality visual controls.
  • Einhugur Softwares: A large collection of professional visual controls, including a Grid control. Also Libraries for various tasks.
  • Excel Software: RbApp, a set of visual controls including Grid control and Chart control. Offers a great number of other tools.
  • Figa Software: FGSourceList control, FGScopebar control and FGThumbnailCanvas control.
  • Jérémie Leroy: Ribbon control, Toolbar control and more.
  • LogicalValue Software: Runs the RSDevZone which offers some nice visual controls.
  • MonkeyBread Software: ChartDirector Plug-in, Complete Plug-in (largest collection of plug-ins), DynaPDF Plug-in.
  • Pariahware Inc: Elastic Window.
  • Roth Soft: RS Report Designer, a designer tool for creating reports in RS.
  • RB Garage: Owned by RB Library and is said to be the largest online resource for RS developers.
  • Thomas Tempelmann: Arbed, a collection of tools to use when developing solutions in RS.
  • True North Softwares: Formatted Text control, RB Code Reports and RTFParser

RS – The Perfect Companion
Why do I write that RS is the perfect companion for Small and Medium sized Enterprises (SMEs)? Well, I have been using RS for nearly a year and based on my experiences of SMEs I believe it is. I also compiled a list of pros and cons. I believe that the outcome speaks for itself.

Pros:

  • RS is indeed a Rapid Application Development (RAD) tool.
  • RS generates small packages and does not rely on any larger framework.
  • The learning curve for RS is not so steep compared with other languages and platforms.
  • The license fee is quite inexpensive compared with other alternatives.
  • Newer version of RS can be installed and used side-by-side with older version.
  • Many corporations have a mix of new and old computers. RS does not require having the latest most powerful computers to develop RS solutions and to run them.
  • Many corporations have a mix of various operating systems in their computers, most Windows and Mac OS X but also sometime Linux. RS is the only realistic development platform that can provide cross-platform solutions.
  • Many Mac OS X developers use it because it does not exist any attractive alternatives. In other words, corporations that rely on Mac OS X can find a greater range of consultants who knows RS.
  • Easy to deploy and it also exist professional installation softwares that can handle to install cross-platform solutions like InstallAnywhere and BitRock InstallBuilder.

Cons

  • Real Software is a small corporate meaning limited man hours to develop, to maintenance and to give support.
  • The use group is, compared with other development tools’ groups, a small group. I believe Real Software writes on their site that RS has 150,000 users.
  • The release cycle can cause problems as classes, methods, properties etc. can be dropped between two versions.
  • Per design RS works on several operating systems which can also be a disadvantage due to the requiry of using the lowest denominator approach.
  • The lack of two important controls, customers to Real Software must depend on third party unless they develop similar controls by themselves.
  • Does not yet support x64.

As for the business risk it’s always higher when working with small corporates. For me that’s by design.

Although RS is competing with other development tools I see RS more as a compliment to  .NET / Eclipse/ Embarcadero Delphi etc.

Finally, this is the first article about RS but certainly not the last; so I say welcome RS to my blog 🙂

Kind regards,
Dennis

November 8, 2011

Paradigma offer Valentina Office Server 5 connections for free!

Filed under: .NET & Excel, COM Add-ins, Excel, SQLite, Valentina DB, Valentina Office Server, VSTO & Excel — Dennis M Wallentin @ 4:43 pm

Paradigma announced today that they offer their Valentina Office Server 5 connections for free. The offer targets certain groups of users, including Small and Medium sized companies, and for certain tasks.

Not only that, under the same license conditions they also offer Valentina Studio + Valentina Report for free together with any Application Development Kit (ADK) or all ADKs.

The most interesting part of this free offer is, together with the Valentina Office Server itself, the Valentina Report tool. This makes it very attractive as report tools are usually not available for free.

For more information please see the following page at Paradigma’s site:

http://www.valentina-db.com/en/company/news/1-latest-news/257-paradigma-software-releases-free-business-ready-database-and-reports-server

Kind regards,
Dennis

Excel & Valentina Database

Filed under: .NET & Excel, COM Add-ins, Excel, SQLite, Tools, Valentina DB, Valentina Office Server, VSTO & Excel — Dennis M Wallentin @ 1:42 am

Introducing the Valentina Database

I thought I would introduce the Valentina Database (Valentina DB) with this article and discuss how we can connect to it by using ODBC, both in VBA and in VB.NET.

For the last 11-12 months I have been using Valentina DB as an embedded database in all clients’ solutions where it requires some data storage. Some of them run 24/7 and up to this date no errors have been reported related to the database in use. It has mainly been used in managed COM add-ins (Shared add-ins and VSTO add-ins) and by using the .NET driver & APIs for the database.

Using the ODBC technology is still a common way. First of all, it’s a fast way to establish a connection to a data source. It’s also a reliable technology as it has been around for the last 20 years or so.

Actually, we can say exactly the same about the Valentina DB although it has been around “only” about 10 years. In addition, it’s a really fast and compact database with a small footprint. Therefore I find it to be a great companion when I need an embedded database solution. Valentina DB can also be connected to the Valentina Office Server on both x86 and x64 platforms.

Another advantage is, compared with other databases, that it targets a great number of operating system and development platforms. So per se it’s a true cross-platform database. I will later discuss in a coming article more details about developing .NET solutions by using the Valentina DB.

On several development platforms we have access to Valentina APIs library. It’s a quite powerful library. For instance, we can use APIs instead of using SQL the traditionally way, i.e. it’s like LINQ.

Valentina DB is also a maturity software as it has been around since the late 90’s. The general price level should not scare away anyone; expect those that looking for free options.

The company behind Valentina DB, Paradigma Software, offers a good support and maintenance a public mailing list for Q&A. The only thing that I regret is that the Valentina DB Wiki documentation is not yet complete. This is rather typical for small companies; they lack the man hours to keep up the pace with both their product(s) and the documentation to them.

What Paradigma Software says themselves about Valentina DB is:

  • Performs Queries x10 to x500 Faster than Other Databases
  • Convert from Old Relational Databases and Reduce Sizes by 1/3
  • Smart SQL Means Shorter, Easier to Read SQL Queries
  • Modern Post Relational Architecture
  • Connects with All Major Environments on Windows, Linux, Mac OS X
  • Royalty Free Embedded Server Deployment

In addition to the Valentina DB product Paradigma Software also offers related tools such as the Valentina Pro Studio, which supports 18+ Valentina DB platforms. The Studio enables us to work with databases and also to connect to Valentina Office Servers. We can also use their excellent Valentina Reports to create highly customized reports both for Valentina DBs and also for SQLite DBs. Once installed Valentina Reports will be part of the Studio.

Valentina DB Homepage

Valentina DB Wiki

Connecting to a Valentina DB through VBA using ODBC

I have intentionally left out all comments of the code as it should be rather self-explanatory. What we need to do before using the licensed ODBC driver is to register it on the computer which requires us to create a user DSN or a File DSN.

In the first case I use a DSN file as the source for the connection string. It contains the following information:

[ODBC]
DRIVER=Valentina ODBC Driver
Database=C:\Users\Dennis Wallentin\Documents\Valentina DBs\FirstDB.vdb
IsDatabaseLocal=yes

In the second case I create the connection string in code. In both cases I use the Microsoft ADO library in order to work with the database and with its data.

'Make sure You set a reference to
'Microsoft ActiveX Data Objects Library x.x

Option Explicit

Sub Retrieve_Data_ValentinaDB_ADO_FILEDSN()

Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset

Dim wsTarget As Worksheet

Dim stSQL As String

Dim lnCountFields As Long

Set adoConnection = New ADODB.Connection
Set adoRecordset = New ADODB.Recordset

Set wsTarget = ThisWorkbook.Worksheets("Data")

stSQL = "SELECT * FROM Person"

adoConnection.Open ConnectionString:= _
"FileDSN=C:\Users\Dennis Wallentin\Documents\Valentina.dsn"

adoRecordset.Open Source:=stSQL, ActiveConnection:= _
"FileDSN=C:\Users\Dennis Wallentin\Documents\Valentina.dsn"

lnCountFields = adoRecordset.Fields.Count

Application.ScreenUpdating = False

For lnCountFields = 0 To lnCountFields - 1
wsTarget.Cells(1, lnCountFields + 1).Value = _
adoRecordset.Fields(lnCountFields).Name
Next

wsTarget.Cells(2, 1).CopyFromRecordset adoRecordset

adoRecordset.Close
adoConnection.Close

Set adoRecordset = Nothing
Set adoConnection = Nothing

End Sub

 

Sub Retrieve_Data_ValentinaDB_ADO_ODBC_Driver()

Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset

Dim wsTarget As Worksheet

Dim stSQL As String
Dim stConnection As String
Dim stDataBase As String

Dim lnCountFields As Long

Set adoConnection = New ADODB.Connection
Set adoRecordset = New ADODB.Recordset

Set wsTarget = ThisWorkbook.Worksheets("Data")

stDataBase = "C:\Users\Dennis Wallentin\Documents\Valentina DBs\FirstDB.vdb"
stConnection = "Driver={Valentina ODBC Driver};IsDatabaseLocal=yes;Database=" & _
stDataBase
stSQL = "SELECT * FROM Person"

adoConnection.Open ConnectionString:=stConnection

adoRecordset.Open Source:=stSQL, ActiveConnection:=stConnection

lnCountFields = adoRecordset.Fields.Count

Application.ScreenUpdating = False

For lnCountFields = 0 To lnCountFields - 1
wsTarget.Cells(1, lnCountFields + 1).Value = _
adoRecordset.Fields(lnCountFields).Name
Next

wsTarget.Cells(2, 1).CopyFromRecordset adoRecordset

adoRecordset.Close
adoConnection.Close

Set adoRecordset = Nothing
Set adoConnection = Nothing

End Sub

Connecting to a Valentina DB through VB.NET using ODBC

The ODBC driver can also be used in VB.NET but it exist a specific version of Valentina that explicit target the .NET platform. In general I recommend using it.

Although the following case shows how we can populate a worksheet with data based on the ODBC datareader I recommend to use classic ADO instead, like above, as we can dump all the data once via the method CopyFromRecordset.

Imports System
Imports System.Data
Imports System.Data.Odbc
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click

'Snabbast är att använda classic ADO.

Dim odbcCon As OdbcConnection = Nothing
Dim odbcCom As OdbcCommand = Nothing
Dim odbcDataR As OdbcDataReader = Nothing

Dim stConnection As String = String.Empty
Dim stDatabase As String = "C:\Users\Dennis Wallentin\Documents\Valentina DBs\FirstDB.vdb;"
Dim stSQL As String = "SELECT * FROM Person"

stConnection = "Driver={Valentina ODBC Driver};IsDatabaseLocal=yes;Database=" + stDatabase

Dim stSoureFile As String = "C:\Users\Dennis Wallentin\Documents\Data\SalesData.xlsm"

Try
odbcCon = New OdbcConnection(stConnection)
odbcCom = New OdbcCommand(stSQL, odbcCon)
odbcCom.CommandType = CommandType.Text
odbcCon.Open()
odbcDataR = odbcCom.ExecuteReader(CommandBehavior.CloseConnection)

'In order to dimension the array for field names the number of columns need to be retrieved.
Dim intColumnsCounter As Int32 = odbcDataR.FieldCount - 1

Dim FieldsArr(0, intColumnsCounter) As String

Dim intRowsCounter As Int32 = 0

'The DataReader is a forward-only and read-only recordset. Therefore we cannot
'know in advance the number of records it will return. The only possible solution
'is to estimate the max number of records and use it in the solution.
Dim intMaxRecords As Int32 = 10000

Dim DataArr(intMaxRecords, intColumnsCounter) As Object

'Populate the array of field names by using the DataReader's method 'GetName'.
For intNameCounter As Int32 = 0 To intColumnsCounter
FieldsArr(0, intNameCounter) = odbcDataR.GetName(intNameCounter)
Next

'Populate the array of records by reading all the records in the DataReader.
While odbcDataR.Read
For intRecordCounter As Int32 = 0 To intColumnsCounter
DataArr(intRowsCounter, intRecordCounter) = odbcDataR.Item(intRecordCounter)
Next
intRowsCounter = intRowsCounter + 1
End While

'Close both the connection and the DataReader.
odbcCon.Close()
odbcDataR.Close()

Dim xlApp As Excel.Application = New Excel.Application
Dim wbBook As Excel.Workbook = xlApp.Workbooks.Open(stSoureFile)
Dim wsSheet As Excel.Worksheet = TryCast(wbBook.Worksheets("Data"), Excel.Worksheet)

Dim xlCalc As Excel.XlCalculation = xlApp.Calculation
xlApp.Calculation = Excel.XlCalculation.xlCalculationManual

With wsSheet
.Range(.Cells(1, 1), .Cells(1, intColumnsCounter + 1)).Value = FieldsArr
.Range(.Cells(2, 1), .Cells(intRowsCounter + 2, intColumnsCounter + 1)).Value = DataArr
.UsedRange.Columns.AutoFit()
End With

With xlApp
.Visible = True
.UserControl = True
'Restore the calculation mode.
.Calculation = xlCalc
End With

Catch ex As Exception

MessageBox.Show(ex.ToString())

End Try

'Prepare variables for the Garbage Collector.
'Don't forget to do a proper release of alla COM objects.

odbcCom.Dispose()
odbcCon.Dispose()

odbcDataR = Nothing
odbcCom = Nothing
odbcCon = Nothing

End Sub

End Class

Valentina DB offers so much that I sometimes feels that only the sky is the limit. I learn every day something new about the database and its features and the more I learn the more impressed I am.

Enjoy!

Kind regards,
Dennis

« Newer Posts

Create a free website or blog at WordPress.com.