Solved

Populate VB DataGrid from Recordset

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 37
Why Delete * statement wont work with sql server ? 6 42
SQL Count issue 24 16
Proper Case SQL Command 2 10
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

810 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