jamesexcel1970
Member
Hello Everybody.
Working with some new assignment, project is all about from excel vba sheet need to validate data with the tables existing in Sql server.
I am copying an VBA code snippet from MSDN that shows me how to grab results from a SQL query into excel sheet.
For example:
Wanted to validated account and names from excl sheet existing in sql server in three tables.
Account Name
123456 James
Working with some new assignment, project is all about from excel vba sheet need to validate data with the tables existing in Sql server.
I am copying an VBA code snippet from MSDN that shows me how to grab results from a SQL query into excel sheet.
For example:
Wanted to validated account and names from excl sheet existing in sql server in three tables.
Account Name
123456 James
Code:
Sub GetDataFromADO()
'Declare variables'
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;User ID=abc;Password=abc;"
objMyConn.Open
'Set and Excecute SQL Command'
Set objMyCmd.ActiveConnection = objMyConn
objMyCmd.CommandText = "select * from myTable"
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open objMyCmd
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)
End Sub