Question Locked

Retrieving data from SQL using Visual Basic 6

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

I need your kind assistance on how to retrieve data from SQL Server using VB6.

The connection string that I used is:(I'm not quite sure on whether my coding is correct.Kindly comment on any errors I have made.)

Public Sub Database_Connection()

Dim i As Integer
Dim strSQL As String
Dim oRS As ADODB.Recordset
Dim oConn As ADODB.Connection

oConn = New ADODB.Connection
oRS = New ADODB.Recordset

strCS = "Provider = SQL.OLEDB.1;" & _
"Integrated Security = SSP1;" & _
"Persist Security Info=False;" & _
"Initial Catalog=BookRecords;" & _
"Data Source = dweappdmy01\eportal;" & _
"UID=sa; PWD=pass;"

oConn.ConnectionString = strCS
oConn.Open

-----------------------------------------------
Below is how I'm currently doing it.Is this correct?

oRS.Recordset

.Fields("Title") = txtTitle.Text
.Fields("Pub_Year") = txtPub.Text
.Fields("Pub_ID") = txtPubID.Text
.Fields("ISBN") = txtISBN.Text
.Fields("Subject") = txtSubject.Text

Thank you.

5 replies

by dearrohit

I assume the connection is fine if your variable strCS holds the proper connection string. If you are trying to add a record using a recordset you have to open the recordset first. something like...

oRS.open oConn, "Select * From tablename where 1=2", adopenDynamic,3

Then assign values to the recordset

.Fields("Title") = txtTitle.Text....
.....

then update the recordset
oRS.Update

by dearrohit

I assume the connection is fine if your variable strCS holds the proper connection string. If you are trying to add a record using a recordset you have to open the recordset first. something like...

oRS.open oConn, "Select * From tablename where 1=2", adopenDynamic,3

Then assign values to the recordset

.Fields("Title") = txtTitle.Text....
.....

then update the recordset
oRS.Update

Let me know if this works

Regards...
Rohit

by itsmegopiin

hi this is surendran did you get answer for your question mean did you establish connection between vb to sql.if you done that one means please send that connection code to my mail id this is my iam also in need of that please reply id:itsmegopiin@yahoo.co.in

This discussion is closed to new replies.