insert CSV File into datagrid

04-17-2004, 03:39 PM

I want to insert a CSV file into a datagrid/flexgrid in vb6, much the same as when you open a CSV file in Excel, and each item is in a seperate field.

I presume this would mean I will need a data control (adodc?).
I've had a play, but not been successful!

Can anyone advise me how to do this?

04-17-2004, 04:02 PM
All you have to is open an ADO recordset.
Have a look here:,84,14,8068

Then just do
Set DataGrid1.DataSource=rst


04-18-2004, 05:12 AM
thanks, that is really helpful

I'll have a go, but its very helpful!

04-18-2004, 07:58 AM

its been a while since i've used databases with vb (and never used CSV files before) so sorry if my questions seem a little stupid!

I've looked at the site, which was great and tried to use the example code, but I can't get it to insert the data into the datagrid. using your line

Set DataGrid1.DataSource=rst

I've tried it in many places but I would presume it needs to be after the loop as finished.
I've got the data to come up in message boxes as in the example, but getting it into the grid is proving difficult!

I've made sure my datagrid was called DataGrid1 etc, so could you give me some more pointers?

Help Greatly Recieved.

TheDutch IceMan
04-18-2004, 08:33 AM
please show more code so we can advise you ! :rolleyes:

Here some text from the helpfile in VB!!

Using the DataGrid Control

The DataGrid control is a spreadsheet-like bound control that displays a series of rows and columns representing records and fields from a Recordset object. You can use the DataGrid to create an application that allows the end user to read and write to most databases. The DataGrid control can be quickly configured at design time with little or no code. When you set the DataGrid control's DataSource property at design time, the control is automatically filled and its column headers are automatically set from the data source's recordset. You can then edit the grid's columns; delete, rearrange, add column headers to, or adjust any column's width.

At run time, the DataSource can be programmatically switched to view a different table, or you can modify the query of the current database to return a different set of records.

Note The DataGrid control is code-compatible with the DBGrid control that shipped in Visual Basic 5.0 with one exception: the DataGrid control doesn't support the DBGrid notion of "unbound mode." The DBGrid control is included with Visual Basic in the Tools directory.

Possible Uses
View and edit data on a remote or local database.

Used in conjunction with another data-bound control, such as the DataList control, use the DataGrid control to display records from one table that are linked through a common field to another table displayed by the second data-bound control.
Using the Design-Time Features of the DataGrid Control
You can create a database application with the DataGrid control without writing a line of code by taking advantage of its design-time features. The following instructions outline the general steps needed to implement the DataGrid control in a typical use. For complete step-by-step instructions, see the topic "DataGrid Scenario 1: Create a Simple Database Application with the DataGrid Control."

To implement a DataGrid control at design-time

Create a Microsoft Data Link (.UDL) file for the database you wish to access. See the topic "Creating the Northwind OLE DB Data Link" for an example.

Place an ADO Data control on a form, and set the ConnectionString property to the OLE DB data source created in step 1.

In the RecordSource field of the Ado Data control, type a SQL statement that returns a recordset. For example,
Select * From MyTableName Where CustID = 12

Place a DataGrid control on a form, and set the DataSource property to the ADO Data control.

Right-click the DataGrid control and then click Retrieve Fields.

Right-click the DataGrid control and then click Edit.

Resize, delete, or add columns to the grid.

Right-click the DataGrid control and then click Properties.

Using the Property Pages dialog box, set the appropriate properties of the control to configure the grid as you wish it to appear and behave.
Changing Displayed Data at Run Time
Once you have created a grid using the design-time features, you may also wish to dynamically change the data source of the grid at run time. The general methods for accomplishing this are discussed below.

Changing the RecordSource of the DataSource
The most common method of changing displayed data is to alter the query of the DataSource. For example, if the DataGrid control uses an ADO Data control as its DataSource, rewriting the RecordSource and refreshing the ADO Data control will change the data displayed.

' The ADO Data control is connected to the Northwind database's
' Products table. The new query asks for all records which have
' the SupplierID = 12.
Dim strQuery As String
strQuery = "SELECT * FROM Suppliers WHERE SupplierID = 12"
Adodc1.RecordSource = strQuery

Changing the DataSource
At run-time you can reset the DataSource property to a different data source. For example, you may have several ADO Data controls, each connected to different databases, or set to different RecordSource properties. Simply reset the DataSource from one ADO Data control to another:

' Reset the DataSource to an ADO Data control that is connected to
' the Pubs database, using the Authors table.
Set DataGrid1.DataSource = adoPubsAuthors

Rebind the DataSource
When using the DataGrid control with a remote database such as SQLServer, it's possible that the structure of the table may become altered. For example, a field may be added to the table. In that case, you can invoke the Rebind method to recreate the grid from the new structure. Note that if you have altered the columns' layout of the grid at design-time, the DataGrid control will attempt to recreate the current layout, including any empty columns. You can, however, force the grid to reset all columns by first invoking the ClearFields method.

Returning Values from the DataGrid
Once the DataGrid is connected to a database, you may want to monitor which cell the user has clicked. Use the RowColChange event not the Click event as shown below:

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
' Print the Text, row, and column of the cell the user clicked.
Debug.Print DataGrid1.Text; DataGrid1.Row; DataGrid1.Col
End Sub

Using the CellText and CellValue Methods
The CellText and CellValue properties are useful when a column has been formatted using the NumberFormat property. The NumberFormat property changes the format of any column that contains a number without changing the format of the actual data. For example, given a grid with a column named ProductID that contains integers, the code below will cause the DataGrid to display the values in the format "P-0000." In other words, although the actual value held in the ProductID field is "3," the value displayed by the grid will be "P-0003."

Private Sub Form_Load()
DataGrid1.Columns("ProductID").NumberFormat = "P-0000"
End Sub

To return the actual value contained in the database, use the CellValue method, as shown below:

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
Debug.Print _
End Sub

Note Both the CellValue used above, and the CellText value used below, require the bookmark property as an argument to function correctly.

Conversely, if you want to return the formatted value of the field, use the CellText method:

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
Debug.Print _
End Sub

Note The CellText method above is equivalent to using the Text property of the DataGrid control.

04-18-2004, 09:37 AM
apologies! It would have seemed a good idea to show more code of what I was doing!

I've attached a file containing the code I'm using.

I'm pretty sure there's a simple explanation as to why it isn't working, but I can't seem to find it!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum