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 Current Database

In a back-end database environment, we almost never create table or query in Access database that executed as the current application. Access database that executed as the current application, also known as front-end database, is almost never used to store a table or query. In most case, the table or query stored in the front-end database is temporary in nature, so that later will be deleted soon after no longer needed.

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.

Function to Get Maximum and Minimum Number of a Data Type

In some circumstances, we want to know what the minimum and maximum number of a data type in Access is. That is, the range of a data type. Knowing the range of a data type would inform us how to determine the field size, thus to save memory used and improve loading speed. The following table shows the data types and ranges as quoted from Access Help.