954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Connectivity of SQL 2000 + VB using ODBC

steps i Follow-
1) Go to Control panel-->Admintrative Tools-->Double -click
Data sources(ODBC)-->click Add button-->I choose SQL server(last option), Is it right or wrong option for connecting to SQL 2000.Then a new screen appears Create a New dataSource-->in name textbox,we can write anyname--Right or wrong. suppose i enter sonia, & my server name is .,so I enter . in server & click next button-->On the Next screen i have checked the option With Sql server authentication-->Login Id-sa, Password-->Blank-->Click next button-->Next-->Finish-->Then I click on button Test Data source-->Mesage Comes TESTS COMPLETED SUCCESSFULLY-->OK-->OK-->Ok.

These steps we have to do or not for Connectivity using ODBC.
If yes,then what after these steps we have to do??Plz reply me early ??Thx in advance.

Plz tell me the coding also for insertion of textbox values into the textbox.

Yogesh Sharma
Light Poster
43 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

the steps you followed for creating the DSN are absolutely correct. but there is no need to create that. it is always recommended that you use a DSN-Less connection. It becomes more faster.

ok...here is a sample connection snippet for you. please note that this code uses DSN-Less connection.
just goto Project->References and add Microsoft Activex Data Objects Library into your project.

<strong>''code for making a connection with the sql server database</strong>
Private Sub Form_Load()
Dim gcn as New ADODB.Connection

gcn.connectionstring="Provider=SQLOLEDB.1;Persist Security Info=False;<strong>User ID=bs</strong>;<strong>Initial Catalog=BILLING_SYSTEM</strong>"
gcn.open
msgbox "A connection to the database is now established"
End Sub

<strong>''code for retrieving some rows from a table</strong>
Private sub Command1_Click()
Dim rs as New ADODB.Recordset
Dim str as String
Dim li as ListItem

str="select * from branch_details order by branch_id"
rs.open str,gcn,1,2

if rs.Recordcount>0 then
   rs.MoveFirst
   While not rs.EOF()
      with Listview1
         set li=.listitems.add(,,(branch_id))
         li.subitems(1)=rs!branch_name
         li.subitems(2)=rs!address
      end with
      rs.MoveNext
   Wend   
else
   msgbox "No record found." & vbcrlf & "Please add some records."
end if

if rs.State=adStateOpen then rs.close
set rs=Nothing
End Sub


look for theBolded Parts in the connection string. the first one should be replaced by yours database user name and the second one is by your original database name.

for inserting values from textbox to your sql server database use the following code :-

Dim rs as New ADODB.Recordset

if rs.state=adStateopen then rs.close
rs.open "select * from <strong>branch_details</strong>",gcn,1,2
rs.AddNew
rs!branch_id=txtID.text
rs!branch_name=trim(txtName.text)
rs!address=trim(txtAddress.text)
rs.Update
if rs.state=adStateopen then rs.close
set rs=nothing

Msgbox "New record added."


look into theBolded Part. it must be replaced by your table name where you wish save your records.

hope this helps you.
try this and give me a feedback.

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

hey tell me dat ADODC is always used on all the form,if we deal with the database.

Yogesh Sharma
Light Poster
43 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

not compulsorily.
but you can use the ADODC control.
but it is not recommended. always use ADODB objects to communicate with your databases.

in the preceding code i gave you, you don't require to have any ADODC control on your form.

ok...
tell me if any more problem arises...

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

There is so such control in the Components... Microsoft Active X Plug in ..Is it same as which u r saying?/I m working on 6.0.

Yogesh Sharma
Light Poster
43 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

you don't require that control you mentioned.

infact, you don't require any data controls from your components list to work with this code. for listview control add this ,
Microsoft Windows Common Control 6.0

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 
There is so such control in the Components... Microsoft Active X Plug in ..Is it same as which u r saying?/I m working on 6.0.

absolutely no.
you are in wrong place.

look at your menu bar inside your vb6 IDE. you will find a menu calledProject.
inside it there is a command References... goto there,scroll down and add the reference
Microsoft Activex DataObjects ... Library

choose the highest version.

ok....

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

But nothing is added in the toolbox to which I can Drop onto the form,after doing this. As when we add ADO,it is added in the toolbox.

Yogesh Sharma
Light Poster
43 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

listen carefully my frnd,
Microsoft Activex Data Objects is a reference not a component or control. so it won't be added into the toolbox. each control you use on your vb6 form adds a reference to your project. now if you add an ADODC control on your form it will add the same ref. on your project too. to see this, just add an ADODC control from your component list and now go to Project->References. there you will see Microsoft Activex Data Objects has been added to your project reference list.

i'm telling you again plz read previous replies ten times before you post a reply.
in my previous replies several times i have said that this code doesn't require any control/component to be added on your form. it is using a reference and for that adding MS ADO is enough.

