Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to SQL-server..? 1

Status
Not open for further replies.

kenjoswe

Technical User
Sep 19, 2000
327
SE
Hi all,

I'm new to VB6.0 and I would like to understand how to connect to a remote database - Pubs in SQL7.0.

I have dragged a Data control object into a form. But when I look at the properties for 'Connect' I can only choose from Access, Excel, Dbase, Text.

Where can I set the name of the ODBC-datasource?

/Kent J.

 
You are using the wrong data control. I think your example uses the Data1 control. You need to use the Adodc1 data control. Go to Project then Components and then click Microsoft ADO 6 Data Control. Then put it on your form and right click it. Under Properties you can link to a SQL Server database via a OLE DB Provider for SQL Server...
 
OK!
I have liked the Datacontrol to a table in a database.
But how can I display the recordset in a DataGrid?

/Kent J.
 
you need to add the Adodc control to the form, than in the properties select the connection string, then enter the record source, after that you can link other objects (like a combo box) to it and select which fields to display
 
Savok,
Yes, I have done that.
But was is the next step.
How can I display the the recordset in a datagrid?

/Kent J.

 
kenjoswe i have no clue :) I fill my grids in the code

first you have to open a connection to your database then load the grid. Here is a sample i use, it may not be the best way but it works for me

Dim DBConn As New ADODB.Connection
Dim DBComm As New ADODB.Command
Dim DBRec As New ADODB.Recordset
DBConn.ConnectionString = "DSN=*;UID=*;SERVER=*;PWD=*"
DBConn.Open
DBComm.ActiveConnection = DBConn
DBComm.CommandText = "Select name from table"
DBComm.CommandType = adCmdText
Set DBRec.Source = DBComm
DBRec.Open
If Not DBRec.EOF Then
i = 1
While Not DBRec.EOF
If i + 1 = grid.Rows Then
grid.Rows = grid.Rows + 1
End If
If Not IsNull(DBRec("name")) Then
grid.TextMatrix(i, 0) = DBRec("name")
End If
i = i + 1
DBRec.MoveNext
Wend
End If
DBRec.Close
DBConn.Close


I use MSFlexGrid here

Hope this helps
 
You have to set the datagrid datasource property (at design time) to adodc1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top