Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Cant Delete from Query in VBA
Sami Shamma 
             
3 years ago
Greetings Friends

I have this simple code that returns '3086' run-time Error "Could not delete from specified tables"

Private Sub DeleteChapBtn_Click()
    Dim S As String
    S = "DELETE* FROM ChapDeleteQ WHERE YMYear = """ & YearCombo & """"
    DoCmd.RunSQL S
End Sub

the query is simple and I can delete from it manually.
What am I doing wrong?
Sami Shamma OP  @Reply  
             
3 years ago

Richard Rost  @Reply  
          
3 years ago
If you copied that from your database, you're missing a space between the DELETE and the *. But you don't need the * with delete queries. Just say "DELETE FROM..."

Also, you may have a non-updateable recordset. Not Updateable
Richard Rost  @Reply  
          
3 years ago
You're best off ALWAYS running delete query operations (and updates) directly from TABLES, not queries.
Sami Shamma OP  @Reply  
             
3 years ago
My issue is that my WHERE condition is not on the table, hence the query. I can de-normalize the Table and store the YMyear in the ChapT Table.

This now the exact code that still gives the same error:

Private Sub DeleteBtn_Click()
    Dim S As String
    If IsNull(YearCombo) Then
        MsgBox "must enter year"
        Exit Sub
    End If
    StatusBox = ""
    If MsgBox("You are about to delete the ChapT tables. Are you sure? ", vbYesNoCancel + vbCritical) <> vbYes Then
        Exit Sub
    Else
        S = "DELETE FROM ChapDeleteQ WHERE YMYear = """ & YearCombo & """"
        DoCmd.RunSQL S
        'CurrentDb.Execute S
    End If
End Sub
Sami Shamma OP  @Reply  
             
3 years ago
The Query is updatable. I can delete manually from it.
Richard Rost  @Reply  
          
3 years ago
Yeah queries will sometimes let you get away with things that you can't in SQL alone. Access can be "smart" like that sometimes. Your join is likely what's causing the problem. Can I see a small sample of your data? Why two tables?
Sami Shamma OP  @Reply  
             
3 years ago

Richard Rost  @Reply  
          
3 years ago
I'm still having trouble understanding why you have the need for the second table. It looks like that's all information that can be derived from the date in the first table. Help me understand why you're doing it that way.
Sami Shamma OP  @Reply  
             
3 years ago
The Department operates on Year/Month reporting periods. everything is Drevin buy that, not a specific date. The date in tables is just the day the report was completed. I could have stored year and month in the table instead of an YearMonthID.
The App is finished but not yet implemented. If you have a better table design, I will rewrite the app as needed. I will post some screen shouts
Sami Shamma OP  @Reply  
             
3 years ago

Kevin Yip  @Reply  
     
3 years ago
Hi Sami, you need to join the tables in the delete query itself, so that you can choose which table to delete from.  For instance, if you join Table 1 with Table 2 and want to delete from Table1, specify DELETE FROM Table1.* in the query:

     DELETE Table1.* FROM Table1 INNER JOIN Table2 ON (Table1.field1 = Table2.field1)

In your original method, you have no way to specify which table to delete from, and that is why it doesn't work.  You can manually delete from the query because Access can make the decision for you there.  But not when you run only an SQL statement.

There are other caveats involved in doing this.  So the best way is to delete from a single table only, as it is usually recommended.  If you need to reference another table to form a criteria, you may need to work around it, such as with DLookup(), in order to keep it a single-table SQL.  E.g.:

     DELETE FROM Table1 WHERE DLookup("field1", "Table2", "ID=" & Table1.ForeignID) = SomeValue
Sami Shamma OP  @Reply  
             
3 years ago
Thank you Kevin
Every time you respond to my question, I learn something new. on top of fixing my issues.
First method worked like a charm.
I will try the DLookup method next.

Again, many thanks
Sami Shamma OP  @Reply  
             
3 years ago
this the code that is now working:

Private Sub DeleteBtn_Click()
    Dim S As String
    If IsNull(YearCombo) Then
        MsgBox "must enter year"
        Exit Sub
    End If
    StatusBox = ""
    If MsgBox("You are about to delete the ChapT tables. Are you sure? ", vbYesNoCancel + vbCritical) <> vbYes Then
        Exit Sub
    Else
        S = "DELETE ChapT.* FROM ChapT INNER JOIN S_YearMonthT ON (ChapT.YearMonthID = S_YearMonthT.YearMonthID) " & _
        " WHERE S_YearMonthT.YMYear = """ & YearCombo & """"
        DoCmd.RunSQL S
    End If
End Sub

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 1:46:49 PM. PLT: 1s