Solved

Populate VB DataGrid from Recordset

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

707 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

12 Experts available now in Live!

Get 1:1 Help Now