VBA certain columns won't read properly from SQL database

Shadow777

New Member
Joined
Nov 10, 2019
Messages
4
I have a SQLite database that contains financial data. The code is working and exporting data correctly. However whenever any text fields are located, e.g. Earningsdate, Ticker and CompanyName it will not output the data correctly. I have set every Database item field as a REAL datatype. If I change the Ticker, CompanyName and Earningsdate fields to the Blob or Text datatype then nothing is output at all.

VBA Code:
Sub GetData()

Dim conn As ADODB.Connection
Dim TestData As ADODB.Recordset
Dim icols As Integer

Dim code As Range

Set code = Worksheets("Sheet1").Range("D1")
Dim r As Variant
r = code.Value

Set conn = New ADODB.Connection
Set TestData = New ADODB.Recordset

conn.Open "DRIVER=SQLITE3 ODBC DRIVER;DATABASE=C:\Users\jaz\financials.db;"

strSQL = "SELECT * FROM financials WHERE Ticker= '" & r & "'"

TestData.Open strSQL, conn

For icols = 0 To TestData.Fields.Count - 1
    Worksheets("Sheet2").Cells(1, icols + 1).Value = TestData.Fields(icols).Name
Next icols

Worksheets("Sheet2").Range("A2").CopyFromRecordset TestData
TestData.Close

Set TestData = Nothing
Set conn = Nothing


End Sub

Here is the output in Excel:

1591474269611.png


As you can see, the CompanyName, Ticker and EarningsDate aren't reported correctly.

Below is what those respective fields look like in the Database:

EarningsDate 16 Jul 2020 - 20 Jul 2020 [type of data is Text / Numeric]
CompanyName Microsoft [type of data is Text / Numeric]
Ticker MSFT [type of data is Text / Numeric]

I'm not sure why these non numeric types aren't outputting properly given that the VBA code says to get all records.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top