Hi there!

What I am used to declaring database connection is that in every form I type the codes for it which is a tiring way. Now, I want to use module instead but I really don't know how. And also, how will I call the connection to every forms I created? Can you help me about it?:)

Thanks in advance!

MODULE GetDBConnection
        
    Public Function GetConnection() As SqlConnection

        
        Dim cnReturn As SqlConnection
        Dim strConnect As String


        cnReturn = Nothing

        strConnect = “Your connection string”

        cnReturn = New SqlConnection(strConnect)

        cnReturn.Open()
        If cnReturn.State = ConnectionState.Open Then GetConnection =cnReturn


        GetConnection = cnReturn
Else
MsgBox("Failed to acquire an open connection to the database server.  Please check your network connection.")


        Exit Function


        
    End Function

End Module

Thanks Pgmer:)
But something went wrong here, please take a look at the picture

You need to handle the else part...:)
Return nothing..

Thanks! I found a simpler way of it and already resolved it:D

Will you please let me know your working solution? I'm in need of the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.