MS-Access / Getting Started

Deleting Data from Tables

One of the last steps in the BuildApplication method is to delete data from the selected tables in the target database. For our user interface, we chose a checked list box in the form because Access doesn't have this control and we think it's cool. The checked list box control has a collection of checked items that we simply need to enumerate.

Add the following code to implement the DeleteTableData method. Again, this might throw a NullReferenceException if the target database does not exist.

private void DeleteTableData()
{
    string sql = null;
    try
    {
	foreach (string tableName in deleteDataListbox.CheckedItems)
	{
	    // create the SQL statement and execute
	    sql = string.Format("DELETE * FROM [{0}]", tableName);
	    targetDatabase.Execute(sql, Type.Missing);
	}
    }
    catch (NullReferenceException)
    {
	// set the error text
	errorText = "Delete table data failed. " +
		    "Target database does not exist.";
    }
}
[Previous] [Contents] [Next]