Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public aCon As New ADODB.Connection
- Public aCmd(1 To 4) As New ADODB.Command
- Sub Connect()
- Dim sConn As String
- sConn = "Provider=SQLOLEDB;Trusted_Connection=Yes;Server=win-GLH5GJH0UBR;Database=Northwind" ' << use your server name
- With aCon
- .ConnectionString = sConn
- .CursorLocation = adUseClient
- .Open
- End With
- BuildProcs
- End Sub
- Sub BuildProcs()
- With aCmd(1)
- .ActiveConnection = aCon
- .CommandType = adCmdStoredProc
- .CommandText = "dbo.UpdOrderCustomer"
- .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
- .Parameters.Append .CreateParameter("@CompanyName", adVarWChar, adParamInput, 40)
- End With
- With aCmd(2)
- .ActiveConnection = aCon
- .CommandType = adCmdStoredProc
- .CommandText = "dbo.UpdOrderOrderDate"
- .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
- .Parameters.Append .CreateParameter("@OrderDate", adDBTimeStamp, adParamInput)
- End With
- With aCmd(3)
- .ActiveConnection = aCon
- .CommandType = adCmdStoredProc
- .CommandText = "dbo.UpdOrderRequiredDate"
- .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
- .Parameters.Append .CreateParameter("@RequiredDate", adDBTimeStamp, adParamInput)
- End With
- With aCmd(4)
- .ActiveConnection = aCon
- .CommandType = adCmdStoredProc
- .CommandText = "dbo.UpdOrderFreight"
- .Parameters.Append .CreateParameter("@OrderID", adInteger, adParamInput)
- .Parameters.Append .CreateParameter("@Freight", adCurrency, adParamInput)
- End With
- End Sub
- Sub PopulateSheet()
- Dim n As Integer, r As Long
- Dim aCmdFetchOrders As New ADODB.Command
- Dim aCmdFetchCustomers As New ADODB.Command
- Dim aRstOrders As New ADODB.Recordset
- Dim aRstCustomers As New ADODB.Recordset
- If aCon.State = adStateClosed Then Connect
- With aCmdFetchOrders
- .ActiveConnection = aCon
- .CommandType = adCmdStoredProc
- .CommandText = "dbo.FetchOrders"
- Set aRstOrders = .Execute
- End With
- r = aRstOrders.RecordCount
- With aCmdFetchCustomers
- .ActiveConnection = aCon
- .CommandType = adCmdStoredProc
- .CommandText = "dbo.FetchCustomers"
- Set aRstCustomers = .Execute
- End With
- Worksheets(1).Activate
- Application.ScreenUpdating = False
- Cells(2, 1).CopyFromRecordset aRstOrders
- For n = 1 To aRstOrders.Fields.Count
- Cells(1, n) = aRstOrders(n - 1).Name
- Cells(1, n).EntireColumn.AutoFit
- Next
- Cells(1).EntireColumn.Hidden = True
- Cells(r + 100, 100).CopyFromRecordset aRstCustomers
- With Range(Cells(2, 2), Cells(2, 2).End(xlDown))
- .Validation.Delete
- .Validation.Add xlValidateList, xlValidAlertInformation, 1, _
- "=" & Range(Cells(r + 100, 100), Cells(r + 100, 100).End(xlDown)).Address
- End With
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment