[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

MS Access as Database

How do I add, delete, modify data in MSAccess as database? Can I have sample codes?
0
Greengirl
Asked:
Greengirl
  • 2
  • 2
1 Solution
 
MoMarviCommented:
First make sure you have M$ DAO compatability Library chosen in your project references.


Second, define your database and table

Dim myDb as Database
Dim myRs as Recordset

Third, set your data access


Set myDb = DBEngine.Workspaces(0).OpenDatabase("C:\mydatabase.mdb")
Set myRs = myDb.OpenRecordset("MyTable", dbOpenTable)


To add a record    
   myRs.AddNew

To Select a record based on a specific value and then edit it

myRs.Movefirst      
   'Start at the first record
While Not myRs.EOF  
   'If were not at the end  
   If myRs!ID = gsRecordID Then
   'Check to see if the field ID = gsRecordID
      myRs.edit
   'Allow editing of the record
      myRs!FieldName = NewValue
   'Set a new value for FieldName
      myRs.Update
   'Update the recordset (Table)
      myRs.Movelast
   'Go to the end of the table
   endif
   myRs.MoveNext
   'Go to the next record
wend

If you wanted to delete the record, substitute
      myRs.Delete
     
      for

      myRs.edit
      myRs!FieldName = NewValue
      myRs.Update
   

myDb.Close
'At the end, Close the database

0
 
jwellisCommented:
What if you want to delelt all records with a table of the database?
0
 
MoMarviCommented:
myRs.Movefirst        
   'Start at the first record
While Not myRs.EOF    
   'If were not at the end    
   myRs.Delete
   delete the record
   myRs.MoveNext
   'Go to the next record
wend
0
 
jwellisCommented:
Thanks MoMarvi.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now