vba help

shyamvinjamuri

Board Regular
Joined
Aug 2, 2006
Messages
175
Hi
I am looking for importing data from sql server thru vba.

I am new to vba. please help.

Here is the code I copied and changed.
Server Name : db-msd5002-devdb-msd5002-dev
Database Name : AMC_DWAMV_DW
Table :dbo.Loandbo.Loan
Fields : LoanID, InvestorNameLoanID,InvestorName
Sub DatafromSQLserver
Dim oConn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mssql As String
Dim row As Integer
Dim Col As Integer
Dim ws As ThisWorkbook
Set ws = ThisWorkbook
Application.ScreenUpdating = False
Set oConn = New ADODB.Connection
Set rs = New ADODB.Recordset
mssql = "Select LoanID,InvestorName" & _
"From Loan"
"Server=db-msd-5002-dev;authenticateduser=TRUE;database = AMC_DW"
oConn.ConnectionTimeout = 30
rs.Open mssql, oConn
If rs.EOF Then
MsgBox "No Matching Records Found"
rs.Close
oConn.Close
Exit Sub
End If
row = 5
col = 1
For Each fld in rs.Fields
Sheet2.Cells(row,col).value = fld
col = col+1
Next
row = row+1
rs.MoveNext
Loop
rs.Close
oConn.Close
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,214,428
Messages
6,119,420
Members
448,895
Latest member
omarahmed1

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