When getting an unhelpful error deep in the bowels of some module developed in-house, it’s often desirable to be able to insert some diagnostic information. There are a number of ways you can achieve this, for example, writing to a file, but one option is to (temporarily, at least) embed code that writes out to the event log.
In attempting to debug a bit of VB6 code, I found this invaluable. So here are some articles:
.NET http://support.microsoft.com/kb/301279
Pre- .NET http://support.microsoft.com/kb/154576
For VB5 and above, you can simply instantiate an object from the Logger class, and call LogEvent. Pre VB5, you’ll need the following class:
Option Explicit
Private Declare Function RegisterEventSource Lib "advapi32" Alias _
"RegisterEventSourceA" (ByVal lpUNCServerName As String, _
ByVal lpSourceName As String) As Long
Private Declare Function DeregisterEventSource Lib "advapi32" _
(ByVal hEventLog As Long) As Long
Private Declare Function ReportEvent Lib "advapi32" Alias _
"ReportEventW" (ByVal hEventLog&, ByVal wType%, ByVal wCategory%, _
ByVal dwEventID&, ByVal lpUserSid As Any, ByVal wNumStrings%, _
ByVal dwDataSize&, plpStrings As Any, lpRawData As Any) As Boolean
Private Declare Function RegOpenKey Lib "advapi32" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult&) As Long
Private Declare Function RegCloseKey& Lib "advapi32" (ByVal hKey&)
Private Declare Function RegCreateKey Lib "advapi32" Alias _
"RegCreateKeyA" (ByVal hKey&, ByVal lpSubKey$, phkResult&) As Long
Private Declare Function RegSetValueEx Lib "advapi32" Alias _
"RegSetValueExA" (ByVal hKey&, ByVal lpValueName$, ByVal Reserved&, _
ByVal dwType&, lpData As Any, ByVal cbData&) As Long
Private Const HKLM& = &H80000002
Public Enum LogTypes
EVENTLOG_ERROR = 1
EVENTLOG_WARNING = 2
EVENTLOG_INFORMATION = 4
End Enum
Public Sub LogEvent(ByVal EvtString As String, LogType As LogTypes)
Dim hEventLog As Long, RegKey As String, EvtDll As String, hK&
Const EvPath$ = "System\CurrentControlSet\Services\Eventlog\Application\"
hEventLog = RegisterEventSource(vbNullString, App.Title)
If hEventLog = 0 Then Exit Sub
RegKey = EvPath & App.Title
EvtDll = "%SystemRoot%\System32\msvbvm60.dll"
If RegOpenKey(HKLM, RegKey, hK) Then 'if not yet existent
If RegCreateKey(HKLM, RegKey, hK) Then Exit Sub
RegSetValueEx hK, "EventMessageFile", 0, 1, ByVal EvtDll, Len(EvtDll)
RegSetValueEx hK, "TypesSupported", 0, 4, 4&, 4
RegCloseKey hK
Else
RegCloseKey hK
End If
EvtString = Split(",Error:,Warning:,,Information:", ",")(LogType) _
& vbCrLf & vbCrLf & EvtString
ReportEvent hEventLog, LogType, 0, 1, 0&, 1, 0, StrPtr(EvtString), 0&
DeregisterEventSource hEventLog
End Sub
A bit legacy, but I found it useful.

