I am very new to VBA(I do have programming knowledge in other language though). I am creating a very detailed report from multiple sources to eventually merge into one report for management. However when ever it selects my values such as dollar amounts and quantities it converts most to a date format which is a no-go.
Code:
Sub WBALL()
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "REMOVED"
Database_Name = "REMOVED"
User_ID = "REMOVED"
Password = "REMOVED"
SQLStr = "Select [Document Date],[SOP Number],[Item Number],[Item Description],[QTY],[Extended Cost],[Extended Price] ,[Unit Cost],[Unit Price],[Customer Number]From [TLC].[dbo].[SalesLineItems]where[SOP Type] = '" & Range("C2").Value & "' and[Customer Number] = '" & Range("C3").Value & "' and[Document Date] > '" & Range("C4").Value & "' and [Document Date] < '" & Range("c5").Value & "' and[Item Number] like '" & Range("C7").Value & "' "
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
With Worksheets("WB").Range("a1:z500")
.ClearContents
.CopyFromRecordset rs
End With
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
Results in Excel:
3/12/2021 INV050514 CONTRACT WB GRD 1 WET BLUE PIGSKIN GRADE 1 7/27/1900 10/3/1900 8/29/1900 1/1/1900 1/1/1900 WOLVERINELEATH
3/12/2021 INV050514 CONTRACT WB GRD 2 WET BLUE PIGSKIN GRADE 2 3/14/1903 4/1/1904 9/17/1903 1/1/1900 1/1/1900 WOLVERINELEATH
Data in SQL Table:
2021-03-12 00:00:00.000 INV050514 CONTRACT WB GRD 1 WET BLUE PIGSKIN GRADE 1 209.00000 277.80000 242.44000 1.33000 1.16000 WOLVERINELEATH
2021-03-12 00:00:00.000 INV050514 CONTRACT WB GRD 2 WET BLUE PIGSKIN GRADE 2 1169.00000 1553.22000 1356.04000 1.33000 1.16000 WOLVERINELEATH
Any help is appreciated!
What I have tried:
Haven't tried really much. A lot of what I've found is targeting me on how to convert values, Not keep values the same. Im sure if I try converting these dates to what it needs to be it would be way off.