Results 1 to 9 of 9
  1. #1
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216

    Another query will not work

    This is almost the same as my previous problem. But this is a delete query. On the line Set rs = db.OpenRecordset(sqlQuery), I get error Run-Time error '3219' Invalid Operation. Follow is the entire function



    Public Function modCal_removePrebilledJob() ' remove job from tblCalendar
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sqlQuery As String

    Set db = CurrentDb
    sqlQuery = "DELETE * FROM tblCalendar WHERE fLngAProposalNo = 20250762;"
    Set rs = db.OpenRecordset(sqlQuery)

    Do While Not rs.EOF
    Debug.Print rs!fLngProposalNo
    rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing
    Set db = Nothing


    End Function

    Please help, thank you.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Can't use a DELETE SQL to set a recordset.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Seriously? How are you meant to walk a deleted recordset? rofl.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    Thanks June7, but what do I do?

  5. #5
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,818
    All you need for a Delete Query is something on these lines:-

    Code:
    DELETE tblMachine.MachineID,
    tblMachine.MachineName
    FROM
        tblMachine
    WHERE
        (((tblMachine.MachineName) = "ab111"));
    

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 11 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    If you are going to use vba, then this example will show the common use of db.execute for action queries.
    Code:
    Sub DeleteRecords()
        Dim db As DAO.Database
        Dim sql As String
    
        ' Define the SQL DELETE query
        sql = "DELETE FROM Employees WHERE Department = 'Sales';"
    
        ' Set the current database
        Set db = CurrentDb
    
        ' Execute the DELETE query
        db.Execute sql, dbFailOnError
    
        ' Confirm deletion
        MsgBox "Records deleted successfully!", vbInformation
    End Sub

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    If you want to see what you are about to delete, you can do it this way.
    Code:
    Sub DeleteTemp()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("Select * from tbltemp")
    Do While Not rs.EOF
        rs.MoveFirst
        Debug.Print rs!TempID
        rs.Delete
    Loop
    Set rs = Nothing
    Set db = Nothing
    End Sub
    However why are you debug.printing a field that was the criteria for the sql?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Quote Originally Posted by mike60smart View Post
    All you need for a Delete Query is something on these lines:-

    Code:
    DELETE tblMachine.MachineID,
    tblMachine.MachineName
    FROM
        tblMachine
    WHERE
        (((tblMachine.MachineName) = "ab111"));
    
    Serves no purpose to list fields with DELETE as entire record is deleted. The * wildcard is not even needed.
    DELETE FROM tblMachine WHERE MachineName = "ab111"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    I want to thank everyone that replied to my post. I have a lot of help and I needed it. I am having trouble with queries, as you could see. You did solve my problem.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 04-24-2022, 11:06 PM
  2. Replies: 2
    Last Post: 07-24-2017, 07:32 AM
  3. Database will not work on another computer
    By yagerlegi in forum Access
    Replies: 5
    Last Post: 04-03-2015, 09:59 PM
  4. Replies: 4
    Last Post: 12-06-2014, 08:49 PM
  5. Replies: 2
    Last Post: 03-14-2014, 11:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums