From what we know already, a SQL statement is an expression that is presented to the SQL interpreter that interprets it and returns a result. ADO proposes yet another way to formulate and execute such a statement. A command is an action that is created or formulated and submitted to the database. The action can be any of the types of SQL statements and queries we have used so far. That is, it can consist of creating a database or a table. It could also consist of selecting fields or restricting the list of values from a table.
To support command, the ADO library provides a class named Command. To start a command, declare a variable of type ADODB.Command and initialize it using the New operator. Here is an example: Private Sub cmdVideosRatedR_Click() Dim cmdVideos As ADODB.Command Set cmdVideos = New ADODB.Command End Sub One of the characteristics of a command is the action it is supposed to perform. This is specified as a string and it is represented by the CommandText property of the Command object. It can be similar to any of the SQL statements we have used so far. Here is an example: Private Sub cmdVideosRatedR_Click() Dim cmdVideos As ADODB.Command Set cmdVideos = New ADODB.Command cmdVideos.CommandText = "SELECT * FROM Videos WHERE Rating='R'" End Sub Alternatively, you can use the name of a table as the text of the command. The text of a command can also be the name of a stored procedure. After setting the action, you should specify the type of command that will be performed. The types of commands are members of the CommandTypeEnum enumerator. If the command is text-based, such as a SQL statement, you can specify the type as adCmdText. Here is an example: Private Sub cmdVideosRatedR_Click() Dim cmdVideos As ADODB.Command Set cmdVideos = New ADODB.Command cmdVideos.CommandText = "SELECT * FROM Videos WHERE Rating='R'" cmdVideos.CommandType = adCmdText End Sub If the text of the command is specified as the name of a table, you can set the command type to adCmdTable. If the text of the command is represented by the name of a stored procedure, then you can specify the command type as adCmdStoredProc. After using a Command object, you should close it. To do this, you can call its Close() method. After creating a Command object, to use its action, you must execute it. To start, you should specify what connection would be used to pass it. To support this, the Command class is equipped with a property named ActiveConnection. You can assign a Connection object to this property. Here is an example: Private Sub cmdVideosRatedR_Click() Dim rstVideos As ADODB.Recordset Dim cmdVideos As ADODB.Command Set cmdVideos = New ADODB.Command cmdVideos.CommandText = "SELECT * FROM Videos WHERE Rating='R'" cmdVideos.CommandType = adCmdText cmdVideos.ActiveConnection = CurrentProject.Connection rstVideos.Close Set rstVideos = Nothing Set cmdVideos = Nothing End Sub To execute a command, you can use the Command itself, a Connection, or a Recordset objects. To execute a command using the Command object, you can call its Execute() method and assign it to a Recordset object. Here is an example: Private Sub cmdVideosRatedR_Click() Dim rstVideos As ADODB.Recordset Dim cmdVideos As ADODB.Command Set cmdVideos = New ADODB.Command cmdVideos.CommandText = "SELECT * FROM Videos WHERE Rating='R'" cmdVideos.CommandType = adCmdText cmdVideos.ActiveConnection = CurrentProject.Connection Set rstVideos = cmdVideos.Execute rstVideos.Close Set rstVideos = Nothing Set cmdVideos = Nothing End Sub Alternatively, you can create a command as a text-based SQL statement and pass it as the Source argument to the Open() method of the Recordset.
We know that, after sorting or filtering records on a table, we could save the results of the table and the table would remember its previous state the next time it's opened. The most formal way to create and save sorting or filtering as an object is through a query. A query is saved as a regular object like a table or a form. The concept of queries is mostly used by Microsoft Access. Other database environments or libraries don't necessarily use queries as objects. For example, ADO allows you to create a list that is either sorted, filtered or both and save it as a formal object. This is referred to as a view. A view is a technique of creating a list of records based on data from an existing table, a query, or another view. Microsoft Access doesn't have its own means of creating a view. Instead, you can create it as part of an ADO database. To create a view, you can start with the following SQL syntax: CREATE VIEW ViewName AS SELECT Statement The creation of a view starts with the CREATE VIEW expression followed by a name. The name of a view follows the rules and suggestions we have applied to other objects so far. After the name of the view, use the AS keyword to indicate that you are ready to define the view. Because a view is like a query, it can be defined using a SELECT statement, following the same rules we applied for data sorting or filtering. Here is an example that creates a view: Private Sub cmdCreateRegistration_Click() Dim conDatabase As ADODB.Connection Dim SQL As String Set conDatabase = Application.CurrentProject.Connection SQL = "CREATE VIEW StudentsIdentification " & _ "AS SELECT FirstName, LastName FROM Students" conDatabase.Execute SQL conDatabase.Close Set conDatabase = Nothing End Sub After creating the view, it is represented in the Navigation Pane where you can access it. After creating a view, you can use it as you would a query. For example, you can use it as the Record Source of a form. Here is an example: Private Sub cmdApplyRegistration_Click() Me.RecordSource = "StudentsIdentification" Me.txtFirstName.ControlSource = "FirstName" Me.txtLastName.ControlSource = "LastName" End Sub You can also extract the values of a view using a SELECT statement. You can also display the value of a query in Datasheet View like a table or a query. To do this, call the OpenQuery() method of the DoCmd object. A view is a list of values from a table, an existing query, or another view. A view is not a procedure but it can be used as if it were one. As imitating a procedure, a view can include a type of argument. The word argument here only means that some values can be passed to a view but these values can be specified only when creating the view. They are not real arguments. When creating a view, you can create placeholders for columns and pass them in the parentheses of the view. This would be done as follows: CREATE VIEW StudentsIdentification([First Name], [Family Name]) . . . If you use this technique, the names passed in the parentheses of the view are the captions that would be displayed in place of the columns of the view. This technique allows you to specify the strings of your choice for the columns. If you want a column header to display the actual name of the column, write it the same. Otherwise, you can use any string you want for the column. If the name is in one word, you can just type it. If the name includes various words, include them between an opening square bracket "[" and a closing square bracket "]". After listing the necessary strings as the captions of columns, in your SELECT statement of the view, you must use the exact same number of columns as the number of arguments of the view. In fact, each column of your SELECT statement should correspond to an argument of the same order. Here is an example: CREATE VIEW CarIdentifier([Tag #], Manufacturer, [Type of Car], Available) AS SELECT TagNumber, Make, Model, Available FROM Cars Because, as we stated already, a view is not a procedure and the values passed to the view are not real arguments, when executing the view, don't specify the names of arguments. Simply create a SELECT statement and specify the name of the view as the source. As mentioned earlier, after creating a view, it becomes a named object like a table, a query or a form. As such, it can be removed from a database. To visually delete a view, in the Navigation Pane, right-click its name and click Delete. To programmatically delete a view, use the DROP VIEW expression in the following formula: DROP VIEW ViewName On the right side of the DROP VIEW expression, enter the name of the undesired view and execute the statement. Here is an example: Private Sub cmdAlterView_Click() Dim conDatabase As ADODB.Connection Dim SQL As String Set conDatabase = Application.CurrentProject.Connection SQL = "DROP VIEW StudentsIdentification" conDatabase.Execute SQL MsgBox "The RegistIdentification view has been deleted.", vbInformation conDatabase.Close Set conDatabase = Nothing End Sub You will not be warned before the interpreter deletes the view. |
|
|||||||||||||||||||||
|