The cExcelNwind class will do the work of placing the data on the
worksheet that is passed into the ActiveX component. Listings 9-2 and 9-3 show the VB 6 code.
Listing 9-2. cData Class from the ActiveX Component
Option Explicit
Const m_sDBPathName As String = "C:\Book\Files\Northwind 2007.accdb"
Private m_oCnn As ADODB.Connection
Private m_oRS As ADODB.Recordset
'
Public Function GetData(Which As String) As ADODB.Recordset
m_oCnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & ??
"Data Source=" & m_sDBPathName & ";"
Set m_oRS = New ADODB.Recordset
m_oRS.Open Which, m_oCnn
Set GetData = m_oRS
End Function
Private Sub Class_Initialize()
Set m_oCnn = New ADODB.Connection
Set m_oRS = New ADODB.Recordset
End Sub
Private Sub Class_Terminate()
Set m_oCnn = Nothing
Set m_oRS = Nothing
End Sub
CHAPTER 9 n ACTIVEX AND .NET 318
Listing 9-3. cExcelNwind Class from the ActiveX Component
Option Explicit
Public Sub PlaceData(TheWorksheet As Excel.Worksheet, WhichData As String)
Dim oData As cData
Dim xl As Excel.Application
Dim rs As ADODB.Recordset
Dim iFieldCount As Integer
Dim i As Integer
Set xl = TheWorksheet.Application 'hook into the current Excel session
TheWorksheet.Activate
TheWorksheet.Range("A1").Activate
xl.Selection.CurrentRegion.Select
xl.Selection.ClearContents
TheWorksheet.Range("A1").Select
Set oData = New cData
Set rs = oData.GetData(WhichData)
iFieldCount = rs.Fields.Count
For i = 1 To iFieldCount
TheWorksheet.Cells(1, i).Value = rs.
Pages:
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297