Click here to Skip to main content
15,886,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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.
Posted
Updated 2-Apr-21 2:39am
v2
Comments
Richard MacCutchan 2-Apr-21 8:14am    
That is Excel formatting the cells. You need to add code to set the default format of the cells, columns or rows, as required.
Cody O'Meara 2-Apr-21 8:44am    
Thank you. This was one of the moments where I was working for around 12 hours straight, it was the end of the night and when you cant figure out the easy solution like this, its time to go to bed. haha! All set :D
Richard MacCutchan 2-Apr-21 8:59am    
So true. The brain goes into relax mode after too many hours focused on a single problem.
Richard Deeming 6-Apr-21 10:21am    
Don't ignore SQL Injection[^] just because you're in an Excel sheet or other desktop application.

Parameters in ADODB are slightly harder to use than in .NET, but they're still vital to protect your database from accidental or malicious damage.

1 solution

Try to use Range.ClearFormats method (Excel) | Microsoft Docs[^]

VB
With Worksheets("WB").Range("a1:z500")
    .ClearContents
    .ClearFormats
    .CopyFromRecordset rs
End With
 
Share this answer
 
Comments
Richard MacCutchan 2-Apr-21 8:40am    
+5. I knew what was needed, just not how to do it.
Maciej Los 2-Apr-21 9:07am    
Thank you, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900