Modifying the SetADOConnection Procedure

If we have an error getting the Connection object, we will want to write this to a text file as it is a critical error. We cannot write to the database if there is something wrong with our connection to the database. If we had the e-mail or beeper function, it is likely we would use it in the error handler of this function.

Private Sub SetADOConnection(ByVal v_strUserID As String, _ ByVal v_strPassword As String, _

Optional ByVal v_sConnectionString As String = "Empty") On Error GoTo SetADOConnectionError

Set m_objADOConnection = CreateInstance("ADODB.Connection")

With m_objADOConnection

.CursorLocation = adUseServer If v_sConnectionString = "Empty" Then

.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _

Persist Security Info=False;Data Source=" & m_cstrDatabasePath

Else

.ConnectionString = v_sConnectionString End If

.Open End With

Continued on following page.

Exit Sub

SetADOConnectionError:

Dim lngErrorCounter As Long Dim strErrors As String strErrors = Err.Number & ": " & Err.Description

If m_objADOConnection.Errors.Count > 0 Then

For lngErrorCounter = 0 To m_objADOConnection.Errors.Count - 1 strErrors = strErrors & _

m_objADOConnection.Errors(lngErrorCounter).Number & _ ": " & m_objADOConnection.Errors(lngErrorCounter).Description & vbCrLf Next lngErrorCounter

End If

WriteToTextFile

App.EXEName, "LogFile",

"SetADOConnection",

"Connection

Failed", Informational

End Sub

Now, go to frmTest and add another command button called cmdTest2. In the click event for cmdTest2 add the following code:

Private Sub CmdTest2_Click() Dim oLog As LogFile Dim sReturnValue As String Set oLog = New LogFile oLog.WriteToEventLog "Test", "FrmMain", "FormLoad", _

"This is a test", Informational oLog.WriteToEventLog "Test", "FrmMain", "FormLoad", _

"This is a test", Error oLog.WriteToEventLog "Test", "FrmMain", "FormLoad", _

"This is a test", Warning oLog.WriteToTextFile "Test", "FrmMain", "FormLoad", _

"This is a test", Informational sReturnValue = oLog.WriteToDatabase("Test", "FrmMain", _

"FormLoad", "This is a test", Informational) If sReturnValue <> "" Then oLog.WriteToTextFile "Test", "FrmMain", "FormLoad", _ sReturnValue, Informational

End If End Sub

You will notice something rather interesting about this code. Our MTS object is created and held open until the end of the sub. This means we are holding our server component for a long time, and also maintaining state across many calls. This will not scale, and is not how we should build our components. A better way to code this would be as follows:

Private Sub CmdTest2_Click()

Dim oLog As LogFile

Dim sReturnValue As String

Set oLog = New LogFile

oLog.WriteToEventLog "Test", "FrmMain",

"FormLoad",

"This is a test", Informational

Set oLog = Nothing

Set oLog = New LogFile

oLog.WriteToEventLog "Test", "FrmMain",

"FormLoad",

"This is a test", Error

Set oLog = Nothing

Set oLog = New LogFile

oLog.WriteToEventLog "Test", "FrmMain",

"FormLoad",

"This is a test", Warning

Set oLog = Nothing

Set oLog = New LogFile

oLog.WriteToTextFile "Test", "FrmMain",

"FormLoad",

"This is a test", Informational

Set oLog = Nothing

Set oLog = New LogFile

sReturnValue = oLog.WriteToDatabase("Test", "FrmMain",

"FormLoad",

"This is a test", Informational)

If sReturnValue <> "" Then

oLog.WriteToTextFile "Test", "FrmMain", "FormLoad",

sReturnValue, Informational

End If

Set oLog = Nothing

End Sub

This is still not too readable, though. It would be best to make a log object for each situation: oLogText, oLogDatabase etc. If you are concerned about the work to create the MTS component, MTS will not completely destroy the object when you set it to nothing. It will keep a copy around for the next call, so you are not losing any performance by doing this.

Before you can run this, you will need to remove your component from the MTS package:

Next recompile the DLL and then add the DLL back into MTS. You will not need to make any changes in prjTest as you are adding to the interface and you set binary compatibility (which means that the component will recompile with the same GUID).

As a final comment, you may find that the event log will not work when you debug your component. But, when you compile the component, it will work fine. Once again, things do not always work the same in the debugger as they do in the compiled component.

Was this article helpful?

0 0
365 Days Of Motivation

365 Days Of Motivation

Stop Wasting Time And Learn How To Stay Motivated. Finally! Discover How To Stop Your Mind From Wandering, And Upgrade Your Motivation. You Can Hack Your Motivation Levels, Allowing You To Take Your Life To The Next Level.

Get My Free Ebook


Post a comment