VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Login"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public Event ServerUnknown()
Public Event ConnectionFailed(ByVal DB As String, ByVal UID As String, ByVal Password As String, ByVal Server As String, ByVal Port As String)
Public Event Connected()
Private CN          As ADODB.Connection
Private ConnStr     As String

Public Function MyDBConnection() As ADODB.Connection

            
    Dim sServer     As Collection
    Set sServer = New Collection

    Set sServer = ServerDetails

    If sServer Is Nothing Then
        RaiseEvent ServerUnknown
    
    Else
        If Not OpenConnection(sServer("Key")(0), sServer("Key")(1), sServer("Key")(2), sServer("Key")(3), sServer("Key")(4)) Then
            RaiseEvent ConnectionFailed(sServer("Key")(0), sServer("Key")(1), sServer("Key")(2), sServer("Key")(3), sServer("Key")(4))
            
        Else
        
            Set MyDBConnection = CN
            RaiseEvent Connected
        
        End If
    End If
    
    

End Function

Private Function ServerDetails() As Collection

    On Error GoTo BailOut
    
    Dim fNum        As Integer
    Dim bIsOpen     As Boolean
    Dim sDetails()  As String
    Dim sLine       As String
    Dim sServer     As Collection
    Set sServer = New Collection
    
    fNum = FreeFile
    
    Open App.Path & "\db.txt" For Input As #fNum
    bIsOpen = True
    While Not EOF(1)
        Line Input #fNum, sLine
    Wend
    Close (fNum)
    sDetails = Split(sLine, "|")
    sServer.Add Array(sDetails(0), sDetails(1), sDetails(2), sDetails(3), sDetails(4)), "Key"
    db_server.Database = sDetails(0)
    db_server.User = sDetails(1)
    db_server.Pwd = sDetails(2)
    db_server.Host = sDetails(3)
    db_server.Port = sDetails(4)
    Set ServerDetails = sServer
    If Not sServer Is Nothing Then Set sServer = Nothing
    
ExitSub:

    Exit Function

BailOut:

    If bIsOpen Then Close (fNum)
    If Err Then Err.Clear
    GoTo ExitSub

End Function

Public Property Get ConnectionString() As String
    ConnectionString = ConnStr
End Property

Private Function OpenConnection(Optional ByVal DB As String = "bjmp", Optional ByVal UID As String = "root", Optional ByVal Password As String = "ok", Optional ByVal Server As String = "localhost", Optional ByVal Port As String = "3306") As Boolean
On Error GoTo BailOut

Set CN = New ADODB.Connection
If CN.State = adStateOpen Then CN.Close

ConnStr = "Provider=MSDASQL.1;Driver=MySQL ODBC 5.1 Driver;SERVER=" & Server & ";UID=" & UID & ";PWD=" & Password & ";DATABASE=" & DB & ";PORT=" & Port & ";OPTION=164287"
CN.CursorLocation = adUseClient
CN.Open ConnStr
OpenConnection = True

Exit Function

BailOut:
    OpenConnection = False
    Err.Clear

End Function
