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.
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-dev | db-msd5002-dev |
Database Name : AMC_DW | AMV_DW |
Table :dbo.Loan | dbo.Loan |
Fields : LoanID, InvestorName | LoanID,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 |