Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I populate a dataGrid from a

Status
Not open for further replies.

jsub1

Programmer
Oct 31, 2002
4
AU
How do I populate a dataGrid from a recordset in vb
 

The easiest way to get started at understanding this is to start a new project.

Select VB from the menu and when the dialog comes up select Application wizard. At some point within this wizard it will ask you if you want it connected to a database. Navigate to a database (northwind.mdb) and it will ask you what kind of view you want. Select datasheet view. There are more steps than this and its been so long I don't remember all of the steps but the application that is developed for you with this wizard will get you started. Remember that not all of the functionality is contained within the code. You will find some of it in the properties of the controls.

Good Luck

 
I just completed the same task, in Access all you make is subform, in VB you have to build it from the recordset(rs) or link the rs to the datasource(ds) property.

Here's the ds example:

Set MSFlexGrid.Datasource = rs.YourRecordSet

Replace MSFlexGrid with your grid name. You might want to use MSFlexGrid instead of the standard DataGrid, more flexibility.
----------------------
Here's the rs example:

You set of the columns.

With MSFlexGrid
.Cols =3
.Col =1
.Colwidth(.Col) = 1400
.ColAlignment(.Col) = 2
.Text = "Colname" -> not sure about this

Dim nAdded as Long
Dim rcString as String
While Not rs.EOF
rcString = vbTab & rs("f1") & vbTab & rs("f2")...
.AddItem rcString
rs.MoveNext
nAdded=nAdded+1
Wend

If nAdded>0 Then -> Chk rows added before removing last
Dim iRows as Long
iRows = .Rows -> number rows in grid not records
.RemoveItem iRows-1 -> removes the blank row
End If
End With

I hope this helps, you made need to tweak it some but you get the gist of it.
 
Thank you VB5prgrmr and TomNQ.

Your advise is much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top