[VBA] Timing Workbook.Open vs SQL Connection

skorpionkz

Well-known Member
Joined
Oct 1, 2013
Messages
1,171
Office Version
  1. 2016
Hi guys,

I curious have I done it correctly. I have lots of csv file which I am using as a database (I know it is not best way, but it is only way I can keep working on it without IT involved).
At the moment I am using Workbook Open method to open each of them and dump into array which i then converted to the classes and array is erased.
I am considering to use SQL connection rather than Workbook Open, therefore before I jump into deep water I decided to do little test. I open 1 of my files 1000 time with Workbook.Open and SQL connection. Then I check average time for it and how many times each of them was faster than the other method.
I was surprised to see that workbook open is actually faster method, but as I am not to familiar with SQL I probably made some stupid mistake. Could you guys have a look and advice if I can improve code somehow? Bonus point... when I using SQL it looks like database is loaded to the array transposed... is this how it should be?

So.. below is the Timing from the test, and then code for Workbook.Open followed by SQL

Excel 2010
ABCDEF
1SQLOPENCheck
20.3648190.307095Average of 1000 repCount
30.2343750.292969SQLSQL25
40.3515630.269531OPENOPEN970
50.3398440.265625OPENSAME6
60.3906250.308594OPEN
70.3515630.269531OPEN
80.3671880.332031OPEN
90.4726560.257813OPEN
100.3671880.449219SQL
110.5078130.308594OPEN

<tbody>
</tbody>
.........

Workbook.Open
Code:
Public Sub DataArray(ByVal myFile As String)


With Application
    .EnableCancelKey = xlDisabled
    .ScreenUpdating = False
End With


Dim wb_CSV As Workbook
Dim ws_CSV As Worksheet
Dim LC As Long, LR As Long
Dim arr_Temp As Variant


Set wb_CSV = SetWorkbook(myFile, , True)
Set ws_CSV = wb_CSV.ActiveSheet


With ws_CSV
    On Error Resume Next
        .ShowAllData
    On Error GoTo 0
    LC = .Rows(1).Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
    LR = .Cells.Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    
    arr_Temp = .Range(.Cells(1, 1), .Cells(LR, LC)).Value2
End With


wb_CSV.Close savechanges:=False




With Application
    .ScreenUpdating = True
    .EnableCancelKey = xlInterrupt
End With


End Sub


Public Function SetWorkbook(ByVal myFile As String, Optional ByVal str_Folder As String = Empty, Optional ByVal b_ReadOnly As Boolean = False) As Workbook


If str_Folder = Empty Then str_Folder = MyFolder


On Error Resume Next
Set SetWorkbook = Nothing
Set SetWorkbook = Workbooks(myFile)
    If SetWorkbook Is Nothing Then
        Workbooks.Open Filename:=str_Folder & myFile, UpdateLinks:=False, ReadOnly:=b_ReadOnly
        Set SetWorkbook = Workbooks(myFile)
    End If
On Error GoTo 0


End Function

SQL
Code:
Sub GetDataFromDatabase()


    Dim myConnection            As New ADODB.Connection
    Dim myRecordSet             As ADODB.Recordset
    
    Dim sConnectionString       As String
    Dim sSQL                    As String
    Dim iRow                    As Integer
    Dim iFieldCount             As Integer
    
    Dim vArray As Variant
       
    sSQL = "SELECT * FROM " & pbl_file_DETAILS
                           
    sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & MyFolder & _
                        ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
    
    myConnection.Open sConnectionString
    
    Set myRecordSet = myConnection.Execute(sSQL)
    
    vArray = myRecordSet.GetRows
    
    myConnection.Close
                
    Set myRecordSet = Nothing
    Set myConnection = Nothing




End Sub

Thank you guys.
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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