Solved

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

Posted on 2008-10-25
6
1,044 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 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