Open Connection ODBC
I had to stretch a little on this one. Everywhere I contract, the connection strings have their own special nyances.I needed to get the UserID and Workstation ID before going for the data. I used Enum to define where the columns are because the data has changed several times.
Enum InvCols
RunDate = 18
DateStamp = 22 'Set column # of field
PlantName = 20
PartNumb = 1
End Enum
Sub OpenQueryPop()
Dim UserName As String
Dim daCurDate As Date
Dim RptDate As String
Dim CurMach As String
Dim I As Long
Dim LastCol As Long
Dim LastRowr As Long
Dim GetDate As Long
Dim cnPubs As ADODB.Connection
Dim rsPubs As ADODB.Recordset
Dim strConn As String
Dim SQLstr As String
Set cnPubs = New ADODB.Connection
daCurDate = InputBox("Enter the date for which you want Inventory report", "Get Date", Application.Text(Now() - 1, "mm/dd/yyyy"))
RptDate = Application.Text(daCurDate, "YYYY-MM-DD")
CurMach = Environ("ComputerName")
UserName = Environ("Username")
ActiveWorkbook.Sheets("Inv").Select
ActiveSheet.UsedRange.Clear
strConn = "Trusted_Connection=Yes;Initial Catalog=CompanyWideMetrics;"
strConn = strConn & "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;"
strConn = strConn & "SERVER=dev-sql;"
strConn = strConn & " UID=" & UserName & ";Workstation ID=" & CurMach & ";"
cnPubs.Open strConn
SQLstr = "SELECT * "
SQLstr = SQLstr & " FROM CompanyWideMetrics.dbo.vwCombinedInventory vwCombinedInventory "
SQLstr = SQLstr & " WHERE (vwCombinedInventory.RunDate>={ts '" & RptDate _
& " 00:00:00.000'} And vwCombinedInventory.RunDate<{ts '" & RptDate & " 23:59:59.999'})"
SQLstr = SQLstr & " ORDER BY vwCombinedInventory.PartNumber,vwCombinedInventory.PlantLocation;"
Set rsPubs = New ADODB.Recordset
With rsPubs
.ActiveConnection = cnPubs
.Open SQLstr
For I = 0 To .Fields.Count - 1
Sheets("Inv").Cells(1, I + 1) = .Fields(I).Name
Next
Sheets("Inv").Range("A2").CopyFromRecordset rsPubs
.Close
End With
LastRowr = Range(Cells(999990, InvCols.PartNumb).Address).End(xlUp).Row 'LAST ROW
Range(Cells(2, InvCols.RunDate), Cells(LastRowr, InvCols.RunDate)).Select
Selection.NumberFormat = "mm/dd/yyyy h:mm;@"
Cells(1, InvCols.PartNumb).Select
LastCol = Selection.End(xlToRight).Column 'LAST COLUM
Columns(LastCol + 1).Select
Range(Cells(1, InvCols.PartNumb), Cells(1, LastCol + 1)).Select
Selection.Font.bold = True
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 192
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Range("A2").Select
ActiveWindow.FreezePanes = True
'----------------------
cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing
ConvertTimeStampI InvCols.RunDate
' BuildDailyPivot
MsgBox "Completed with " & LastRowr & " lines of data", vbInformation
End Sub
We were originally pulling back 990,156 rows of data. VERY big error someplace. Now it is more like 75,000 rows.
|