Sunday, June 28, 2015

Excel VBA Copy the contents of a recordset to a range

To copy the results of query, you first have to execute a query - using either ADO or DAO (for this example, ADO will be used). Then, you use the CopyRecordset method of the range object

Excel VBA

Dim objConn as ADODB.Connection
Dim objCmd as ADODB.Command


Set objConn = Application.CurrentProject.Connection
Set objCmd = New ADODB.Command
With objCmd
    Set .ActiveConnection = objConn
    .CommandType = adCmdStoredProc
    .CommandText = "qryBrazilianRevenueExtract"
    Set rs = .Execute
End With


Set objRange=Worksheets(1).Cells(2,1).CurrentRegion
objRange.Cells(1, 1).CopyFromRecordset rs

Wednesday, June 17, 2015

C# VBA Side by Side using Enumerations

An enumeration is a set of named constants whose values are generally related.  In Excel VBA, an enumeration statement (Enum) is made in the Declarations section of a standard or public class module. In C#, an enumeration is defined directly within a namespace.  By default, the first enumerator is assigned the value of 0 and the value of each successive enumerator is increased by 1.

Excel VBA

Tip: IF the enum description, contains blanks or special characters, then surround the enum name with brackets ([ ]).


Public Enum OfficeProduct
    Access2007Only = 1
    [MSAccess95-2003]
    Excel2007Only
    [MSExcel95-2003]
    [XML]
End Enum

C#


public enum OfficeProduct {
       Access2007Only=1,
       MSAccess95_2003=2,
       Excel2007Only=3,
       MSExcel95_2003=4,
       XML=5};