Results 1 to 10 of 10
  1. #1
    civl_eng is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    41

    Delete query fields

    hi all, i wrote a code for delete all records of a query on run program ,but it doesn't work, which part of this code is inaccurate?? can anyone correct it ?


    Public Function del()


    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim qdf As DAO.QueryDef

    Set db = CurrentDb

    Set qdf = db.QueryDefs("qry_Students")

    Set rst = qdf.OpenRecordset()


    rst.Fields.Delete
    rst.Close

    Set rst = Nothing
    Set qdf = Nothing
    Set db = Nothing

    End Function



    Thanks in Advanced.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Normally, you Delete records from Tables. Please tell us more of your situation.

    Code:
    Delete FROM TableX  WHERE some criteria
    Be aware that unintended/inaccurate DELETE queries can cause a lot of damage quickly.

    Also," but it doesn't work ", does not help readers to understand your post.
    It prompts questions:
    -did you get an error number
    -do you understand the error message
    -could you please post the error number and message...

  3. #3
    civl_eng is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    41
    hi thank you very much for help me. i done with your code.

    in my code i get this error : Compile error, argument not optional

    can you correct my code ??

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    I don't understand what you are trying to do.
    It seems you are trying to remove fields from a queryDef, but why??

    If you want to redesign a query, then use the query grid/interface and remove some fields.

    If you are trying to Delete records in a table, then review the link in my post #2.

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If "qry_Students" a delete query?
    If so, why not just run it? Why is VBA required?

    If not, please explain in detail exactly what it is you are trying to do (and why you are trying to do it that way).

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Again, your code is attempting to delete fields which is changing a query design. Why would you want to programmatically change design? What are you really trying to accomplish?
    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.

  7. #7
    civl_eng is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    41
    hello, thanks all for reply and sorry for the delay in my reply , i create a query from table by date criteria and i put that function to on load event of form to delete records in query(by date criteria ) automatically,,,,,, in fact i want to delete some record in specific date(i created them by query and they must be deleted based on specific date and i do it with above function to delete them each load program automatically)

    thanks again to all, i accept any other advice

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Advice is to use a DELETE action, not modify query with QueryDefs.

    Normally DELETE is done directly on table but if the query is updatable then records can be deleted via query reference as well.
    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
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    civl_eng,

    Data (records) reside in Tables.
    You do not necessarily write vba code nor functions to delete data in a query.
    You DELETE records from a Table.
    You can use a query to --Select or Add or Update or Delete ---RECORD(s) in a Table.

    Can you tell us in simple English --no database jargon -- what you have and what you are trying to do?

    Good luck.

  10. #10
    civl_eng is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    41
    thanks guys, my problem is resolved.

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

Similar Threads

  1. Delete query to delete records that meet criteria
    By neill_long in forum Queries
    Replies: 1
    Last Post: 06-11-2018, 02:41 PM
  2. Replies: 7
    Last Post: 09-16-2017, 04:01 PM
  3. Delete Button vs. Delete Query
    By JennyL in forum Access
    Replies: 4
    Last Post: 02-11-2017, 10:11 PM
  4. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  5. Replies: 1
    Last Post: 05-21-2010, 02:22 PM

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