Wednesday, November 14, 2007

Ado Connecting Excel to Microsoft Access

'Code for the basic structure for getting and ado recordset in excel on an Access Database
Option Explicit
Public Const sql_Connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDatabase.mdb;" 'Persist Security Info=False;"
Private AdoRs As ADODB.Recordset
Private AdoCnn As ADODB.Connection
Private sSql As String

Public Sub LoadTotalsAndAverages()

Dim sSql As String

sSql = "SELECT sum(MetricValue) as Total From Snapshot "


Set AdoCnn = New ADODB.Connection
AdoCnn.Open sql_Connection
AdoCnn.CommandTimeout = 120

debug.print GetValue(sSql)


AdoCnn.Close
End Sub

Private Function GetValue(sSql) As Double

Set AdoRs = New ADODB.Recordset
AdoRs.Open sSql, AdoCnn, adOpenStatic, adLockReadOnly

If AdoRs.EOF Then
GetValue = 0
ElseIf IsNull(AdoRs(0)) Then
GetValue = 0
Else
GetValue = CDbl(AdoRs(0))
End If

AdoRs.Close

End Function

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home