1

I have created a form to delete a record from a certain table. It contains a field where the user can type the ID of the record that he wants to get rid of (field name: "idp"). The deletion is validated clicking a button. Behind the button I have the following code:

Private Sub button12_Click()
Dim dbs As Database, rst As Recordset
Set dbs = CurrentDb
dbs.Execute "DELETE * FROM " _
    & "mytable WHERE ID = Me.idp;"
dbs.Close
DoCmd.Close End Sub

However, I keep getting error 3061 when trying to use it. ("error 3061 too few parameters. expected 1") I would appreciate your help, guys.

Vityata
  • 42,633
  • 8
  • 55
  • 100

1 Answers1

2

Can you try like this:

Private Sub button12_Click()

    Dim dbs As Database, rst As Recordset
    dim strCommand as String

    Set dbs = CurrentDb

    strCommand = "DELETE * FROM " & "mytable WHERE ID = " & Me.idp
    debug.print strCommand 
    dbs.Execute strCommand

    dbs.Close
    DoCmd.Close 

End Sub

I think that you were not passing the Me.idp as a parameter, but it is in the string.

In general, after running it, take a look at the immediate window. The SQL there should be executable, if you open a new query in Access.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • The line dbs.Execute "DELETE * FROM " & "mytable WHERE ID = " & Me.idp; highlights itself in red and gives a compile error. :( – Tamás Kovács Jun 28 '17 at 08:44
  • @TamásKovács - give it a try again. What is the immediate window showing? – Vityata Jun 28 '17 at 08:48
  • It's working now...there was no semicolon required at the end of the line:) – Tamás Kovács Jun 28 '17 at 08:51
  • @TamásKovács - true, I have forgotten about it in the second edit. – Vityata Jun 28 '17 at 08:52
  • 1
    It should be `Debug.print strCommand`, to see what is actually run. But +1 for using it. [How to debug dynamic SQL in VBA](http://stackoverflow.com/questions/418960/managing-and-debugging-sql-queries-in-ms-access/1099570#1099570) – Andre Jun 28 '17 at 09:09