Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub GetTrusteeAudit()
- Dim conn As ADODB.Connection
- Dim runsp As ADODB.Command
- Dim results As ADODB.Recordset
- Dim prmdate As ADODB.Parameter
- Dim sconnect As String
- Dim server As String
- Dim db As String
- Dim userid As String
- Dim pwd As String
- Dim todaysdate As Variant
- ' Enter the start date, will be passed to the stored procedure and shown in B2
- Do While Not IsDate(todaysdate)
- todaysdate = InputBox("Enter the start date")
- Loop
- Sheets("Audit").Cells(2, 2).Value = todaysdate
- ' Info for connection
- server = "p"
- db = "r"
- userid = "a"
- pwd = "@"
- sconnect = "Driver={SQL Server};Server=" & server & "; Database=" & db & _
- ";uid=" & userid & ";pwd=" & pwd & ";"
- ' Create connection and recordset
- Set conn = New ADODB.Connection
- With conn
- .CommandTimeout = 900
- .ConnectionString = sconnect
- .Open
- End With
- ' Run the stored procedure
- Set runsp = New ADODB.Command
- With runsp
- .CommandType = adCmdStoredProc
- .CommandText = "DEV_GetTrusteeAudit"
- .ActiveConnection = conn
- Set prmdate = runsp.CreateParameter(Name:="startdate", Type:=adDate, Direction:=adParamInput)
- .Parameters.Append prmdate
- .Parameters("startdate").Value = todaysdate
- End With
- Set results = New ADODB.Recordset
- results.CursorType = adOpenStatic
- results.LockType = adLockOptimistic
- results.Open runsp
- If results.EOF Then
- MsgBox ("Nothing found")
- Else
- ' put headers on
- For Each qf In results.Fields
- Range("a4").Offset(0, coloffset).Value = qf.Name
- coloffset = coloffset + 1
- Next qf
- Sheets("audit").Cells(5, 1).CopyFromRecordset results
- End If
- results.Close
- conn.Close
- Set results = Nothing
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement