I want to write a global function for my whole project to use in every form. I tried creating a public module and create a public function, but when I call it to my form, it generates error.
let say my global function is about connection to the database. Then when I call it, it says that the connection property is not initialized.
In my function file, I used:
Imports System.Data.SqlClient
Public Module Connection
Dim myConnection As SqlConnection
Public Sub ConnectToDatabase()
myConnection = New SqlConnection(".............")
myConnection.Open()
End Sub
End Module
And in my form, I used:
Private Sub Form_Load(...........) Handles MyBase.Load
ConnectToDatabase() 'I call the function here
...............................................
End Sub
And this does not work.
Thanks.
I suspect this has nothing to do with the fact that this is a public module or public function and everything to do with the fact that you aren’t initialising your connection correctly.
Try the following code: