Why doesn't this work??? (1 Viewer)

AlvaroCity

Registered User.
Local time
Today, 14:25
Joined
Jul 16, 2016
Messages
70
Hi there.

I'd like to use this to delete a record from a form.
I cannot use the easy way such as "DoCmd.RunCommand acCmdDeleteRecord" so there are so many different fields from other tables that this command actually works.
So I am forced to use a sql delete to do it.
Code:
    Dim sql As String
    Dim str As String
    
    str = Me.frmPedidoDetalleMat.Form!PedidoMatDetalleID
    
    If MsgBox("¿Estas seguro que quieres eliminar el Registro de Material?", vbExclamation + vbYesNo, "Eliminar Registro de Material") = vbYes Then
    
    sql = "DELETE * FROM [tblPedidoDetalleMat] WHERE [PedidoMatDetalleID] =" & str & ""
    
    DoCmd.SetWarnings (0)
    DoCmd.RunSQL sql
    DoCmd.SetWarnings (1)
    Me.Refresh
    Forms!frmPedidoMat.Form.Refresh
    Forms!frmHome.Form.Refresh
    End If

Could you tell me please if there is something wrong in here??

It pop ups an error during the DoCmd.RunSQL sql step
 

plog

Banishment Pending
Local time
Today, 08:25
Joined
May 11, 2011
Messages
11,645
1. Why an empty string at the end of your SQL statement?

2. What SQL are you actually running? Don't just look at the string and assume what it evaluates to, spit it out and see exactly what it contains.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 09:25
Joined
Oct 17, 2012
Messages
3,276
For debugging, you should put 'Debug.Print sql' on the line after the SQL statement is created. That will let you see the actual statement being executed; as plog pointed out, the value you're getting isn't necessarily the value you THINK you're getting.

While we're on that, is PedidoMatDetalleID a number (my guess is yes) or text? A common error is not including single quotes around text.

What is the specific error message you're getting? Just saying that it pops up an error is a lot like going to a doctor, telling him or her you don't feel right, and expecting him to tell you what the problem is. Details are important. :)

I would also comment out the SetWarnings for now. In fact, instead of using them at all (since you're simply trying to suppress the confirmation pop-ups), try using the following instead of DoCmd.RunSQL:
Code:
CurrentDb.Execute sql, dbFailOnError
That approach will run the SQL statement, not bother your users asking for a confirmation, and still generate an error (which can then be tracked down) if something goes wrong.
 

AlvaroCity

Registered User.
Local time
Today, 14:25
Joined
Jul 16, 2016
Messages
70
Sorry for the lack of information provided.

I thought It was because I wrongly put a coma, quotes or something.

And yes I found out what happened. Maybe it was because I spent 12 hours with this and I couldn't see that the spelling of the table was wrong :banghead:
 

Users who are viewing this thread

Top Bottom