LearningMacro
New Member
- Joined
- Aug 19, 2014
- Messages
- 19
I've to create a macro that will match the column from spreadsheet to the sql server table and dump the data on the spreadsheet. For example my spreadsheet has below columns:
<code style="border: 0px; font-weight: inherit; font-style: inherit; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; outline: 0px; padding: 0px; white-space: inherit;">JobNumber
J114567
J012345
J556896</code>I need to match the Jobnumber with the sql server table and dump the data on the spreadsheet;
<code style="border: 0px; font-weight: inherit; font-style: inherit; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; outline: 0px; padding: 0px; white-space: inherit;">JobNumber`Customer`
J114567 `ABC`
J012345` Xyz Solutions`
J556896` DEF corporation`</code>I've created a simple macro that will pull data from sql server. How can I match the job from the spreadsheet to sql table? I'm new to VBA. Any help is appreciated. Thanks.
Here is the macro:
<code style="border: 0px; font-weight: inherit; font-style: inherit; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; outline: 0px; padding: 0px; white-space: inherit;">Sub GetDataFromADO()
'Declare variables'
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=0.0.0.0;Initial Catalog=job;User ID=test;Password=test123;"
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = "select top 100 * from jobtab"
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)</code>End Sub
<code style="border: 0px; font-weight: inherit; font-style: inherit; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; outline: 0px; padding: 0px; white-space: inherit;">JobNumber
J114567
J012345
J556896</code>I need to match the Jobnumber with the sql server table and dump the data on the spreadsheet;
<code style="border: 0px; font-weight: inherit; font-style: inherit; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; outline: 0px; padding: 0px; white-space: inherit;">JobNumber`Customer`
J114567 `ABC`
J012345` Xyz Solutions`
J556896` DEF corporation`</code>I've created a simple macro that will pull data from sql server. How can I match the job from the spreadsheet to sql table? I'm new to VBA. Any help is appreciated. Thanks.
Here is the macro:
<code style="border: 0px; font-weight: inherit; font-style: inherit; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; margin: 0px; outline: 0px; padding: 0px; white-space: inherit;">Sub GetDataFromADO()
'Declare variables'
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=0.0.0.0;Initial Catalog=job;User ID=test;Password=test123;"
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = "select top 100 * from jobtab"
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)</code>End Sub