Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.    
  3.     Sub GetTrusteeAudit()
  4.    
  5.     Dim conn As ADODB.Connection
  6.     Dim runsp As ADODB.Command
  7.     Dim results As ADODB.Recordset
  8.     Dim prmdate As ADODB.Parameter
  9.     Dim sconnect As String
  10.     Dim server As String
  11.     Dim db As String
  12.     Dim userid As String
  13.     Dim pwd As String
  14.     Dim todaysdate As Variant
  15.    
  16.    
  17.     ' Enter the start date, will be passed to the stored procedure and shown in B2
  18.    
  19.     Do While Not IsDate(todaysdate)
  20.         todaysdate = InputBox("Enter the start date")
  21.     Loop
  22.    
  23.     Sheets("Audit").Cells(2, 2).Value = todaysdate
  24.    
  25.     ' Info for connection
  26.    
  27.     server = "p"
  28.     db = "r"
  29.     userid = "a"
  30.     pwd = "@"
  31.     sconnect = "Driver={SQL Server};Server=" & server & "; Database=" & db & _
  32.     ";uid=" & userid & ";pwd=" & pwd & ";"
  33.    
  34.     ' Create connection and recordset
  35.    
  36.     Set conn = New ADODB.Connection
  37.     With conn
  38.         .CommandTimeout = 900
  39.         .ConnectionString = sconnect
  40.         .Open
  41.     End With
  42.    
  43.    
  44.    
  45.     ' Run the stored procedure
  46.    
  47.     Set runsp = New ADODB.Command
  48.     With runsp
  49.         .CommandType = adCmdStoredProc
  50.         .CommandText = "DEV_GetTrusteeAudit"
  51.         .ActiveConnection = conn
  52.         Set prmdate = runsp.CreateParameter(Name:="startdate", Type:=adDate, Direction:=adParamInput)
  53.         .Parameters.Append prmdate
  54.         .Parameters("startdate").Value = todaysdate
  55.     End With
  56.    
  57.    
  58.    
  59.     Set results = New ADODB.Recordset
  60.     results.CursorType = adOpenStatic
  61.     results.LockType = adLockOptimistic
  62.     results.Open runsp
  63.    
  64.     If results.EOF Then
  65.         MsgBox ("Nothing found")
  66.     Else
  67.    
  68.     ' put headers on
  69.        For Each qf In results.Fields
  70.             Range("a4").Offset(0, coloffset).Value = qf.Name
  71.             coloffset = coloffset + 1
  72.         Next qf
  73.    
  74.         Sheets("audit").Cells(5, 1).CopyFromRecordset results
  75.    
  76.    
  77.     End If
  78.    
  79.     results.Close
  80.    
  81.     conn.Close
  82.    
  83.     Set results = Nothing
  84.    
  85.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement