473,770 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access with ADODB

75 New Member
Hi all,

This is how you can connect to an Access Database using ADODB..
First add a reference to Microsoft ActiveX Data Objects 2.0 Library

Now to lets Declare some variables :
Expand|Select|Wrap|Line Numbers
  1. Private Con As New ADODB.Connection
  2. Private Rec As New ADODB.Recordset
* You can set them to Public too if you want to access the Connection and Recordset in any other Form or Module.

Now lets Create a Connection to your Database :
Expand|Select|Wrap|Line Numbers
  1. Con.Provider = "Microsoft.jet.oledb.4.0; "
  2. Con.Open Database Path
* Provider tells the type of Database, if you are using any other type of Database instead of Access mdb use its provider which you can see in ODBC Manager in Control Panel.

After this Connection will established with the Database.

Now we have to open some Tables and have to fetch Records to your app, to do so we need a Recordset in which all the records from selected Tables are fetched.

Now the following code will fetch all Records from Table1 in MDB File :
Expand|Select|Wrap|Line Numbers
  1. Rec.Open "Select * From Table1", Con, adOpenDynamic, adLockOptimistic
* First parameter is Source
* Second parameter is Connection
* Third parameter is Open Mode means you can move forward or backward in the table, can add and delete. There are several Modes.
* Fourth parameter helps you to Lock your Database File that it should open once at a time.
* There are few more parameters but i m not discussing them here if anybody want to know can ask :)

Now lets create a sample program which will open a Database and then Load all Records from Table1 in a Recordset.

Expand|Select|Wrap|Line Numbers
  1. Private Con As New ADODB.Connection
  2. Private Rec As New ADODB.Recordset
  3.  
  4. Public Sub Create_Connection(File_Path as String)
  5.  
  6.     Set Con = New ADODB.Connection
  7.     Set Rec = New ADODB.Recordset
  8.  
  9.     Con.Provider = "Microsoft.jet.oledb.4.0; "
  10.     Con.Open File_Path
  11.  
  12. End Sub
  13.  
  14. Public Sub Load_Records(sqlQuery as String)
  15.  
  16.     Rec.Open sqlQuery, Con, adOpenDynamic, adLockOptimistic
  17.  
  18. End Sub
  19.  
  20. Public Sub Close_Connection()
  21.  
  22.     Rec.Close
  23.     Con.Close
  24.  
  25. End Sub
> How to use ?

Expand|Select|Wrap|Line Numbers
  1. Create_Connection (App.Path & "\db1.mdb")
  2. Load_Records ("Select * From Table1")
Now Connection has been established, Records has been loaded to Recordset now you can easily navigate the database using Recordset.

> How to Navigate ?
Expand|Select|Wrap|Line Numbers
  1. Rec.MoveNext
  2. Rec.MovePrevious
  3. Rec.MoveFirst
  4. Rec.MoveLast
Function Name is telling us the position of cursor in Recordset.

# if anywhere in the article, i have made any mistake please correct that.

Regards
ARUZ
Mar 29 '09 #1
2 6732
EYE4U
75 New Member
> How to Add/Update a Record ?
Lets suppose there are Three Columns in Table1.
1. ID
2. Name
3. Phone No

As we have to Add record in Table and Table has been opened in Recordset (Rec) so we will use Recordset to Add a Record.

Expand|Select|Wrap|Line Numbers
  1. Rec.AddNew 0,12
OR
Expand|Select|Wrap|Line Numbers
  1. Rec.AddNew "ID",12
First parameter is Field and second parameter is the Value.
Value based on the type of Field we have set if its Integer we have to pass an integer value if string we have to pass strings and so on for other data types.

Now this code will only create a new Record in Table but not fill other fields like Name and Phone No field will remain empty. Mean now we need to update the Record 12.

So the code will be like :
Expand|Select|Wrap|Line Numbers
  1. Rec.AddNew 0,12
  2. Rec.Update 1,"Ali"
  3. Rec.Update 2,"042-0001111"
We have done this all using Functions of Recordset. We can also add a record using SQL Query in this the code will like ;
Expand|Select|Wrap|Line Numbers
  1. sqlQuery = INSERT INTO Table1 WHERE ID = 12, Name = "Ali", Phone_No = 042-0001111"
  2. Rec.Open sqlQuery, Con, adOpenDynamic, adLockOptimistic
Now this Query will be used to update some record :
Expand|Select|Wrap|Line Numbers
  1. sqlQuery = "UPDATE Table1 Set ID=?, Name=?, Phone_No=?"
or Simply use
Expand|Select|Wrap|Line Numbers
  1. Rec.Update 'Field Name', 'Value'
> How to Delete a Record ?

To delete any record we have to methods to Delete some record.
One is to use Sql Query and Second is to use Delete Function.
We just need to call delete function in order to delete record like
Expand|Select|Wrap|Line Numbers
  1. Rec.Delete
or use this Query :
Expand|Select|Wrap|Line Numbers
  1. sqlQuery = "DELETE FROM Table1 WHERE ID = 12"
Regards
ARUZ
Mar 30 '09 #2
Colanth
1 New Member
It's an old thread, but

Private Con As New ADODB.Connectio n

is a bad idea. When you're through with an object, you should destroy it. If you instantiate it in the declaration, you can't. Declare it as

Private Con As ADODB.Connectio n

Then, in the code, instantiate it:

Set Con = New ADODB.Connectio n

Later in the code, when you're through with it, destroy it:

Set Con = Nothing

If you declare objects and don't destroy them, you're eventually going to have to reboot the computer. (Memory taken by objects isn't returned until you destroy the objects.)
Aug 19 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3082
by: chris | last post by:
Hi, I try to connect to Access with php with te code below. The database and table are ok. <?php include('../adodb/adodb.inc.php'); $db =& ADONewConnection('access'); $dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=newres.mdb;Uid=Admin;Pwd=;"; $db->Connect($dsn);
2
11709
by: Josh Strickland | last post by:
I am attempting to create an Access database which uses forms to enter data. The issue I am having is returning the query results from the Stored Procedure back in to the Access Form. tCetecM1CUST (SQL Table that contains the Customer Information) tAccountingDetail (SQL Table that contains the information in the form) frmAccountingEntry (Access form used to enter data) spGetCustomerInformation (Stored Procedure which returns data using...
1
9200
by: Andrew Arace | last post by:
I scoured the groups for some hands on code to perform the menial task of exporting table data from an Access 2000 database to Oracle database (in this case, it was oracle 8i but i'm assuming this will work for 9i and even 10g ) No one had what I needed, so I wrote it myself. I Rule. This code isn't going for efficiency, and isn't trying to be dynamic. It doesn't create the table structure in Oracle, that's up to you. (I
1
5911
by: Ray Holtz | last post by:
I have a database in Access 2003 (Access2000 file format). There are two tables that are being used: Employees and Items. It is linked by the Employee field so that one employee can have many items. I have a VB6 application that is tied into this database using an ADODB.Connection and uses two ADODB.Recordsets: rsEmployees and rsItems. I can pull data from the DB into the app with both recordsets. But I can only change data in rsItems...
0
3029
by: totierne | last post by:
comp.databases.ms-access, I want to know how to use Oracle views with session variables in Access. The parameterised views in access, are migrated to views with per session variables. The open questions: How to display a resultset
5
6702
by: josephrthomas | last post by:
hi.. i am using ASP.NET with VB.NET to connect to a MS Access database... can someone pls tell me how do i make the sql statement to insert a new record into the existing table pls?? also how do i filter data? i mean lets say i wanna filter the NAME field and get all the names starting with
0
3537
by: ASP.Confused | last post by:
The old message looked a little stale, so I am re-posting it here. Anybody have any ideas of what I could do?!? The previous responses to this question are below. If you want to look at the original question, the subject line is: ADODB.NET and "Access Denied" I have an ASP.NET page writtein in VB that uses ADODB. I just had to
2
2287
by: Can | last post by:
I have a Microsoft Access front end, SQL server backend and I am using .dll files to complete the n-tier application. How do I reference an unbound textbox (from Microsoft Access) in the .dll? In the code below it does not recognize Forms!SQL_Server_Prompt Patient_Hospital_ID -> unbound textbox in a MS Access form Select_Waiting_List -> unbound textbox in a MS Access form Select_OR_Completed -> unbound textbox in a MS Access form...
7
2424
by: gordy | last post by:
Hey all, I have a fairly simple app which goes out to the web to download data. I want to store this data in a database (1 table, ~8 fields or so). My program is written in VB.net and works fine to get the data. My question is, how do I get the data into a database? I want to use Microsoft Access. I have seen several articles on using VB.net and ADO.net or Jet to read data in, but I haven't seen anything to write data out. Can...
0
767
by: DrewYK | last post by:
I have the project that may never end in front of me. I am creating a routine that will take SpreadSheets from Excel and bring them into Access. I am not using any "DoCmd"s because the goal is for the import code to be moved to a stand alone VB app which will use the Access DB as a workspace to process the data from the spreadsheets. Quite honestly, done right this may not even require Access or Excel to be on the users machine. ...
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.