Introduction
The DBIO Access
class library (tgs_dbio_access.dll) encapsulates one class named C_DBIO_Access
. This class is a plug-in I/O layer for Access databases which includes the automatic generation of SQL statements.
Using The Class Library In A Visual Studio Project
- In Visual Studio, add the tgs_dbio_access.dll class library to a project's
References
.
- Add
Imports tgs_dbio_access
at the top of each module in which the class is consumed.
Constructors
The C_DBIO_Access
class exposes three constructor options.
Constructor Option |
Signature |
Description |
No parameters |
Empty |
Instantiate a skeleton object with all properties set to their default values. |
Dim dba As New C_DBIO_Access
|
Database path |
String |
The database path is validated and, if valid, a database connection string is built without a password. |
Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
|
Database path and password |
String, String |
The database password property is set. The database path is validated and, if valid, a database connection string is built including the password. |
Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb", _
"password")
|
Methods
The C_DBIO_Access
class exposes five methods. They are listed here in alphabetical order:
Method |
Signature |
Description |
DeleteRow |
DataTable, String |
Builds an SQL DELETE command to remove a row from an Access table. The String parameter is the WHERE expression that identifies the row (or rows) to be deleted. Updates the database and returns True if the delete is successful. False is returned should the delete fail. In the latter case, an error message is displayed that includes the exception Message property along with the complete SQL DELETE statement. |
Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
Dim dtb As New DataTable
With dba
.DatabaseTableName(True) = "Customers"
If .PopulateDataTable() Then
dtb = .FilledDataTable
If Not .DeleteRow(dtb, "CustID = '123'") Then
End If
Else
End If
End With
|
InsertRow |
DataTable, (optional) String Array, (optional) Integer Array |
Builds an SQL INSERT command to add a row to an Access table. The row being added is assumed to be the highest indexed row in the DataTable . Updates the database and returns True if the insertion is successful. False is returned should the insertion fail. In the latter case, an error message is displayed that includes the exception Message property along with the complete SQL INSERT statement.
The optional String Array contains the names of datatable columns that are not to be included in the INSERT statement. The names are case sensitive. The names in the array may be in any order.
The optional Integer Array contains the indexes (zero based) of datatable columns that are not to be included in the INSERT statement. The indexes in the array may be in any order.
One or both of the arrays may be passed. The string array is checked first. If a column name is not in the string array, the integer array is checked.
|
Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
Dim dtb As New DataTable
With dba
.SQLTop = 1
.DatabaseTableName(True) = "Customers"
If .PopulateDataTable Then
dtb = .FilledDataTable
Else
End If
End With
Dim dr As DataRow = dtb.NewRow
dr("CustID") = "456"
dr("CustName") = "Acme Products"
dt.Rows.Add(dr)
dr = Nothing
Dim skip(1) As String
skip(0) = "CustAddress"
skip(1) = "CustPhone"
With dba
If Not .InsertRow(dtb, skip) Then
End If
End With
|
PopulateDataTable |
(Optional) Boolean |
Populates the FilledDataTable property using the SQLString property (either set manually or automatically generated). If any part of the DataTable fill process fails, the FilledDataTable property is not changed. True is returned when the DataTable fill is successful; False if the process fails.
The optional Boolean parameter for DisplayError controls whether an error message is displayed when the fill process fails. The default is True . When an error message is displayed it includes the exception Message property along with the complete SQL SELECT statement. When the DisplayError parameter is False , the exception Message property is exposed via the ErrorMessage property.
The name of the filled DataTable is set to the DatabaseTableName property if that property has been set. Otherwise, the name of the Access table, from which the data was extracted, is used after being parsed from the SQLString property text.
|
UpdateRow |
DataTable, Integer, (Optional) String, (optional) String Array, (optional) Integer Array |
Builds an SQL UPDATE command to update a row in an Access table from the corresponding changed row in the DataTable . The Integer parameter is the index of the DataTable row that was changed.
The optional String parameter is the WHERE expression that identifies one or more Access table rows to be updated. Not supplying a WHERE expression causes all rows in the table to be updated.
The optional String Array contains the names of datatable columns that are not to be included in the UPDATE statement. The names are case sensitive. The names in the array may be in any order.
The optional Integer Array contains the indexes (zero based) of datatable columns that are not to be included in the UPDATE statement. The indexes in the array may be in any order.
Updates the database and returns True if the update is successful. False is returned should the update fail. In the latter case, an error message is displayed that includes the exception Message property along with the complete SQL UPDATE statement.
|
Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
Dim dtb As New DataTable
With dba
.SQLWhere = "CustID = '456'"
.DatabaseTableName(True) = "Customers"
If .PopulateDataTable Then
dtb = .FilledDataTable
Else
End If
End With
With dtb.Rows(0)
.Item("CustAddress") = "18 Hill Drive, Anywhere, USA"
.Item("CustPhone") = "111-222-5555"
End With
Dim skip(1) As String
skip(0) = "CustID"
skip(1) = "CustName"
If Not dba.UpdateRow(dtb, _
0, _
"CustID = '456', _
skip) Then
'** Code to execute when update fails
End If
|
UseCommandBuilder |
None |
Updates an Access table with transactions generated by the CommandBuilder object. Transactions are based on the FilledDataTable and SQLString properties. The SQLString property must contain exactly the same SQL query that was originally used to populate the DataTable . Returns the total number of Access table rows that were added, updated, and deleted. If the database update fails, -1 is returned, and an error message is displayed that includes the exception Message property, the complete SQLString property, and the complete database connection string. |
Dim dba As New C_DBIO_Access("C:\Database\Demo.mdb")
Dim dtb As New DataTable
With dba
.DatabaseTableName(True) = "Customers"
If .PopulateDataTable Then
dtb = .FilledDataTable
Else
End If
End With
With dtb.Rows(IndexOfRowBeingChanged)
.Item("CustAddress") = "18 Hill Drive, Anywhere, USA"
.Item("CustPhone") = "111-222-5555"
End With
With dba
.FilledDataTable = dtb
If .UseCommandBuilder < 0 Then
End If
End With
|
Properties
The C_DBIO_Access
class exposes fifteen properties. They are listed here in alphabetical order:
Property |
Variable Type |
Set Value (=) or (Parameters) |
Description |
ConnectionReady |
Boolean |
N/A |
Get the state of the database connection string. |
DatabasePassword |
N/A |
= String |
Set the password parameter for the database connection string. Setting the property to Nothing or an empty string clears the property. If the DatabasePath property is set, the database connection string is re-built to update or remove the password parameter. |
DatabasePath |
String |
= String |
Get/set the fully qualified path to the Access database. If the database path validates, the connection string is rebuilt and the ConnectionReady property is set to True . If the path to the database is not valid, the database connection string is cleared and the ConnectionReady property is set to False . |
DatabaseTableName |
String |
(Boolean ) defaults to False |
Get/set the name of the table in the Access database. If the passed table name is enclosed in brackets, the brackets are removed (the table name is enclosed in brackets whenever the name is used in an SQL statement). When a True parameter is also passed, the SQLString property is populated with an SQL SELECT * command for the table. |
ErrorMessage |
String |
N/A |
Get the exception error message generated when the PopulateDataTable method failed with the DisplayError parameter set to False . |
FilledDataTable |
DataTable |
= DataTable |
Get/set the DataTable . |
SQLDistinct |
Boolean |
= Boolean |
Get/set the flag that specifies if the DISTINCT keyword is to be included when the SQL SELECT statement is built. |
SQLOrderBy |
String |
= String |
Get/set the ORDER BY expression to be included when the SQL SELECT statement is built. When this property is not set, the ORDER BY clause is not included in the SELECT command. |
dba.SQLOrderBy = "CustID"
|
SQLOrderByAscending |
Boolean |
= Boolean |
Get/set the flag that specifies if the DataTable is to be sorted in ascending or descending sequence. When True , ASC is appended to the ORDER BY clause. DESC is appended when this property is False . |
SQLSelectList |
String |
= String |
Get/set the property holding the list of fields to include in the DataTable . When this property is not set, an asterisk is used for the list of fields in the SQL SELECT statement. |
dba.SQLSelectList = "CustID, CustName"
|
SQLString |
String |
= String |
Get/set the entire SQL command string. |
dba.SQLString = "SELECT CustID, CustName FROM Customers"
dba.DatabaseTableName() = "Customers"
|
SQLTop |
N/A |
= Integer |
Set the property limiting the number of rows included in the DataTable . The default is zero which means return all rows. The value must be greater than or equal to 0. |
SQLWhere |
String |
= String |
Get/set the WHERE expression to be included when the SQL SELECT statement is built. When this property is not set, the WHERE clause is not included in the SELECT command. |
dba.SQLWhere = "CustID = '123'"
|
TableColumnNames |
String Array |
N/A |
Get the datatable column names. Returned in an unsorted string array. |
TableNames |
String Array |
N/A |
Get the names of all tables in the database. Returned in an unsorted string array. |
History
This article totally replaces the "VB.NET Class Library: Access Table Interface" article that was posted in May, 2006.