Retrieving data via ADO

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to retrieve data from a csv file via ADO.

If I open the csv file, the data format for my numbers are just that, numbers.

However, once I run the following code, it turns these numbers into text.

Can someone please tell me how I can retrieve the data so that it returns numbers, not text?

Thanks



Code:
Option Explicit

Sub ExtractQuery()

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

Dim rs As ADODB.Recordset
Dim rng As Range
Dim strcon As String
Dim strSQL As String
Dim DBLoc As String

DBLoc = "C:\DataLocation\"

On Error GoTo ErrHandler
    
    ' The database connection string.
            
    strcon = "Provider = Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source=" & DBLoc & ";" & _
             "Extended Properties=""text;" & _
             "HDR=Yes;" & _
             "FMT=Delimited"""
    
    cn.Open ConnectionString:=strcon
    
    ' This is the range that will receive the data.
    
    Set rng = Cells(2, 1)
    
    ' The query to execute
       
    strSQL = "SELECT * FROM [Myfile.csv]"
    
    ' Create & Open the recordset
    
    Set rs = New ADODB.Recordset
    
    rs.Open Source:=strSQL, _
            ActiveConnection:=strcon
    
    ' Copy to the range
    
    rng.CopyFromRecordset Data:=rs
    ' Close the recordset when you're done with it.
    
    rs.Close
    cn.Close
   
    Set rng = Nothing
    
    Set rs = Nothing
    Set cn = Nothing
          
    Exit Sub
    
ErrHandler:
    
    MsgBox Prompt:="Sorry, an error occured. " & Err.Description, _
           Buttons:=vbOKOnly, _
           Title:="Error retrieving data"
    
    ' resume at the ExitPoint label to clean up object variables
    
    Set rng = Nothing
    
    Set rs = Nothing
    Set cn = Nothing
    
    Application.StatusBar = False
    
    Application.ScreenUpdating = True
    
    End
    
End Sub
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
you need to create a schema.ini file in the same folder as your csv file to help describe the data layout to the ADO process, see below a sample of what i have used in the past, whilst not perfect code it works for me, change the file locations and number of columns of data etc etc

NOTE field F6 is text, you also need to experiment with ColNameHeader being true or false depending on your data headers or not

Code:
Sub ParseDataFile()

'****
' Setup constants for the SQL query
'****

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1

'****
' Declare stuff
'****

Dim StrSQL As String

'****
' create objects that we need
'****

Set objconnection = CreateObject("ADODB.Connection")
Set objrecordset = CreateObject("ADODB.Recordset")

StrPathToTextFile = "C:\mydata\"
filename = "file1.csv"

Call CreateSchema

'****
' All preparatory work done, lets begin the main process
'****

'****
' Open JET connection to our data file
'****

objconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & StrPathToTextFile & ";" & _
        "Extended Properties=""text;HDR=NO;FMT=FixedLength"""

'****
' Build SQL strings to get unique categories in no order
'****

StrSQL = "SELECT * FROM " & StrFile
StrSQL = StrSQL & " WHERE F5 <> 0"

'****
' Execute the SQL and get the results and store for next stage
'****

objrecordset.Open StrSQL, objconnection, adOpenStatic, adLockOptimistic, adCmdText

Range("A1").CopyFromRecordset objrecordset
Columns("A:F").EntireColumn.AutoFit

objrecordset.Close

End Sub
Sub CreateSchema()

Open StrPathToTextFile & "schema.ini" For Output As #1

Print #1, "[" & StrFile & "]"
Print #1, "Format=Delimited(,)"
Print #1, "ColNameHeader = False"
Print #1, "MaxScanRows=0"
Print #1, "Col1=F1 Integer"
Print #1, "Col2=F2 Integer"
Print #1, "Col3=F3 Integer"
Print #1, "Col4=F4 Integer"
Print #1, "Col5=F5 Integer"
Print #1, "Col6=F6 Text"

Close #1

End Sub
 
Last edited:
Upvote 0
you need to create a schema.ini file in the same folder as your csv file to help describe the data layout to the ADO process, see below a sample of what i have used in the past, whilst not perfect code it works for me, change the file locations and number of columns of data etc etc

NOTE field F6 is text, you also need to experiment with ColNameHeader being true or false depending on your data headers or not

Code:
Sub ParseDataFile()

'****
' Setup constants for the SQL query
'****

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1

'****
' Declare stuff
'****

Dim StrSQL As String

'****
' create objects that we need
'****

Set objconnection = CreateObject("ADODB.Connection")
Set objrecordset = CreateObject("ADODB.Recordset")

StrPathToTextFile = "C:\mydata\"
filename = "file1.csv"

Call CreateSchema

'****
' All preparatory work done, lets begin the main process
'****

'****
' Open JET connection to our data file
'****

objconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & StrPathToTextFile & ";" & _
        "Extended Properties=""text;HDR=NO;FMT=FixedLength"""

'****
' Build SQL strings to get unique categories in no order
'****

StrSQL = "SELECT * FROM " & StrFile
StrSQL = StrSQL & " WHERE F5 <> 0"

'****
' Execute the SQL and get the results and store for next stage
'****

objrecordset.Open StrSQL, objconnection, adOpenStatic, adLockOptimistic, adCmdText

Range("A1").CopyFromRecordset objrecordset
Columns("A:F").EntireColumn.AutoFit

objrecordset.Close

End Sub
Sub CreateSchema()

Open StrPathToTextFile & "schema.ini" For Output As #1

Print #1, "[" & StrFile & "]"
Print #1, "Format=Delimited(,)"
Print #1, "ColNameHeader = False"
Print #1, "MaxScanRows=0"
Print #1, "Col1=F1 Integer"
Print #1, "Col2=F2 Integer"
Print #1, "Col3=F3 Integer"
Print #1, "Col4=F4 Integer"
Print #1, "Col5=F5 Integer"
Print #1, "Col6=F6 Text"

Close #1

End Sub

Thanks
 
Upvote 0
cool, if you google ADO and schema you will find how to define other datatypes, sometimes it is hard to find as it is an obscure area as it took me ages to find hence my willingness to share

another thing I forgot to tell you most important is the delimiter option when creating the schema.ini, set accordingly
 
Last edited:
Upvote 0
here are some more examples for defining fields for combinations of data

Code:
Print #1, "Col1=EndDate DateTime"
Print #1, "Col2=RequestingPerson Text"
Print #1, "Col3=Description Text"
Print #1, "Col4=Value double"
Print #1, "Col5=Balance double"

and here is how to deal with fixed width data

Code:
Print #1, "MaxScanRows=0"
Print #1, "Col1=DontNeed Text Width 45"
Print #1, "Col2=Freight Integer width 6"
Print #1, "Col3=Mail Integer width 6"
Print #1, "Col4=Domestic Integer width 3"
Print #1, "Col5=International integer width 3"
Print #1, "Col6=Transit integer width 3"

note col1 I use this to disregard unwanted data from fixed length data basically ignore the first 45 characters
 
Upvote 0
here are some more examples for defining fields for combinations of data

Code:
Print #1, "Col1=EndDate DateTime"
Print #1, "Col2=RequestingPerson Text"
Print #1, "Col3=Description Text"
Print #1, "Col4=Value double"
Print #1, "Col5=Balance double"

and here is how to deal with fixed width data

Code:
Print #1, "MaxScanRows=0"
Print #1, "Col1=DontNeed Text Width 45"
Print #1, "Col2=Freight Integer width 6"
Print #1, "Col3=Mail Integer width 6"
Print #1, "Col4=Domestic Integer width 3"
Print #1, "Col5=International integer width 3"
Print #1, "Col6=Transit integer width 3"

note col1 I use this to disregard unwanted data from fixed length data basically ignore the first 45 characters

cheers, really helpful.
 
Upvote 0
apologies my macro code supplied was flawed, here is a working example posted to another forum request

Code:
Sub ParseDataFile()

'****
' Setup constants for the SQL query
'****

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1

'****
' Declare stuff
'****

Dim StrSQL As String
Dim strPathToTextFile As String
Dim strFile As String

'****
' create objects that we need
'****

Set objconnection = CreateObject("ADODB.Connection")
Set objrecordset = CreateObject("ADODB.Recordset")

strPathToTextFile = "E:\mydata\"
strFile = "file1.csv"

Call CreateSchema(strPathToTextFile, strFile)

'****
' All preparatory work done, lets begin the main process
'****

'****
' Open JET connection to our data file
'****

objconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strPathToTextFile & ";" & _
        "Extended Properties=""text;HDR=YES;FMT=Delimited"""

'****
' Build SQL strings to get unique categories in no order
'****

StrSQL = "SELECT F6 FROM " & strFile
'StrSQL = StrSQL & " WHERE F6 <> 0"

'****
' Execute the SQL and get the results and store for next stage
'****

objrecordset.Open StrSQL, objconnection, adOpenStatic, adLockOptimistic, adCmdText

Range("A1").CopyFromRecordset objrecordset
Columns("A:F").EntireColumn.AutoFit

objrecordset.Close

End Sub
Sub CreateSchema(strPathToTextFile As String, strFile As String)

Open strPathToTextFile & "schema.ini" For Output As #1

