Solved

How do I clear records from an Access table using VBA?

Posted on 2008-10-25
6
1,006 Views
Last Modified: 2013-11-27
I want to clear the records in the 'LSI_US_BookOrders' table before I run the next line of code.

DoCmd.TransferText acImportDelim, "LSI_US ImportSpec", "LSI_US_BookOrders", "f:\Shared Documents\LSI_US.txt"

I can't remember.  Searched long enough. Help.
0
Comment
Question by:c9k9h
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
jmoss111 earned 50 total points
ID: 22805468
Hi c9k9h,

dim db as dao.database
set db = currentdb
db.execute("DELETE * FROM LSI_US_BookOrders"), dbfailonerrors

Regards,

Jim
0
 

Author Comment

by:c9k9h
ID: 22805606
Thank you Jim.  I am getting a compile error.  User-defined type not defined on the dao line.  I'm working on a remote server and 'References' is greyed out so I cannot change anything.  Is there a work around that you know of?
Thanks!
Karen
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22805625
Dim dbcur As Database
   Set dbcur = DBEngine.Workspaces(0).OpenDatabase("X:\MyPath\myRemoteDB.mdb")    
    dbcur.execute("DELETE * FROM LSI_US_BookOrders"), dbfailonerrors

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 50 total points
ID: 22805660
c9k9h

Try using SQL:
(BTW, you always want some type of confirmation before deleting *all* records! Because deleted records in Access can never be recovered)

Dim bytConfirm As Byte
bytConfirm = MsgBox("Delete all records? (deleted records cannot be recovered)", vbYesNo + vbQuestion)
    If bytConfirm = vbNo Then
        Exit Sub
    End If
   
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Delete * FROM LSI_US_BookOrders "
    DoCmd.SetWarnings True
   
    MsgBox "Done deleting all from the table", vbInformation

JeffCoachman
0
 

Author Comment

by:c9k9h
ID: 22805730
Jim - still the compile error.  I've sent a note to the admin to see if I can get the access to the 'References.'  I'm sure both of you're solutions would work if it were available.

Jeff - You're absolutely right on the caution of deleting records.  I actually did have a message box.  I like your code and it works like a charm.

Thank you both - I think you both gave good posts for other readers as well.  I'm upping the points and splitting them.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22806215
;-)
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now