Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Help with datagrid


Message #1 by jleung@m... on Thu, 6 Mar 2003 10:16:53 -0600
>From: jleung@m...
>Date: Thu, 6 Mar 2003 10:16:53 -0600
>
>Is that possible to read data from a text file and display into datagrid
>control?
>
>e.g. text file format
>
>Order#      Amount      Date
>123   100   03/04/03
>124   200   03/05/03
>125   300   03/06/03

You can use ADO. Here's an example which displays the text file 
"Customers_CSV.txt" which is located in the "d:\SourceDirectory" subdirectory.

This assumes that the "Customers_CSV.txt" file is in CSV format. If your 
text file uses Tabs to separate the columns (as your sample above seems to 
indicate) then the subdirectory in which your text file resides must 
contain a (text) file called "SCHEMA.INI" with the following entry:

          [NameOfYourFile.txt]
          Format = TabDelimited


'-------------------------------------------------
Dim cSQL As String
Dim oRs As ADODB.Recordset
Set oRs = New ADODB.Recordset
With oRs
  .CursorLocation = adUseClient
  .ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                       "Data Source= d:\SourceDirectory;" & _
                       "Extended Properties=Text"

  cSQL = "SELECT ContactName, CompanyName, PostalCode, Country " & _
          "FROM  Customers_CSV.txt " & _
          "ORDER BY ContactName"

   .Open cSQL, , adOpenStatic, adLockReadOnly, adCmdText

   Set DataGrid1.DataSource = oRs

' Save resources/disconnect the recordset:
   Set .ActiveConnection = Nothing

End With
'-------------------------------------------------

For a wealth of information about conversion between various data formats 
(Access, Excel, Paradox, dBASE, Text and HTML) and the secrets of the 
SCHEMA.INI, download this utility:

http://www.carpet-sharks.com/ZipArch/ADOFileConversions.zip



Regards,
-Toby



  Return to Index