Private Sub cmdImportCsvToFlexGrid_Click()
Dim sConnectionString As String
Dim oCon As ADODB.Connection
Dim oRs As ADODB.Recordset
Dim sFilePath As String
Dim sFileName As String
sFilePath = App.Path & "\"
sFileName = "Example.csv"
'Create a Schema.ini File.
'It must be in the same folder as your csv file.
'In this file, you define the columns (fields) using syntax similar to this:
'/Example.csv
'LastName;FirstName
'John;Doe
'Terry;Wortham
'Jack;McGinnis
'Mary;Copeland
'/
'/Schema.ini
'[YourFileName.csv]
'ColNameHeader = True
'CharacterSet = ANSI
'Format=Delimited(;)
'Col1="LastName" Text
'Col2="FirstName" Text
'/
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFilePath & ";" & _
"Extended Properties=""text;HDR=Yes;FMT=Delimited""" 'CSVDelimited
Set oCon = New ADODB.Connection
oCon.CursorLocation = adUseClient
oCon.Open sConnectionString
Set oRs = New ADODB.Recordset
oRs.Open "Select * From " & sFileName, oCon, adOpenStatic, adLockOptimistic, adCmdText
oRs.MoveFirst
Debug.Print oRs.RecordCount
Call Me.FillFlexGridADORs(Me.MSFlexGrid1, oRs)
End Sub
' Fill MSFlexGrid with ADO Recordset
Public Sub FillFlexGridADORs( _
oFlexGrid As MSFlexGrid, _
oRs As ADODB.Recordset _
)
Dim nRow As Long
Dim nCol As Long
With oFlexGrid
.Clear
.Redraw = False
.FixedRows = 1
.FixedCols = 1
' Set Count Columns
.Cols = oRs.Fields.Count + .FixedCols
' Grid Header
For nCol = 0 To oRs.Fields.Count - 1
.TextMatrix(0, nCol + 1) = oRs.Fields(nCol).Name
Next
' Set Count Rows
.Rows = oRs.RecordCount + .FixedRows
' Fill FlexGrid with Data
Do While Not oRs.EOF
' Row Number is Record Number
nRow = oRs.AbsolutePosition
' Record Number to Column 0
.TextMatrix(nRow, 0) = nRow
For nCol = 0 To oRs.Fields.Count - 1
If Not IsNull(oRs.Fields(nCol).Value) Then
.TextMatrix(nRow, nCol + 1) = oRs.Fields(nCol).Value
End If
Next
oRs.MoveNext
Loop
' Optimize Column Width
Call AutoSizeColumnWidthFlexGrid(oFlexGrid, 90)
.Redraw = True
End With
End Sub
' Optimize Column Width in MSFlexGrid Control
Public Sub AutoSizeColumnWidthFlexGrid( _
ByRef oFlexGrid As MSFlexGrid, _
ByVal nExtendInTwips As Long _
)
Dim nRow As Long
Dim nCol As Long
Dim nColumnWidth As Single
With oFlexGrid
For nCol = 0 To .Cols - 1
nColumnWidth = 0
For nRow = 0 To .Rows - 1
If Me.TextWidth(.TextMatrix(nRow, nCol)) > nColumnWidth Then
nColumnWidth = Me.TextWidth(.TextMatrix(nRow, nCol))
End If
Next
.ColWidth(nCol) = nColumnWidth + nExtendInTwips
Next
End With
End Sub
2/1/2012,Remodeling,Central,Painting,Support,Financials
2/15/2012,Outsource Flanks,Accounts BLD,Parade,Development,Purchases
3/8/2012,Military,Window Migrate,Suppor,Meetings,Wrehouse
3/21/2012,Steaming Front,Central,Training,Support,Financials
2/2/2012,Cost Project,Central,Install,Development,Financials
Private Sub Command1_Click()
Dim sFilePath As String
Dim sFileName As String
sFilePath = "C:\Temp\"
sFileName = "zCSVfile.txt"
Adodc1.ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & sFilePath & ";Extensions=asc,csv,tab,txt;"
Adodc1.RecordSource = "Select * from " & sFileName
'Set Me.DataGrid1.DataSource = Adodc1
Set Me.vbalGrid1.DataSource = Adodc1
End Sub
Private Sub FormatDateColumnHFlexGrid( _
ByRef oHFlexGrid As MSHFlexGrid, _
ByVal sDateFormat As String _
)
On Error GoTo Err_Handler
Dim nRow As Long
Dim nCol As Long
With oHFlexGrid
For nRow = 0 To .Rows - 1
For nCol = 0 To .Cols - 1
If IsDate(.TextMatrix(nRow, nCol)) Then
.TextMatrix(nRow, nCol) = Format(.TextMatrix(nRow, nCol), sDateFormat)
End If
Next nCol
Next nRow
End With
Exit Sub
Err_Handler:
Debug.Print "ERROR (FormatDateColumnHFlexGrid): " & Err.Description & ", " & CStr(Err.Number)
End Sub
Private Sub HighlightEntireRowHFlexGrid( _
ByRef oHFlexGrid As MSHFlexGrid _
)
On Error GoTo Err_Handler
'/ Highlight the entire row when the grid is clicked
With oHFlexGrid
.FocusRect = flexFocusNone
.SelectionMode = flexSelectionByRow
End With
With oHFlexGrid
'.Col = 1
.Col = .FixedCols
.ColSel = .Cols - 1
End With
'/
Exit Sub
Err_Handler:
Debug.Print "ERROR (HighlightEntireRowHFlexGrid): " & Err.Description & ", " & Err.Number
End Sub
' Optimize Column Width in MSHFlexGrid Control
Public Sub AutoSizeColumnWidthHFlexGrid( _
ByRef oHFlexGrid As MSHFlexGrid, _
ByVal nExtendInTwips As Long _
)
On Error GoTo Err_Handler
Dim nRow As Long
Dim nCol As Long
Dim nColumnWidth As Single
With oHFlexGrid
For nCol = 0 To .Cols - 1
nColumnWidth = 0
For nRow = 0 To .Rows - 1
If Me.TextWidth(.TextMatrix(nRow, nCol)) > nColumnWidth Then
nColumnWidth = Me.TextWidth(.TextMatrix(nRow, nCol))
End If
Next
.ColWidth(nCol) = nColumnWidth + nExtendInTwips
Next
End With
Exit Sub
Err_Handler:
Debug.Print "ERROR (AutoSizeColumnWidthHFlexGrid): " & Err.Description & ", " & CStr(Err.Number)
End Sub
Set the ADO data control connection string to something like:
Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\MyFolder\;E
Set the data control RecordSource to:
Select * from MyFile.csv
Set the DataSource for the DataGrid to the datacontrol, e.g. ADODC1
Thr grid should now be filled when the form is run