Solved

Populate VB DataGrid from Recordset

Posted on 2003-10-24
2
372 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now