MS Access as Database

How do I add, delete, modify data in MSAccess as database? Can I have sample codes?
GreengirlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.