look into the code very carefully. there is a statement Dim gcn as New ADODB.Connection. this is a global variable which is nothing but an instance of ADODB Connection object. so to use this vars or accessing ADO methods/functions inclusion of this reference is very much important. otherwise you will face compilation errors.

hope you'll understand and approach to right direction this time.

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

Hey choudhary,
Names I have mentioned below are all used to connect to VB or not??
ADO,RDO,ODBC,DAO,OLE DB????
AnyMore methods are there to connect to VB??
Plz just give the names as above.

To connect to ODBC--we require DSN Connection. Right or Wrong?
To connect to OLEDB--we reruire OPEN Connection. Right or Wrong?

Can u plz give me just one-line short descriptions abt the others.

Second thing,The coding given by you to yogesh as scrap,In that method we are connecting using which method???
is that ADO or RDO or DAO.

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

Hi Sonia, first of all this is a personal request from me to you....don't call me like Hey choudhary....it becomes better if you use Hey Shouvik....ok..

now find your answers inline.

Hey choudhary, Names I have mentioned below are all used to connect to VB or not?? ADO,RDO,ODBC,DAO,OLE DB????

Yes, you are quite right. Actually there are physically three connection types available and those are :- RDO,DAO,ADO. The ADO/ODBC/OLEDB refers to same thing which is ADO (Activex Data Objects).

DAO (Data Access Objects) is used to connect to local databases such as an ms-access database.

RDO (Remote Database Objects) was used to connect to remote database objects. this sounds too spooky. actually remote database means connecting to a database which resides on some other server/node/machine other than the machine from where the vb6 application is running but both are in the same network. this was used in LOCAL AREA NETWORK protocols. it had so many limitations. for this microsoft abandoned further improvements on this technology and it was gone. after this ADO comes into the picture which overcomes all limitations of RDO along with DAO and has more features. using ADO you can connect to any databases regardless of its locations such that it can reside in a server node or in the local machine.AnyMore methods are there to connect to VB??
Plz just give the names as above.

No, there are no such connections method available other than these three.
I think the previous line has its answer.To connect to ODBC--we require DSN Connection. Right or Wrong?
To connect to OLEDB--we reruire OPEN Connection. Right or Wrong?

yes, to connect an ODBC data source a DSN connection is required. In ODBC connection, you don't require any middle tier control for linking a connection from your vb6 application to the database. just adding the referenceMicrosoft Activex DataObjects will do the job. after creating the connection use the ADODB objects for data interaction.

There is no connection type "OPEN". "Open" is a method of ADO connection object which creates an active connection to the database with the parameters passed in the connection string. In case of OLEDB, using DSN connection is optional. you can also use a DSN-Less connection . In OLEDB, if you want to use a DSN-Less connection you can use the ADODC control (Microsoft ADO DataControl (OLEDB).

Can u plz give me just one-line short descriptions abt the others.

As RDO is not used today any more, further discussion on it becomes absurd. so, i'm skipping it.
Now if you use DAO technology, the coding is different. here is a sample DAO code for you. this is for opening a connection and referencing a table as a recordset object. Now you are familier with VB.Net and might you have used Dataset in your vb.net coding. In vb6 we have Recordset which performs like Dataset in vb.net. only diff. lies in their connection behaviour. as dataset can work on disconnected environment, recordset works on connected environment only.

for this code you need to add the following reference :-Microsoft DAO Objects Library

Dim db as Database
Dim rs as Recordset

set db=OpenDatabase("c:\mydatabase.mdb", False, False, ";pwd=mydatabasepassword")
set rs=db.OpenRecordset("mytablename",dbOpenTable)

if rs.RecordCount>0 then
   rs.MoveFirst
else
   Msgbox "Please add some records..."
end if
Second thing,The coding given by you to yogesh as scrap,In that method we are connecting using which method??? is that ADO or RDO or DAO.

the method is ADO.

hope you like my answers.

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

Hey Shouvik ,I like the answers.

One thing told me Suppose I learn just one connection to connect to Vb,As there are may ways to connect to VB, In companies there is any restriction that to connect using this particular method.
Suppose as we have in ASP.Net,
Like some companies require ASP with VB,
ASP with C#,
ASP with J#.

The ADO/ODBC/OLEDB refers to same thing which is ADO (Activex Data Objects).
Secondly,told me that even they are refer to the same thing,Steps are different na to connect to the database. THirdly,ADO/ODBC/OLEDB can be connect to remote or local database.Rite??

CAN We call these ADO,DAO.....as DATA ACCESS COMPONENTS.

DAO can be used to connect to local database such as MS-Access. IT means we cannot connect with SQL using DAO.Rite??

In Vb.Net we use dataset in case Of DATAGRIDVIEW. But ib Vb why v use RECORDSET HERE.

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

ok....here are your answers...

1. well, there is nothing to say about. this is completely depend upon the company policy. but as i have seen most of the companies now use ADO techniques from vb6. in my office i'm using ADO methods. you are quite right. the company management may have some project restrictions. like in the project which i'm working on, has restrictions to use the ADO technology only.

2. not too much difference you will found in the connection techniques whether you use ADO/ODBC/OLEDB. the diff. can be while you connecting to the database but the rest of the methods/properties for further database manipulations will be same. now whichever method is use there is a common thing in all of these......they all use Microsoft Activex Data Objects Library.

3. yes, ADO/DAO is known as MDAC (Microsoft Data Access Components)

4. yes, 100% correct. you cannot use DAO to connect to a sql database. you have to use ADO for this purpose.

5. not only the case of datagridview. you have to use the dataset class for data manipulations (inserting,updating,deleting,searching...etc..). in vb6 we use Recordset just for this purpose. the recordset must be used here because there is no concept of dataset i visual basic 6.

hope this helps....
any more questions.....just put it right here...

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

The ADO/ODBC/OLEDB refers to same thing which is ADO (Activex Data Objects).
Secondly,told me that even they are refer to the same thing,Steps are different na to connect to the database. THirdly,ADO/ODBC/OLEDB can be connect to remote or local database.Rite??

Thx Shouvik ,I m facing so much of difficulty raegarding connections,U Sort all my probs.
THX Very Much Again.

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

answer to your first and second questions i have already given in my previous reply.
just read it carefully.

and for your third question, the answer is yes.

and one more thing, please start your own thread instead of equipping in some other's thread. this will help other people to easily get their answers having same problem like you.

hope you will accept this request.

next time i wanna see you in your own thread...
ok...you are most welcome...

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

I will.

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

INSERTION CODE PROVIDED BY YOU IN THE PREVIOUS REPLIES-Dim rs as New ADODB.Recordset

if rs.state=adStateopen then rs.close
rs.open
"select * from branch_details",gcn,1,2
rs.AddNew
rs!branch_id=txtID.text
rs!branch_name=trim(txtName.text)
rs!address=trim(txtAddress.text)
rs.Update
if rs.state=adStateopen then rs.close
set rs=nothing

Msgbox "New record added."

Hey Shouvik ,
1) In VB,there is no insert ,update,delete statements as in VB.net.

