Using parameters to execute delete statement in vba ms access

二次信任 提交于 2019-12-25 00:42:50

问题


I want the below Delete statement to be executed when the user click the delete button.

CurrentDb.Execute "Delete from [TableName] where ColumnName is not null"

But I want the user to input the tablename and the columnName. Can you please help


回答1:


This will grab parameter from form control:

SELECT *
FROM Customers
WHERE Country = _
  [Forms]![frmSelectCountry]![txtCountry]

This will prompt user "Enter County":

SELECT *
FROM Customers
WHERE Country = _
  [Enter Country]

To prompt a user for the table name and prompt user to "Enter County":

tblName= InputBox("Please enter a name for the source table", "?")

mysql = "DELETE * FROM " & tblName _
           & "WHERE (((" & tblName & ".[City])=[Enter City]));"

You could build a form (named "frmSelectTable") with a combo box control (named "cboTable") on it listing the tables to choose from and have the code retrieve the combo box controls value.

tblName = [Forms]![frmSelectTable]![cboTable]

Incorporate with your work:

tblName = InputBox("Please enter a name for the source table", "?")
mysql = "DELETE * FROM " & tblName _
           & "WHERE ColumnName Is Not Null;"
CurrentDb.Execute mysql

Here is a tutorial that will walk you through making the form that has a combo box listing all the tables in your database: How to Get all Table Names into Combo box



来源:https://stackoverflow.com/questions/49821503/using-parameters-to-execute-delete-statement-in-vba-ms-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!