msfz751

SQL connection from Excel

Jan 2nd, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public aCon As New ADODB.Connection
  2. Public aCmd(1 To 4) As New ADODB.Command
  3. Sub Connect()
  4.   Dim sConn As String
  5.   sConn = "Provider=SQLOLEDB;Trusted_Connection=Yes;Server=win-GLH5GJH0UBR;Database=Northwind" ' << use your server name
  6.  With aCon
  7.     .ConnectionString = sConn
  8.     .CursorLocation = adUseClient
  9.     .Open
  10.   End With
  11.   BuildProcs
  12. End Sub
  13. Sub BuildProcs()
  14.   With aCmd(1)
  15.     .ActiveConnection = aCon
  16.     .CommandType = adCmdStoredProc
  17.     .CommandText = "dbo.UpdOrderCustomer"
  18.     .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
  19.     .Parameters.Append .CreateParameter("@CompanyName", adVarWChar, adParamInput, 40)
  20.   End With
  21.   With aCmd(2)
  22.     .ActiveConnection = aCon
  23.     .CommandType = adCmdStoredProc
  24.     .CommandText = "dbo.UpdOrderOrderDate"
  25.     .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
  26.     .Parameters.Append .CreateParameter("@OrderDate", adDBTimeStamp, adParamInput)
  27.   End With
  28.   With aCmd(3)
  29.     .ActiveConnection = aCon
  30.     .CommandType = adCmdStoredProc
  31.     .CommandText = "dbo.UpdOrderRequiredDate"
  32.     .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
  33.     .Parameters.Append .CreateParameter("@RequiredDate", adDBTimeStamp, adParamInput)
  34.   End With
  35.   With aCmd(4)
  36.     .ActiveConnection = aCon
  37.     .CommandType = adCmdStoredProc
  38.     .CommandText = "dbo.UpdOrderFreight"
  39.     .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
  40.     .Parameters.Append .CreateParameter("@Freight", adCurrency, adParamInput)
  41.   End With
  42. End Sub
  43.  
  44. Sub PopulateSheet()
  45.   Dim n As Integer, r As Long
  46.   Dim aCmdFetchOrders As New ADODB.Command
  47.   Dim aCmdFetchCustomers As New ADODB.Command
  48.   Dim aRstOrders As New ADODB.Recordset
  49.   Dim aRstCustomers As New ADODB.Recordset
  50.  
  51.   If aCon.State = adStateClosed Then Connect
  52.  
  53.   With aCmdFetchOrders
  54.     .ActiveConnection = aCon
  55.     .CommandType = adCmdStoredProc
  56.     .CommandText = "dbo.FetchOrders"
  57.     Set aRstOrders = .Execute
  58.   End With
  59.   r = aRstOrders.RecordCount
  60.  
  61.   With aCmdFetchCustomers
  62.     .ActiveConnection = aCon
  63.     .CommandType = adCmdStoredProc
  64.     .CommandText = "dbo.FetchCustomers"
  65.     Set aRstCustomers = .Execute
  66.   End With
  67.  
  68.   Worksheets(1).Activate
  69.   Application.ScreenUpdating = False
  70.   Cells(2, 1).CopyFromRecordset aRstOrders
  71.  
  72.   For n = 1 To aRstOrders.Fields.Count
  73.     Cells(1, n) = aRstOrders(n - 1).Name
  74.     Cells(1, n).EntireColumn.AutoFit
  75.   Next
  76.   Cells(1).EntireColumn.Hidden = True
  77.   Cells(r + 100, 100).CopyFromRecordset aRstCustomers
  78.   With Range(Cells(2, 2), Cells(2, 2).End(xlDown))
  79.     .Validation.Delete
  80.     .Validation.Add xlValidateList, xlValidAlertInformation, 1, _
  81.       "=" & Range(Cells(r + 100, 100), Cells(r + 100, 100).End(xlDown)).Address
  82.   End With
  83. End Sub
Advertisement
Add Comment
Please, Sign In to add comment