12 hours ago: Power and Internet are out in my office due to Hurricane Ian. Click for details.
 
Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Seminars   Templates   TechHelp   Fast Tips   Forums   Help   Contact   Join   Order   Logon  
 
Home > Forums > Access
Access Forum


Back to Access Forum
 

Delete Query in VBA Pinned    Upload Images   Link  
Chris Pardy 
23 days ago
HI, I have created a delete query that when run is working as intended :

DELETE tblEventElementIssue.ShowElementIssueID, tblEventElementIssue.EventID, tblElementIssue.ShowElementID, tblElementIssue.IssueID, tblEventElementIssue.*
FROM tblElementIssue INNER JOIN tblEventElementIssue ON tblElementIssue.ShowElementIssueID = tblEventElementIssue.ShowElementIssueID
WHERE (((tblEventElementIssue.EventID)=Forms!frmEventIssueLog!EventID) And ((tblElementIssue.ShowElementID)=Nz(DLookUp("ShowElementID","tblShowElements","ShowElement='COTG'"),"")) And ((tblElementIssue.IssueID)=Nz(DLookUp("IssueID","tblIssues","Issue='No New Issue'"),"")));


Using the SQL to VBA tool available here it translated to :

'DoCmd.SetWarnings TRUE
DoCmd.RunSQL "DELETE tblEventElementIssue.ShowElementIssueID, tblEventElementIssue.EventID,  " & _
"tblElementIssue.ShowElementID, tblElementIssue.IssueID,  " & _
"tblEventElementIssue.* " & _
"FROM tblElementIssue INNER JOIN tblEventElementIssue  " & _
"ON tblElementIssue.ShowElementIssueID = tblEventElementIssue.ShowElementIssueID " & _
"WHERE (((tblEventElementIssue.EventID)=Forms!frmEventIssueLog!EventID)  " & _
"And ((tblElementIssue.ShowElementID)=Nz(DLookUp(""ShowElementID"",""tblShowElements"",""ShowElement='COTG'""),""""))  " & _
"And ((tblElementIssue.IssueID)=Nz(DLookUp(""IssueID"",""tblIssues"",""Issue='No  " & _
"New Issue'""),"""")));"
'DoCmd.SetWarnings FALSE


However when I run this code from a button, it completes, but finds 0 records to delete where there should be 1.

Also when running from code like this.. should I replace the WHERE condition criteria Dlookup functions with variables ? I tried in the code below but got syntax errors.

Dim eID As Integer 'Event ID
Dim seID As Integer ' ShowElement ID Lookup
Dim iID As Integer ' Issue ID Lookup

eID = EventID.Value
seID = Nz(DLookup("ShowElementID", "tblShowElements", "ShowElement='COTG'"), "")
iID = Nz(DLookup("IssueID", "tblIssues", "Issue='No New Issue'"), "")

MsgBox eID & " " & seID & " " & iID ' temp debug tool to check if variables are correct

'DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE tblEventElementIssue.ShowElementIssueID, tblEventElementIssue.EventID,  " & _
    "tblElementIssue.ShowElementID, tblElementIssue.IssueID,  " & _
    "tblEventElementIssue.* " & _
    "FROM tblElementIssue INNER JOIN tblEventElementIssue  " & _
    "ON tblElementIssue.ShowElementIssueID = tblEventElementIssue.ShowElementIssueID " & _
    "WHERE (((tblEventElementIssue.EventID)=" & eID & _
    "And ((tblElementIssue.ShowElementID)=" & seID & _
    "And ((tblElementIssue.IssueID)=" & iID & _
    "New Issue'""),"""")));"
'DoCmd.SetWarnings True


Juan C Rivera
22 days ago
Just a thought but you may have extra spaces at the end looks like 2 spaces on some.  I know when I do this I get error when I forget to double check on this as for the variables try it..... but 1st make a backup. also I do have problems with single quote and sometimes it works when I change it overt to double quotes.  give it a shot and see what happends.
Add a ReplyUpload an Image


Back to Access Forum Comments
 

Start a NEW Conversation
 
Only students may post right now. Click here for more information on how you can set up an account. If you are a student, please LOG ON first.
 
Subscribe
 

Check out these other pages that may be of interest to you:

9/27/2022Report Border
9/26/2022First Day of Month
9/23/2022Before Update
9/22/2022Synchronize Subforms
9/21/2022InputBox
9/20/2022Link Master Fields
9/19/2022DateSerial
9/18/2022Sequential Annual Coding
9/16/2022Scope
9/15/2022Resize Forms
 
 

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
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
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn