Solved

Populate VB DataGrid from Recordset

Posted on 2003-10-24
2
380 Views
Last Modified: 2012-05-04
Populate VB DataGrid from Recordset

Could someone please help me and complete the following sub procedure.
(I am not sure if I should ask this Q on in the VB area?)

I have a Datagrid placed on a Visual Basic Form.
When I open the form, I want the Datagrid to be populated with data from a select
query.
(Could you also please let me know which library references to use.)


Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Private Sub Form_Load()

Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;
Persist Security Info=False;Initial Catalog=Northwind;Data Source=SQLServer"
cnn.Open
Set rst = New ADODB.Recordset
.
.
.
.
Set DataGrid1.DataSource = rst

End Sub
0
Comment
Question by:DGWParker
2 Comments
 

Expert Comment

by:MCummings
ID: 9615708
You'll need a reference to the 'Microsoft ActiveX Data Objects 2.XX'

And this code

rst.open "SELECT * FROM ORDERS",cnn

And yes, this belongs in the VB areas.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 100 total points
ID: 9616141
>>I am not sure if I should ask this Q on in the VB area?<<
Yes

Actually, you need a bookmarkable recordset to be able to use in the data grid, as in:

Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;
Persist Security Info=False;Initial Catalog=Northwind;Data Source=SQLServer"
cnn.Open
Set rst = New ADODB.Recordset
With rst
    .Source = "Select * from Customers"
    Set .ActiveConnection = cnn
    .CursorType = adOpenDynamic
    .LockType = adLockReadOnly       ' Or adLockOptimistic depending on your requirements
    .CursorLocation = adUseClient
    .Open Options:=adCmdText
End With
Set DataGrid1.DataSource = rst

Anthony
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question