Wednesday, July 1, 2009

Creating ADODB Connection in VB


Application development in Visual Basic (VB) is quite easy. However,
thinking to connect to the database using the code in May pure hard look.
But no! After understanding the basics and a little coding, it becomes easy.
First we need to get everything ready with the reference, then you will
understand what should I do (I leave this part to you. Google here), and
finally make a connection ... Note that this article related to VB6, but
it can work for flavors.Setting Reference - We will use Microsoft ActiveX Data Objects 2.6 Library. You can find it under "Projects" -> "References". Now,
you have to get all the references that are displayed, scroll down and find "Microsoft ActiveX Data Objects 2.6 Library" and check the box beside it.
You are now ready to connect to your database.


Setting up the Module - What does the following steps, "Project" -> "Add Module" and you will have something like "Module1.bas" appears in the project. Do not panic. : P Just head to the module, and in the "Properties" window, specify the name of "conndb", which means something. : D Now, in the head by double-clicking on the module. Remember, if we want to start our form, (well technically, the object through each module), we must set "Startup Object" as a "Sub Main". Still thinking where to do so? This is found in the project Properties. You "conndb" module, place the following code snippet:

Private Sub main ()
This can be considered as the beginning of the project
here we will start our main form and open a connection to our database
End Sub


Shooting connection - we can open our database connection now.
Please note, this tutorial only uses MS Access database. But! The concept
is still the same when using the other engine data. We continue to create
more new "sub" we "conndb" module. My name as "connect_db", but you can
select the name you want.

Private sub connect_db()
end sub

Go to the top of the module (ie the first line in conndb) type:

option explicit
dim conn as new addb.connection
dim cmdrs as new adodb.command
public rstable as new adodb.recordset

Private sub connect_db()

create the database connection
with conndb

connectionString="Provider=microsoft.jet.oledb.4.0;data source
="& App.parh&"\" &
"database\database.mdb
cursorlocation=aduserclient
open the connection

end with

if conndb.state=adtateclosed then
msgbox"connection to database failed"
end
end if
open

with cmdrs
activeconnection=conndb
commandtext="set your SQL query over here"
commandtype=admdtext
end with

with rstable

cursortype=adopenstatic
cursorlocation=aduserclient
locktype=adlockoptimistic
open cmdrs
end with

end sub



Related Posts by Categories



Widget by Simran

0 comments on "Creating ADODB Connection in VB"

Add your comment. Please don't spam!
Subscribe in a Reader
:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Post a Comment

 

Hadena | Copyright © 2009 | Original Design By Deluxe Themes | Converted To Blogger By Technolizard