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)

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

        GetConnection = cnReturn
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

Edited 5 Years Ago by pixma: n/a

Attachments oledb.JPG 60.29 KB

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

This question has already been answered. Start a new discussion instead.