SQL Delete Record - Adds a new record!

BSJWright

Registered User.
Local time
Today, 23:16
Joined
Nov 16, 2004
Messages
13
Ok, here is my code:

Code:
Private Sub cmdRemove_Click()
On Error GoTo Err_cmdRemove_Click

Dim SQL As String
Dim stLinkCriteria As String
    
SQL = "DELETE * FROM Suppliers WHERE ((Suppliers.[Supplier Name])= '" & txtSupplierName & "') AND (Suppliers.[Supplier Code]='" & txtSupplierCode & "' );"
CurrentProject.Connection.Execute SQL
txtSupplierName = Null
txtSupplierCode = Null

Exit_cmdRemove_Click:
    Exit Sub

Err_cmdRemove_Click:
    MsgBox Err.Description
    Resume Exit_cmdRemove_Click

End Sub

I am trying to delete the supplier from a table called "Suppliers" where the Supplier Name and Code are the same as the text boxes txtSupplierName and txtSupplierCode respectively. I had to make the text box values null, otherwise the supplier was not actually deleted for some reason. The above code now works, but I now get a new "supplier" with blank Supplier Name and Supplier Code fields. Does anyone have any advice on how to stop this from happening please?
 
Why are you using text boxes?

Shouldn't you be using Combo boxes?

Not really sure what this is for..
 
BSJWright said:
I am trying to delete the supplier from a table called "Suppliers" where the Supplier Name and Code are the same as the text boxes txtSupplierName and txtSupplierCode respectively. I had to make the text box values null, otherwise the supplier was not actually deleted for some reason. The above code now works, but I now get a new "supplier" with blank Supplier Name and Supplier Code fields. Does anyone have any advice on how to stop this from happening please?

The SQL you have supplied is incorrect. You have simply used a select statement, but replaced select with delete.

DoCmd.RunSQL ("DELETE FROM table WHERE field = '" & textbox & "' AND field = '" & textbox & "'")

Try that.
 
Crilen007 said:
Why are you using text boxes?

Shouldn't you be using Combo boxes?

Not really sure what this is for..

I use a combo box on another form when I want to look at other supplier details. This form is for adding or deleting suppliers only.
 
yhgtbfk said:
The SQL you have supplied is incorrect. You have simply used a select statement, but replaced select with delete.

DoCmd.RunSQL ("DELETE FROM table WHERE field = '" & textbox & "' AND field = '" & textbox & "'")

Try that.

Hmm, this hasn't worked. The "test" suppliers are still in the table - there are no new "blank" suppliers, though!
 
Last edited:
BSJWright said:
Hmm, this hasn't worked. The "test" suppliers are still in the table - there are no new "blank" suppliers, though!
Anyone got any other ideas? The code didn't work...
 
I would suggest changing the SQL to:

Code:
SQL = "DELETE * FROM Suppliers WHERE Suppliers.[Supplier Name]= """ & Me.txtSupplierName & """ AND Suppliers.[Supplier Code]= """ & Me.txtSupplierCode & """;"

Also, rather than building SQL in code, just create a query and save it. For the criteria, you can reference the two textboxes on your form like so:

[Forms]![MyFormName]![txtSupplierName]

This will help to reduce database bloat as you will have a stored query definition rather than making and deleting one every time the code is run forcing more frequent compacting.
 

Users who are viewing this thread

Back
Top Bottom