Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

User-Maintainable Connection Strings

Latest post Thu, Jan 17 2008 6:05 PM by XL-Dennis. 3 replies.
  • Thu, Jan 17 2008 8:32 AM

    • Rob Bruce
    • Top 50 Contributor
    • Joined on Wed, Jan 9 2008
    • Llandrindod, Cymru/Wales
    • Posts 14
    • Points 167

    User-Maintainable Connection Strings

    I recently worked on a project where it was unclear ultimately where the data was going to sit. I started working against an Access database but was aware thet at some point there would be an upgrade to SQLExpress or maybe a different, but equally cheap, RDMS. I was assured that the database schema would remain intact through the transition (though I was sceptical) so I concluded that I needed some way for my app to make a smooth change from one database to another.

    I considered various strategies, but the one I finally settled on was to include the setting of the database connection string in my application's Options dialog.

    Here's how I did it. If you want to try this in an Excel workbook, just add a new form, add a button (retain it's name), set a reference to the Microsoft OLE DB Service Component 1.0 Type Library noted in the commentary, run the form using F8 and click the button.

    Option Explicit

    Private Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

    Private Sub CommandButton1_Click()
    Dim strRet As String
    strRet = GetConnString
    If Len(strRet) > 0 Then MsgBox strRet, vbInformation
    End Sub

    Private Function GetConnString() As String
    '----------------------------------------------------------------------
    ' Procedure : GetConnString
    ' DateTime : 21-Jun-2007 21:14
    ' Author : Rob Bruce
    ' Purpose : Gets a connection string from the user by showing the
    ' : standard ADO Data Link Wizard. Empty string if the user
    ' : cancels.
    ' Depends : Requires a reference to the Microsoft OLE DB Service
    ' : Component 1.0 Type Library (oledb32.dll).
    ' : Calls GetMSFormHwnd.
    '----------------------------------------------------------------------
    '
    Dim objDataLinks As New MSDASC.DataLinks
    Dim lngHWNDParent As Long

    ' Make the Wizard modal (if possible)...
    lngHWNDParent = GetMSFormHwnd(Me.Caption)
    If lngHWNDParent > 0 Then objDataLinks.Hwnd = lngHWNDParent

    ' The Data Link Wizard raises an error if the user cancels...
    On Error Resume Next

    'Display the dialog
    GetConnString = objDataLinks.PromptNew

    End Function

    ' Utility functions...

    Private Function GetMSFormHwnd(Caption As String)
    '----------------------------------------------------------------------
    ' Procedure : GetMSFormHwnd
    ' DateTime : Back in the mists of time
    ' Author : Rob Bruce
    ' Purpose : Gets the window handle of an MSForms form. Sadly
    ' : these forms don't have a simple hWnd property.
    ' Depends : Requires FindWindow API declare.
    ' : Calls GetMSFormClassName.
    '----------------------------------------------------------------------
    Dim lngRet As Long

    On Error GoTo GetMSFormHwnd_Error

    lngRet = FindWindow(GetMSFormClassName, Caption)

    GetMSFormHwnd = lngRet

    Exit Function

    GetMSFormHwnd_Error:
    Err.Clear
    GetMSFormHwnd = 0

    End Function

    Private Function GetMSFormClassName() As String
    '----------------------------------------------------------------------
    ' Procedure : GetMSFormClassName
    ' DateTime : Back in the mists of time
    ' Author : Rob Bruce
    ' Purpose : The MSForms windows class name changed for version 2
    ' : (Office 2000), so this checks the version of Excel
    ' : in order to return the correct class name.
    ' Depends : Application object is assumed to be Excel.
    '----------------------------------------------------------------------
    On Error GoTo GetMSFormClassName_Error

    If CLng(Val(Application.Version)) > 8 Then
    ' Excel 2000 onwards
    GetMSFormClassName = "ThunderDFrame"
    Else
    ' Excel 97 only
    GetMSFormClassName = "ThunderXFrame"
    End If

    Exit Function

    GetMSFormClassName_Error:
    With Err
    .Raise .Number, .Source & "[GetMSFormClassName]", .Description
    End With
    End Function

    As you can see, there's a stupid amount of playing around with the form just to get the Data Link Wizard to become modal. What price an hWnd property for MSForms?

    Good Luck!

    Rob ============================================ Siaradwch yn araf, os gwelwch'n dda - dim ond dysgwyr ydw i!

    • Post Points: 21
  • Thu, Jan 17 2008 9:43 AM In reply to

    • XL-Dennis
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Östersund, Sweden
    • Posts 38
    • Points 650

    Re: User-Maintainable Connection Strings

    Rob,

    I have until now never considered to make the Wizard modal Smile


    ...there's a stupid amount of playing around with the form just to get the Data Link Wizard to become modal.

    From my point of view it's not that much more code it requires. Wink Forms in managed / unmanaged COM Add-ins that we want to be part of Excel requires some code too...

    BTW, my latest free add-in (.NET Co Library) makes it possible to both create and store connection strings both with the Data Link Wizard as well as with .the NET Wizard. The Data Link Wizard seems to (in my experience) not be able to create connection strings to SQL Server 2005 (and 2008) which the .NET Wizard can do.

    Speaking about modeless, have You been able to make Windows Forms (in managed COM Add-ins) integrated with the VBE in Excel?

     

    With kind regards,
    Dennis W
    --------------------------------------------------------------
    My English Excel site My VSTO & .NET & Excel blog

    • Post Points: 21
  • Thu, Jan 17 2008 5:33 PM In reply to

    • Rob Bruce
    • Top 50 Contributor
    • Joined on Wed, Jan 9 2008
    • Llandrindod, Cymru/Wales
    • Posts 14
    • Points 167

    Re: User-Maintainable Connection Strings

    XL-Dennis:
    The Data Link Wizard seems to (in my experience) not be able to create connection strings to SQL Server 2005 (and 2008) which the .NET Wizard can do.

    I moved out of big corporate development just before the release of SQLServer 2005, so I've never met a full implementation of that in the wild. However, I don't have a problem using the Data Link Wizard with the Express edition (which is the database I currently recommend to clients). You just need to remember that, unlike SQL Server 2000, it defaults to being installed as a named instance. So, whereas with 2000 you would likely connect to [machine name] as the server, with 2005 you'll more likely connect to [machine name]\[instance name] - for example localhost\sqlexpress or DUALCORE_3\SQLEXPRESS.

    XL-Dennis:
    Speaking about modeless, have You been able to make Windows Forms (in managed COM Add-ins) integrated with the VBE in Excel?
     

    I still prefer to use VB6 for COM work. The way I look at it is that VB(classic) is COM, so it's the appropriate tool. Yes, I'd really, really like to work with the power of a .NET language and with the flexibility of the VS2003/2005/2008 IDEs, but interop and all of the other jumping through hoops just makes it inappropriate. I'm sure it would be nice to take my daughter to school in a chauffeur-driven limo every day, but it's close enough for us to walk and to sing and sometimes to have fun getting rained on, so, on balance, a big car and driver would just be a waste.

    Best Regards

     

    Rob ============================================ Siaradwch yn araf, os gwelwch'n dda - dim ond dysgwyr ydw i!

    • Post Points: 21
  • Thu, Jan 17 2008 6:05 PM In reply to

    • XL-Dennis
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Östersund, Sweden
    • Posts 38
    • Points 650

    Re: User-Maintainable Connection Strings

    Rob,


    ...but interop and all of the other jumping through hoops just makes it inappropriate.

    In my experience .NET & VSTO solutions works well in controled enviroments, i e all clients are configured identical. From a personal point of view, I do .NET and I cannot see that I will switch back. But I need to keep a copy of classic VB to assure that I can update some customized unmanaged COM add-ins.


    So, whereas with 2000 you would likely connect to [machine name] as the server, with 2005 you'll more likely connect to [machine name]\[instance name] - for example localhost\sqlexpress or DUALCORE_3\SQLEXPRESS

    It's true but this is what I mean is a shortcoming of the Data Link Wizard as we need to have that info before creating a workable connection string. With .NET's wizard we don't.


    ... a big car and driver ...

    Don't You agree that it can be rather nice especially when the temperature is around +/- 0 Celcius, it's snowing and the wind is strong.

    With kind regards,
    Dennis W
    --------------------------------------------------------------
    My English Excel site My VSTO & .NET & Excel blog

    • Post Points: 5
Page 1 of 1 (4 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.