Match column from spreadsheet to SQL server and dump data to same spreadsheet

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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top