Deleting data from Access with VBA

跟風遠走 提交于 2019-12-06 05:16:00

You were not supposed to use parentheses for the recordset.execute function, unless you want to send the execute function options, as well as your query.

currentDB.Execute ("SQL EXECUTABLE QUERY", Options)

options is an optional constant that you can include in the function, such as dbDenyWrite, or dbFailOnError. when you don't include options in your execute function, Microsoft uses the default dbInconsistent, and you should not use parentheses.

Set query = CurrentDb.Execute "DELETE * FROM tb_SOMMAIRE..."

The reason your code worked when you used the variable sql is because you did not use the parantheses when you used the variable.

You already solved the problem. Congratulations. See whether a different approach for your WHERE clause is easier to work with.

The WHERE clause checks whether VARIABLE matches a list of values. Instead of using multiple OR conditions to compare VARIABLE with each member of the list, you can simply ask the db engine whether VARIABLE is present IN the list of values.

AND
    (
        [VARIABLE] IN
            (
                'NomAdminContrats',
                'TitreAdminContrats',
                'UnitAdminContrats',
                'AdrAdminContrats',
                'VilleAdminContrats',
                'TelAdminContrats',
                'TelecAdminContrats',
                'CourrielAdminContrats'
            )
    )

Finally managed to get it working. Had to declare another var for the query for some reason.

Dim sql as String
sql = "DELETE * FROM tb_SOMMAIRE WHERE OTP = '" & txtOTP.value & "' AND (VARIABLE = 'NomAdminContrats' or VARIABLE = 'TitreAdminContrats' or VARIABLE = 'UnitAdminContrats' or VARIABLE = 'AdrAdminContrats' or  VARIABLE = 'VilleAdminContrats' or VARIABLE = 'TelAdminContrats' or VARIABLE = 'TelecAdminContrats' or VARIABLE = 'CourrielAdminContrats')"
CurrentDb.Execute sql
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!