Connecting To A Database Using Visual Basic

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 08, 2009, 10:57 AM
423564 members and 291107 Topics
Latest Member: Adavize41
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  Connecting To A Database Using Visual Basic
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Connecting To A Database Using Visual Basic  (Read 10351 views)
Zule (m)
Connecting To A Database Using Visual Basic
« on: May 30, 2006, 04:16 PM »

Please, can anyone help me by giving out a complete tutorial on the step by step way a connecting to a database using VB?
smartsoft (m)
Re: Connecting To A Database Using Vb
« #1 on: May 31, 2006, 12:04 PM »

Create a datasource name for your Access database

Set db = DBEngine.Workspaces(0).OpenDatabase("dbsourcename", ,"DSN=dbsourcename;UID=MyUID;PWD=Mypassw")
============================================================
This is for SQL SERVER.

You can do that in the same way it is done in VB6 or any other languages.
Here is a quick sample:

NOTE: in Excel in your VB6 code editor go to Tools >> References and select MS ADO 2.x library before running this code

visual basic code:--------------------------------------------------------------------------------
Option Explicit

Dim adoConn As ADODB.Connection
Dim strConString As String

Private Sub Worksheet_Activate()
'================================
Dim adoRST As ADODB.Recordset
Dim strSQL As String

    strConString = "Provider=SQLOLEDB.1;Initial Catalog=smartsoft;" _
                    & "User ID=sa;Password=password;Persist Security Info=True"

    Set adoConn = New ADODB.Connection
    adoConn.Open strConString
   
    Set adoRST = New ADODB.Recordset
    strSQL = "Select * from smartsoft"
   
    adoRST.Open strSQL, adoConn
   
    Do While Not adoRST.EOF
        'populate your cells here
    Loop

End Sub
parosky (m)
Re: Connecting To A Database Using Vb
« #2 on: May 31, 2006, 12:16 PM »

There are various ways by which you canconnect to a DB. If you are new to VB database programming, I will advise you to use Active X Data Object(ADO). There are 2 means of achieving this.
1. Through a ADO data control (Adodc). Place the adodc on your form and click the ellipses next to the connectionstring property. On the Provider tab of the window shown, select your data provider, e.g select Microsoft Jet OLEDB 3.51(or 4.0) depending on the version of microsoft access you are using, that is if you are connecting to microsoft Acess. On the connection tab, use brose to navigate to the path of your database (the access file*.mdb) This is the simplest you need to do, then click on Test Connection button to see you are connected to your database). Then set the Record source properties to indicatewhether you recordset will be from a table, stored procedure or a sql command.  You can then bind controls to displayvalue of each field that will be exposed by your recordset. NB Adodc is not in your tool box by default. You need to add it ( click project>> components then select Microsoft ADO DataControl 6.0 (OLEDB). then click apply button and OK. This show the adodc in your toolbox.

2. Similar to the first but gives you a whole lot of control over your data is to program directly against the ADO Object. You will not need to use the Adodc but you sould set a reference to an ADO object. Click Project >>Reference.
Then
(i) Declare an object variable whose type is ADODB.Connection in the general section of you code or module e.g Dim myConn as
  ADODB.Connection
.  then create the object by writing Set myConn = new ADODB.Connection
ii)Set the Connection object’s ConnectString property to reflect a valid OLE DB provider, or specify the provider string in the next step, as follows:
myConn .ConnectString = "Provider=Microsoft.Jet.OLEDB.3.51;"
iii)Call the Connection object’s Open method. If you didn’t specify the ConnectString property in the preceding step, you need to pass the connection string as the first argument to the Open method, as in the following example. Better still reproduce the following format ( to reduce my typing explanations actually)
dim dbPath as string
myConn.Provider=“Provider=Microsoft.Jet.OLEDB.3.51;”
dbPath="c:\my documents\myDB.mdb"
myConn.Open dbPath

This connect you to the database against which you can now give query to retrieve your recordset. But I feel you are new to database programming. You can ask some more question to let me know I've been helpful and to tell me area where you need more help. I'll check back tommorrow
Zule (m)
Re: Connecting To A Database Using Vb
« #3 on: June 07, 2006, 05:55 PM »

