Results 1 to 2 of 2

Thread: Open Connection

  1. #1
    Join Date
    Jul 2006

    Unanswered: Open Connection

    Hi: I try to open a connection in a function but it gives syntax error. Needs Help
    OpenConn ("Driver={SQL Server};Server=localhost;Database=gl;Trusted_Conne ction=Yes;")
    Public conn As ADODB.Connection
    Public rs As ADODB.Recordset
    Public SQLstr As String
    Public modconnString As String
    Public cmd As ADODB.Command
    Public Sub setConnString(ConnString As String)
        modconnString = ConnString
    End Sub
    Public Function OpenConn(xconn As ADODB.Connection, Optional ConnString As String = "", Optional remainOpenIfAlreadyOpened As Boolean = False) As Boolean
        On Error GoTo EH
        'Open Connection
        If Not xconn Is Nothing Then
            If xconn.State = adStateOpen Then
                If remainOpenIfAlreadyOpened Then
                    OpenConn = True
                    Exit Function
                End If
            End If
        End If
        Set xconn = New ADODB.Connection
        xconn.ConnectionString = IIf(ConnString = "", modconnString, ConnString)
        xconn.CursorLocation = adUseClient
        OpenConn = (xconn.State = adStateOpen)
        Exit Function
        OpenConn = False
    End Function
    Last edited by loquin; 01-12-07 at 02:55.

  2. #2
    Join Date
    Jun 2004
    Arizona, USA
    Two things.

    You've written openconn as a function, returning a boolean value indicating success.

    You would therefore need to call it as a function, rather than as a sub.

    if openconn (yourConnectionString) then
      msgbox "Success!!!"
      msgbox "Failure..."
    end if
    If this is not the issue, add a breakpoint to the openconn function, and run the app. When execution halts at the breakpoint you set, step through the function using the <F8> key. Pause the cursor over variable names to see the curent value as a tooltip.

    ref Debugging a VB App at one of our sister sites.
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Posting Permissions

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