Results 1 to 5 of 5
  1. #1
    f15e is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2016
    Posts
    75

    Need Help with DELETE statement

    I don't exactly know where I'm going wrong but I have a textbox where a user enters a value and that value is used to query my table to delete all records that don't match the entered value but it doesn't work.


    User enters text into a 'myTxtBx' textbox and clicks a button to remove all records that are not equal to the 'myTxtBx' value. These are just random names I'm giving for this example.
    See code below:
    CurrentDb.Execute "DELETE FROM [MyTable] " _
    & "WHERE [fieldName] <> '" & Me.myTextBox & "'"

    This just doesn't work. When I run the code, all records in the table are deleted. The thing is that when using a combo box, it works perfectly but not when using a textbox.
    Here is an example of using a combo box to delete records based on the user entered text value. This works with no issues. I don't know why the textbox control doesn't work. It would seem that this should work and the only difference is that a textbox is being used to get the value for use in query instead of a combo box.
    CurrentDb.Execute "DELETE FROM [MyTable] " _
    & "WHERE [fieldName]<> '" & Me.myCBO & "'"

    I'd really appreciate any help you could provide. Thanks!!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Have you tried making a query (not SQL)?
    the query looks at the form as criteria.
    Select * from table where [field]<> forms!myForm!cboBox

    try a select query to pull the items.
    if that works,then make it a delete query.
    docmd.openquery "qdDeleteQry"

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    If it works as a combo, perhaps the bound field isn't the text you see, but an ID? What is the row source of the combo?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Try
    Code:
    CurrentDb.Execute "DELETE * FROM [MyTable] " _
    & "WHERE [fieldName] <> '" & Me.myTextBox & "'"

  5. #5
    f15e is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2016
    Posts
    75
    Sorry for my delay in response. Didn't have access to a computer for a while. I will try your suggestions and hope to get it worked out.

    I just want to let everyone know that I really appreciate your help and this is an excellent site for those who are new or struggling with their DB and able to have experienced users take their time to answer questions. Thanks again!

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

Similar Threads

  1. Is this kind of SQL DELETE statement valid?
    By VAer in forum Programming
    Replies: 3
    Last Post: 03-04-2017, 10:12 AM
  2. Does INTO statement delete existing table?
    By vetabz in forum Queries
    Replies: 6
    Last Post: 01-25-2017, 05:46 PM
  3. simple delete SQL statement not working
    By markjkubicki in forum Programming
    Replies: 9
    Last Post: 05-22-2013, 05:49 PM
  4. Delete fileds in tables by using sql statement
    By BorisGomel in forum Access
    Replies: 4
    Last Post: 11-14-2012, 04:38 PM
  5. Delete SQL statement not working properly
    By Alexandre Cote in forum Programming
    Replies: 3
    Last Post: 10-18-2010, 12:56 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