DebbieCoates
Programmer
I am new to using visual basic with SQL, previously I have used Access.
I was wondering if anyone could answer these few questions for me.
If I was to create a login form asking for username and password, and then once that
Was supplied I then connect to my SQL Server using an ado connection, providing I didn’t close the form down (hid it instead), would my connection stay open all the time whilst my user is in my application?
And if I declared the adodb connection as a global variable, whatever form I am in, could I then just use this to call the connection
Ie
Global cnn As New ADODB.Connection
Public Function Connect() As Integer
cnn.Open "Provider=sqloledb;" & _
"Data Source=SERVERNAME;" & _
"Initial Catalog=DATABASENAME;" & _
"User Id=USERID;" & _
"Password=PASSWORD"
Connect = cnn.State
This function would open my connection when my users first enter my application
And then
Private Sub Command6_Click()
If Connect = 1 Then
Dim rs As New ADODB.Recordset
Dim strsql As String
strsql = "Select * from Projects"
rs.Open strsql, cnn
With rs
Me.projectid = rs.Fields("ProjectId")
Me.txtname = rs.Fields("name")
Me.organisation = rs.Fields("organisation")
End With
rs.Close
Set rs = Nothing
Else
'Unable to establish a connection
End If
End Sub
Private Sub Form_Close()
cnn.Close
Set cnn = Nothing
End Sub
Private Sub Form_Open(Cancel As Integer)
Call Connect
End Sub
End Function
Is this a good way of doing this rather than opening a new connection everytime I wanted to do something with SQL?
I was wondering if anyone could answer these few questions for me.
If I was to create a login form asking for username and password, and then once that
Was supplied I then connect to my SQL Server using an ado connection, providing I didn’t close the form down (hid it instead), would my connection stay open all the time whilst my user is in my application?
And if I declared the adodb connection as a global variable, whatever form I am in, could I then just use this to call the connection
Ie
Global cnn As New ADODB.Connection
Public Function Connect() As Integer
cnn.Open "Provider=sqloledb;" & _
"Data Source=SERVERNAME;" & _
"Initial Catalog=DATABASENAME;" & _
"User Id=USERID;" & _
"Password=PASSWORD"
Connect = cnn.State
This function would open my connection when my users first enter my application
And then
Private Sub Command6_Click()
If Connect = 1 Then
Dim rs As New ADODB.Recordset
Dim strsql As String
strsql = "Select * from Projects"
rs.Open strsql, cnn
With rs
Me.projectid = rs.Fields("ProjectId")
Me.txtname = rs.Fields("name")
Me.organisation = rs.Fields("organisation")
End With
rs.Close
Set rs = Nothing
Else
'Unable to establish a connection
End If
End Sub
Private Sub Form_Close()
cnn.Close
Set cnn = Nothing
End Sub
Private Sub Form_Open(Cancel As Integer)
Call Connect
End Sub
End Function
Is this a good way of doing this rather than opening a new connection everytime I wanted to do something with SQL?