2)In Vb.net cmd.execute Nonquery() ,& in VB rs.Update does the same job here.

3) "select * from branch_details",gcn,1,2
What does the above line means ,Can u explain dat. We have to insert the records into the DataBase,Y we r selectiong it. Epecially what does 1,2 means here.

4)
rs!address=trim(txtAddress.text)
In the above line,If I m not wrong,Address is the column name in database.

5) What does rs.AddNew means.

I think so VB.net is more easy thab VB.isn't it?
:-O

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

Hi Sonia,

[B]1) In VB,there is no insert ,update,delete statements as in VB.net.

WRONG... VB6 also supports all DML Statements
Connection Object and Command Object has Execute Method, which can be any DML statement:

Dim Conn As New ADODB.Connection
Conn.Open <My Connection String>
Conn.Execute "Insert Into MyTable Values (1,'ABCD')"
2)In Vb.net cmd.execute Nonquery() ,& in VB rs.Update does the same job here.


yes ..3) "select * from branch_details",gcn,1,2
they are Cursor Type and LockType respectively

What does the above line means ,Can u explain dat. We have to insert the records into the DataBase,Y we r selectiong it. Epecially what does 1,2 means here.5) What does rs.AddNew means.

Telling Recordset that you are adding new Record..I think so VB.net is more easy thab VB.isn't it?No... Not at all
Apart from executing Insert SQL Statement, have you Tried Adding a Record with DataAdpter , DataTable and DataRow in VB.net....?


Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Madam Sonia,
I think Veena has already given all of your answers. So there is nothing need to say any more from my side except these,

3) "select * from branch_details",gcn,1,2 Can u explain dat. We have to insert the records into the DataBase,Y we r selectiong it.

in the above statement we are not selecting anything from the database. a connection/reference to the table has just been established or you can say opened so that the recordset object which is "rs" here recognize/know where to send the data when the .update method fires. here we are just setting the destination for the recordset object.4)
rs!address=trim(txtAddress.text)
In the above line,If I m not wrong,Address is the column name in database.

yes, you are correct. Address is the column name. This is just one type of syntax used here. Instead of this you can also use this,rs("Address") or this rs(2)-->here 2 is the index/subscript value of the address field in the rs recordset.

Apart from this, if you still have any more questions then feel free to post here.

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

I have tried the foll. code- But it contains errors.plz help me out to sort it & Reply--

Private Sub Form_Load()
Dim gcn As New ADODB.Connection
gcn.Open = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
Dim query As String


query = "Insert into stuinfo values([roll],[name],[marks]), &_"
values("& me.txtroll.Text &","& me.txtname.Text &","&me.txtmarks.Text &")
gcn.Execute (query)
MsgBox "Record Inserted"
gcn.Close
End Sub

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You