Skip to content

Instantly share code, notes, and snippets.

@addohm
Created November 30, 2018 04:21
Show Gist options
  • Save addohm/132d22e669691461ba2fb54597e4b578 to your computer and use it in GitHub Desktop.
Save addohm/132d22e669691461ba2fb54597e4b578 to your computer and use it in GitHub Desktop.
Setting up an SQL connection and using it
Sub DoSomething()
Set conn = New ADODB.connection
Set cmd = New ADODB.Command
conn.Open ThisDisplay.SqlConnectionParameters.ConnectionString
...
...
...
conn.Close
If Not conn Is Nothing Then Set conn = Nothing
If Not cmd Is Nothing Then Set cmd = Nothing
End Sub
Dim actSqlServer, actSqlDBName, actSqlUserName, actSqlPassword As String
Public Property Get ConnectionString() As String
ConnectionString = BuildConnectionString()
End Property
Public Property Let SqlServer(ByVal NewValue As String)
actSqlServer = NewValue
End Property
Public Property Get SqlServer() As String
SqlServer = actSqlServer
End Property
Public Property Let SqlDBName(ByVal NewValue As String)
actSqlDBName = NewValue
End Property
Public Property Get SqlDBName() As String
SqlDBName = actSqlDBName
End Property
Public Property Let SqlUserName(ByVal NewValue As String)
actSqlUserName = NewValue
End Property
Public Property Get SqlUserName() As String
SqlUserName = actSqlUserName
End Property
Public Property Let SqlPassword(ByVal NewValue As String)
actSqlPassword = NewValue
End Property
Public Property Get SqlPassword() As String
SqlPassword = actSqlPassword
End Property
Private Function BuildConnectionString() As String
If Not Me.SqlDBName = "" And Not Me.SqlServer = "" And Not Me.SqlUserName = "" And Not Me.SqlPassword = "" Then
BuildConnectionString = "Provider=sqloledb;data source=" & Me.SqlServer _
& ";initial catalog=" & Me.SqlDBName _
& ";user id=" & Me.SqlUserName _
& ";password=" & Me.SqlPassword
Else
BuildConnectionString = ""
End If
End Function
Private Sub Display_AnimationStart()
Set SqlConnectionParameters = New SQLConnection
SqlConnectionParameters.SqlDBName = "databasename"
SqlConnectionParameters.SqlServer = "serveraddress"
SqlConnectionParameters.SqlUserName = "username"
SqlConnectionParameters.SqlPassword = "password"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment