Attribute VB_Name = "Context"
Option Explicit

Private Declare Function GetComputerNameAPI Lib "kernel32" Alias "GetComputerNameA" _
 (ByVal lpBuffer As String, nSize As Long) As Long
 
 
Public Sub CtxSetAbort()
    GetObjectContext.SetAbort
End Sub

Public Sub CtxSetComplete()
    GetObjectContext.SetComplete
End Sub

Public Function CtxCreateObject(ByVal sProgID As String) As Object
    Set CtxCreateObject = CreateObject(sProgID)
End Function

Public Sub CtxRaiseError(module As String, functionName As String)
    ' Save the error information before calling CtxSetAbort in case it has side effects
    Dim lErr As Long
    Dim sErr As String
    lErr = VBA.Err.Number
    sErr = VBA.Err.Description
    
    CtxSetAbort
    
    Err.Raise lErr, SetErrSource(module, functionName), sErr
End Sub

Public Sub RaiseError(module As String, functionName As String)
    Err.Raise Err.Number, SetErrSource(module, functionName), Err.Description
End Sub


Function GetComputerName() As String
    ' Set or retrieve the name of the computer.
    Dim strBuffer As String
    Dim lngLen As Long
        
    strBuffer = Space(255 + 1)
    lngLen = Len(strBuffer)
    If CBool(GetComputerNameAPI(strBuffer, lngLen)) Then
        GetComputerName = Left$(strBuffer, lngLen)
    Else
        GetComputerName = ""
    End If
End Function


Function GetSource(ByVal pstrSource As String, ByVal pstrProc As String, ByVal pstrMod As String) As String
    Dim strFront As String
    Dim strBack As String
  
    strBack = pstrProc & "@" & GetComputerName()
  
    If Left$(pstrMod, (InStr(1, pstrMod, ":") - 1)) = pstrSource Then
        strFront = pstrSource & Mid$(pstrMod, InStr(1, pstrMod, ":"))
    Else
        strFront = "|" & pstrMod
    End If
  
    GetSource = strFront & strBack
End Function

Function SetErrSource(modName As String, procName As String) As String
    ' Returns an error message like:  "[FMStocks_DB.Account] VerifyUser [on AHI version 5.21.176]"
    
    SetErrSource = Err.Source & "<br>" & "[" & modName & "]  " & procName & _
            " [on " & GetComputerName() & " version " & GetVersionNumber() & "]"
End Function


Function GetVersionNumber() As String
    GetVersionNumber = App.Major & "." & App.Minor & "." & App.Revision
End Function
'function takes a string with an '=' in it and returns the left part
'GetKey("test=1") return test
Public Function GetKey(ByVal strItem As String) As String
    GetKey = ""
    If InStr(1, strItem, "=", vbTextCompare) > 0 Then
        GetKey = Left(strItem, InStr(1, strItem, "=", vbTextCompare) - 1)
    End If
End Function
'function takes a string with an '=' in it and returns the right part
'GetValue("test=1") return 1
Public Function GetValue(ByVal strItem As String) As String
    GetValue = ""
    If InStr(1, strItem, "=", vbTextCompare) > 0 Then
        GetValue = Right(strItem, Len(strItem) - InStr(1, strItem, "=", vbTextCompare))
    End If
End Function
