'Connection string utilizing a DSN-less connection
Public Function GetConnectString() As String
GetConnectString = "Driver={SQL Server};Server=MyServer;Database=MyDatabase;Uid=MyUser;Pwd=MyPassword"
End Function
'Use the given SQL statement to return a recordset
Public Function GetRecordset(ByVal sql As String) As ADODB.Recordset
On Error GoTo Error_Handler
Dim myConn As ADODB.Connection
Set myConn = New ADODB.Connection
myConn.Open GetConnectString
Dim rsTemp As ADODB.Recordset
Set rsTemp = New ADODB.Recordset
Set GetRecordset = rsTemp.Open sql, m_oConn, adOpenStatic, adLockReadOnly
Clean_Up:
rsTemp.ActiveConnection = Nothing
myConn.Close
Set rsTemp = Nothing
Exit Function
Error_Handler:
MsgBox "Error Number: " & Err.number & vbCrLf & Err.Description
GoTo Clean_Up
End Function
'Execute an INSERT/UPDATE/ETC statement in a transaction
Public Sub RunSQL(ByVal sql As String)
On Error GoTo Error_Handler:
Dim myConn As ADODB.Connection
Set myConn = New ADODB.Connection
myConn.Open GetConnectString
myConn.BeginTrans
myConn.Execute sql
Clean_Up:
myConn.CommitTrans
myConn.Close
Set myConn = Nothing
Exit Sub
Error_Handler:
MsgBox "Error Number: " & Err.number & vbCrLf & Err.Description
myConn.RollbackTrans
GoTo Clean_Up
End Sub
|