Skip to content

Instantly share code, notes, and snippets.

@skazuki0430
Last active May 25, 2018 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skazuki0430/9a6a9f8376bac873dbaebd4d609e033b to your computer and use it in GitHub Desktop.
Save skazuki0430/9a6a9f8376bac873dbaebd4d609e033b to your computer and use it in GitHub Desktop.
Execute Query from VBA
Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Public Function Connect(ServerName As String, DBName As String, UserId As String, Password As String)
Dim connectionString As String
connectionString = "Provider=SQLOLEDB;" & _
"Data Source=" & ServerName & ";" & _
"Initial Catalog=" & DBName
cnn.Open connectionString, userid, password
cmd.ActiveConnection = cnn
End Function
Public Function Disconnect()
cnn.Close
Set cmd = Nothing
Set cnn = Nothing
End Function
Public Function ExecuteQuery(Query As String)
cmd.CommandText = Query
cmd.Execute
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment