0

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!

3
Contributors
5
Replies
12
Views
6 Years
Discussion Span
Last Post by savedlema
0
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
0

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

Edited by pixma: n/a

Attachments oledb.JPG 60.29 KB
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.