Results 1 to 3 of 3

Thread: ADO Connections Dialog API

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Leicestershire; ENGLAND
    Posts
    71
    I once had some source code that allowed me to display the same dialog as used within the VB environment for configuring data links to configure connections within an application. Obviously, being able to display a standardised dialog to the users for connecting to a database via ADO/OLEDB is preferable. does anybody else out there have this code available ?
    I believe that it was found in a book, I have tried searching the MSKB, but no help there ...

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Leicestershire; ENGLAND
    Posts
    71

    Angry

    Surely I am not the onl;y person to have ever seen this code, am I ???

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Leicestershire; ENGLAND
    Posts
    71
    Sorry Peeps:
    This was not an API question after all. I have now worked out how to do what I want and for anyone else out there wanting to do something similar here is the *simple* code (All in VB!)

    Create an ActiveX DLL project, and reference
    Microsoft ActiveX Data Objects 2.x Library
    Microsoft OLE DB Service Component 1.0 Type Library

    now paste in the following code to a class module (this one is called dbConn)

    Option Explicit

    Private Enum ERR_CODES
    ERR_NOCONN = vbObjectError + 1
    ERR_OPEN
    ERR_CLOSED
    ERR_CONNECTED
    ERR_UNKNOWN
    End Enum

    Private cnn As ADODB.Connection
    Private m_ConnectionString As String

    Public Property Let ConnectionString(s As String)

    If cnn Is Nothing Then
    m_ConnectionString = s
    Else
    Err.Raise ERR_CONNECTED, "dbConn:ConnectionString()", "An attempt was made to change the cnnection string whilst the connection was open."
    End If

    End Property

    Public Property Get ConnectionString() As String

    ConnectionString = m_ConnectionString

    End Property

    Public Function GetConnection() As ADODB.Connection

    Dim objDlg As MSDASC.DataLinks

    On Error GoTo GetConnection_Error
    If (cnn Is Nothing) Then

    If Trim(m_ConnectionString) = "" Then
    Set objDlg = New MSDASC.DataLinks
    Set cnn = objDlg.PromptNew
    Set objDlg = Nothing
    Else
    Set cnn = New ADODB.Connection
    cnn.ConnectionString = m_ConnectionString
    End If

    With cnn
    If .ConnectionString <> "" Then
    On Error GoTo GetConnection_Open_Error
    .Open
    On Error GoTo GetConnection_Error
    Else
    Err.Raise ERR_NOCONN, "dbConn:GetConnection()", "An attempt was made to open a connection without a connection string"
    End If
    End With

    DoEvents

    End If

    Set GetConnection = cnn
    Exit Function

    GetConnection_Error:
    Err.Raise ERR_UNKNOWN, "dbConn:GetConnection()", "Unknown Error has occurred"
    Exit Function

    GetConnection_Open_Error:
    Err.Raise ERR_OPEN, "dbConn:GetConnection()", "An error occurred whilst attempting to open the connection"
    Exit Function

    End Function

    Public Sub CloseConnection()

    If cnn Is Nothing Then
    Err.Raise ERR_CLOSED, "dbConn:CloseConnection()", "Attempted to close unopened connection."
    Else
    cnn.Close
    Set cnn = Nothing
    End If

    End Sub

    If you then create an instance of dbConn in your application, you can connect to a pre-defined database by setting the .ConnectionString property and then assigning the return of .GetConnection() to the .ActiveConnection of an ADODB.Command or ADODB.RecordSet etc., or alternatively, do the same without setting the .ConnectionString property and the Data Links dialog will be displayed to build the cnnection string for you. You can later save the connection string if you wish to prevent users having to go through this process every time.

    Hey Presto, the users can now select the dtabase to connect to at run time.

    Anybody think of any improvements, please let me know ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width