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

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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