问题
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