Skip to main content

Function to Run Delete Query

Though the runActionQuery function is enough to execute all action queries types, we can create a more specific function to run an action query for deleting records. In general, deleting records has the same method as the runActionQuery. The only difference is that we insert required parameters to delete records. Usually, deleting records in a table has general pattern of SQL statement as follows:

"DELETE * FROM " & strTableName & " WHERE " & strFieldName & "=" & varFieldValue
Thus, the function to run delete query is as follows:
Function runDeleteQuery(strTableName As String, strFieldName as String , varFieldValue As Variant)
' Parameters:
' strTableName  = String expression refer to table name.
' strFieldName  = String expression as the field name reference for deleting records
' varFieldValue    = Variant data type as the value for strFieldName field name
'                 use single quote '' for string value e.g. 'John'
'                 use hash sign ## for date value e.g. #1/1/2016#
On Error GoTo Err_Msg
  runActionQuery "DELETE * FROM " & strTableName & _
              " WHERE " & strFieldName & "=" & varFieldValue
Exit_Function:
  Exit Function
Err_Msg:
  MsgBox "Function runDeleteQuery, Error # " & str(Err.Number) & ", source: " & Err.Source & _
  Chr(13) & Err.description
  Resume Exit_Function
End Function
The runDeleteQuery has three mandatory parameters:
  1. strTableName, string data type specifies a table name
  2. strFieldName, string data type specifies a field name in a table named strTableName. This parameter is included in the criterion.
  3. varFieldValue, variant data type specifies the value for related field name. This parameter is included in the criterion.
The runDeleteQuery function must be executed after a database has been opened. Therefore, make sure that letDbsOpen has been carried out before executing runDeleteQuery function. Thus, the programming sequence is as follows:
  1. letDbsOpen
  2. runDeleteQuery

Implementation:

To perform runDeleteQuery, varFieldValue must comply with strFieldName data type. For example, for a strFieldName with its text/string data type, the varFieldValue must use '' (in between single quote, e.g. 'Accounts', 'Tokyo', etc). Another example, for a strFieldName with its date data type, the varFieldValue must use ## (in between hash sign, e.e. #12/31/2015#, #2/20/2016#, etc). If varFieldValue is not complied with strFieldName data type, a data type mismatch error occurred.

The following example open a database named daoDbs that is set by using letDbsOpen and execute a query to delete (DELETE QUERY) a record in a strTableName equal to tblPrimaryAccounts, strFieldName equal to primaryaccountCode and set its varFieldValue equal to '10009' (text data type).
letDbsOpen
runDeleteQuery "tblPrimaryAccounts", "primaryAccountCode", "'10009'"
The following example open a database named daoDbs that is set by using letDbsOpen and execute a query to delete (DELETE QUERY) a record in a strTableName equal to tblVoucherTemp, strFieldName equal to vouchId and set its varFieldValue equal to 2 (long integer data type).
letDbsOpen
runDeleteQuery "tblVoucherTemp", "vouchId", "2"
The following example open a database named daoDbs that is set by using letDbsOpen and execute a query to delete (DELETE QUERY) a record in a strTableName equal to tblVoucherTemp, strFieldName equal to vouchDate and set its varFieldValue equal to #1/25/2016# (date data type).
letDbsOpen
runDeleteQuery "tblVoucherTemp", "vouchDate", "#1/25/2016#"

Comments

Popular posts from this blog

Get Connection String to Access DAO Database

To specify the connection string to a database, we use getDbsConString function. There are, at least, two variables to be set to specify the connection string. They are strProvider and strPassword. The strProvider variable specifies database provider to be employed, for example: the provider for MS Access database is MS Access. If the connection string has a password, then the strPassword variable must also be set. The result of getDbsConString function is a string value.

Creating Master Table for Subaccounts

In our accounting database, we have two subaccounts that are part of the primary accounts. They are some sort of derivative accounts for the primary accounts. The two subaccounts may serve as profit center, cost center, organization unit, vendor, customer, tax, etc. The subaccounts are stored in two tables, one for subaccounts 1 and the other for subaccounts 2. Following are the explanation for both two tables.

Function to Get Data Type DAO

Data type is the most important property in a field. Data type determines what type of data should be stored in a field of a table and how to present the data in a report. For example, a field with Text data type stores data in a text or numerical character. For a field that has Numeric data type, this field can only stores numerical data.