Print #1, "[" & strFile & "]"
Print #1, "Format=Delimited(,)"
Print #1, "ColNameHeader = True"
Print #1, "MaxScanRows=0"
Print #1, "Col1=F1 Text"
Print #1, "Col2=F2 DateTime"
Print #1, "Col3=F3 Double"
Print #1, "Col4=F4 Double"
Print #1, "Col5=F5 Double"
Print #1, "Col6=F6 Double"
Print #1, "Col7=F6 Double"

Close #1

End Sub
 
Upvote 0
Thanks Jimrward, I have adopted your code; however, I have two bottleneck remained: (i) How to print reverse-word or fields with two or more words into the schema.ini file. and (ii) How do I save the parsed data into another text file per the original objective.

Here is what I got:

Rich (BB code):
Sub ParseDataFile()
'**********************************
'Setup constants for the SQL query'
'**********************************
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
'****************
' Declare stuff '
'****************
Dim StrSQL As String
Dim strPathToTextFile As String
Dim strFile As String
'******************************
' create objects that we need '
'******************************
Set objconnection = CreateObject("ADODB.Connection")
Set objrecordset = CreateObject("ADODB.Recordset")
strPathToTextFile = "C:\Users\C033732\Desktop\10192018\CSVFolderPivotTable"
strFile = "FFR Query 20181019.csv"
Call CreateSchema(strPathToTextFile, strFile)
'*********************************************************
' All preparatory work done, lets begin the main process '
'*********************************************************
'***************************************
' Open JET connection to our data file '
'***************************************
objconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strPathToTextFile & ";" & _
        "Extended Properties=""text;HDR=YES;FMT=Delimited"""
'*********************************************************
' Build SQL strings to get unique categories in no order '
'*********************************************************
StrSQL = "SELECT FAIN, Fund, Scope, ALI, Project, Activity, [Resource ID], Accounting FROM " & "[" & strFile & "]"
'***************************************************************
' Execute the SQL and get the results and store for next stage '
'***************************************************************
objrecordset.Open StrSQL, objconnection, adOpenStatic, adLockOptimistic, adCmdText
'Range("A1").CopyFromRecordset objrecordset
'Columns("A:F").EntireColumn.AutoFit

' I need to save to another text file, the output is more than a million rows.
objrecordset.Close
End Sub


Rich (BB code):
Sub CreateSchema(strPathToTextFile As String, strFile As String)
Open strPathToTextFile & "schema.ini" For Output As #1 
Print #1 , "[" & strFile & "]"
Print #1 , "Format=CSVDelimited"
Print #1 , "ColNameHeader = True"
Print #1 , "MaxScanRows=25"
Print #1 , "CharacterSet=ANSI"
Print #1 , "Col1=FAIN Char"
Print #1 , "Col2=FUND Char"
Print #1 , "Col3=SCOPE Char"
Print #1 , "Col4=ALI Char"
Print #1 , "Col5=PROJECT Char"
Print #1 , "Col6=ACTIVITY Char"
'Print #1 , "Col7="; RESOURCE; ID; " Char"
Print #1 , "Col8=ACCOUNTING Char"
Print #1 , "Col9=TRANSACTION Char"
'Print #1 , "Col10="; SYSTEM; Source; " Char"
Print #1 , "Col11=JOURNAL Char"
'Print #1 , "Col12="; JRNL; Date; " Char"
'Print #1 , "Col13="; JRNL; SEQ; " Char"
'Print #1 , "Col14="; JRNL; Ln#; " Char"
Print #1 , "Col15=ACCOUNT Char"
Print #1 , "Col16=EMPLOYEE Char"
Print #1 , "Col17=JOBCODE Char"
Print #1 , "Col18=DEPARTMENT Char"
Print #1 , "Col19=TRC Char"
Print #1 , "Col20=VOUCHER Char"
Print #1 , "Col21=VENDOR Char"
'Print #1 , "Col22="; VENDOR; Name; " Char"
Print #1 , "Col23=INVOICE Char"
'Print #1 , "Col24="; INVOICE; Date; " Char"
'Print #1 , "Col25="; VHCR; Ln#; " Char"
'Print #1 , "Col26="; VCHR; DLN#; " Char"
'Print #1 , "Col27="; PO; CONTRACT; " Char"
Print #1 , "Col28=PO# Char"
'Print #1 , "Col29="; LABOR; HOURS; " Char"
'Print #1 , "Col30="; ACT; AMOUNT; " Char"
'Print #1 , "Col31="; FRG; AMOUNT; " Char"
'Print #1 , "Col32="; BRD; AMOUNT; " Char"
'Print #1 , "Col33="; Total; AMOUNT; " Char"
'Print #1 , "Col34="; RMB; AMOUNT; " Char"
'Print #1 , "Col35="; [UTL AMOUNT]; Char; ""
Print #1 , "Col36=TYPE Char"
Print #1 , "Col37=PACKAGE Char"
Close #1 
End Sub

Thanks for the insight.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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