Quote from: parosky on May 31, 2006, 12:16 PM
There are various ways by which you canconnect to a DB. If you are new to VB database programming, I will advise you to use Active X Data Object(ADO). There are 2 means of achieving this.
1. Through a ADO data control (Adodc). Place the adodc on your form and click the ellipses next to the connectionstring property. On the Provider tab of the window shown, select your data provider, e.g select Microsoft Jet OLEDB 3.51(or 4.0) depending on the version of microsoft access you are using, that is if you are connecting to microsoft Acess. On the connection tab, use brose to navigate to the path of your database (the access file*.mdb) This is the simplest you need to do, then click on Test Connection button to see you are connected to your database). Then set the Record source properties to indicatewhether you recordset will be from a table, stored procedure or a sql command.  You can then bind controls to displayvalue of each field that will be exposed by your recordset. NB Adodc is not in your tool box by default. You need to add it ( click project>> components then select Microsoft ADO DataControl 6.0 (OLEDB). then click apply button and OK. This show the adodc in your toolbox.

2. Similar to the first but gives you a whole lot of control over your data is to program directly against the ADO Object. You will not need to use the Adodc but you sould set a reference to an ADO object. Click Project >>Reference.
Then
(i) Declare an object variable whose type is ADODB.Connection in the general section of you code or module e.g Dim myConn as
  ADODB.Connection
.  then create the object by writing Set myConn = new ADODB.Connection
ii)Set the Connection object’s ConnectString property to reflect a valid OLE DB provider, or specify the provider string in the next step, as follows:
myConn .ConnectString = "Provider=Microsoft.Jet.OLEDB.3.51;"
iii)Call the Connection object’s Open method. If you didn’t specify the ConnectString property in the preceding step, you need to pass the connection string as the first argument to the Open method, as in the following example. Better still reproduce the following format ( to reduce my typing explanations actually)
dim dbPath as string
myConn.Provider=“Provider=Microsoft.Jet.OLEDB.3.51;”
dbPath="c:\my documents\myDB.mdb"
myConn.Open dbPath

This connect you to the database against which you can now give query to retrieve your recordset. But I feel you are new to database programming. You can ask some more question to let me know I've been helpful and to tell me area where you need more help. I'll check back tommorrow
sorry, for not replying as at when due it was owing to the fact that i've been very very busy with my school.
As you said i'm a newbie vb. and i will also like to know if u have  any e-book on vb
do u?
IF reply= true   then

pls, send it to my box

Else

End If
once again i would like to thank u for doing me that favour

parosky (m)
Re: Connecting To A Database Using Vb
« #4 on: June 09, 2006, 10:01 PM »

You don't seem like a newbie cos I've seen some of your contributions. I'm still looking for your email adddress so I can attach one to you or through this site,  I'm not browsing on my system now. Take care.
Zule (m)
Re: Connecting To A Database Using Vb
« #5 on: June 12, 2006, 07:06 PM »

Quote
Posted on: June 09, 2006, 10:01:09 PMPosted by: parosky 
Insert Quote
You don't seem like a newbie because I've seen some of your contributions. I'm still looking for your email adddress 

@  parosky   your right in what u said but one must always lets him self down in ohter to learn from the people above him
any way this is mail email address i am waiting for the e-books to be farwarded thanks
parosky (m)
Re: Connecting To A Database Using Vb
« #6 on: June 16, 2006, 02:05 PM »

Quote from: Zule on June 12, 2006, 07:06 PM
@ parosky your right in what u said but one must always lets him self down in ohter to learn from the people above him

Well nobody knows it all. In a forum like this what you gain will depend on what people percieve as the level you are. If you make people believe you are totally new they will be giving you low stuffs. Let people know where you are so that the vibrations will be at a level that will benefit you, not that we'll be telling a guru who is being "humble" about variable declaration.
I'm still looking for your email address.
Zule (m)
Re: Connecting To A Database Using Vb
« #7 on: June 17, 2006, 03:12 PM »

ok,
Quote from: parosky on June 16, 2006, 02:05 PM
Well nobody knows it all. In a forum like this what you gain will depend on what people percieve as the level you are. If you make people believe you are totally new they will be giving you low stuffs. Let people know where you are so that the vibrations will be at a level that will benefit you, not that we'll be telling a guru who is being "humble" about variable declaration.
I'm still looking for your email address.
@parosky  my email address is( zule_jojo at yahoo dot co dot uk)
larryshow
Re: Connecting To A Database Using Vb
« #8 on: July 18, 2006, 09:03 AM »

hellooo seniors, though am new here but yet i will like to contribute .  i hv been looking toward an opportunity like this where by nigerians will be able to express and share ideals. its good to hnow something like this exist my contribution is attached.

vb.doc
* vb.doc (23.5 KB - downloaded )
parosky (m)
Re: Connecting To A Database Using Vb
« #9 on: July 18, 2006, 10:07 AM »

Larryshow
I was only able to download a blank word document. May be you'll help attach it again or just copy and past it directly. At times opening attachment is a distraction to some people
dag (m)
Re: Connecting To A Database Using Vb
« #10 on: September 01, 2006, 02:53 PM »

hi people
the lot posted so far is a good primer for database connectivity, but going up from here to where the technology is heading u really have to understand what microsoft wants u to know. OOP is hoopla of the developers world and all that has been said is an implementation of certain classes represented in the ADO Object model. so its all in the model, and the model for ADO is smply
1.connection object
2.command object
3.recordset object

all conn to a database wld basically obey this model, like it has been correctly outlined in earlier postings, if u look carefully ull observe the truth about the model. so just get the model into your head and your good to go, study the properties and methods exposed by these objects and ull better understand what they do best and get intouch with oop its the koko for developers.
femi_ko (m)
Re: Connecting To A Database Using Visual Basic
« #11 on: March 11, 2008, 03:04 PM »

hope u got the problem solved
 Project Topics In Computer Science  Top Earners in the Nigerian Software Industry  Post Ur Vb 6.0 Questions Here  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.