Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Populate VB DataGrid from Recordset

Posted on 2003-10-24
2
Medium Priority
?
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

715 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