Tips >> Visual Basic |
||||||||||||||||||||||||||||||||||||||||
Database Connectivity |
||||||||||||||||||||||||||||||||||||||||
MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE Contents
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
Cursors The Recordset object fetches data using Cursors, which are used to identify the current position in the result set, and what row will be returned next. The Recordset object supports four cursor types. Recordset Cursors |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
The cursor type is set with the CursorType property of the Recordset object. | ||||||||||||||||||||||||||||||||||||||||
adoRS.CursorType = adOpenDynamic | ||||||||||||||||||||||||||||||||||||||||
Opening a Recordset The Open method is used to retrieve a recordset from the database. The method takes four optional parameters, the recordset source, the active connection, the cursor type, the lock type, and options. The following table describes the parameters. Open Method Parameters |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
The following may be used to open a recordset without using any of the properties of the Recordset object. | ||||||||||||||||||||||||||||||||||||||||
adoRS.Open strSQL, strConn, adOpenForwardOnly, adLockReadOnly, adCmdText | ||||||||||||||||||||||||||||||||||||||||
The following example uses the Properties to determine how the recordset should be opened, and calls the Open method with no parameters. | ||||||||||||||||||||||||||||||||||||||||
Dim adoConn As New ADODB.Connection Dim adoRS As New ADODB.Recordset Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" strConn = strConn & "c:\db\music.mdb" adoConn.ConnectionString = strConn adoConn.Open adoRS.Source = "SELECT * FROM CD" adoRS.CursorType = adOpenForwardOnly adoRS.ActiveConnection = adoConn adoRS.Open Do While Not adoRS.EOF Debug.Print adoRS.Fields("Title").Value adoRS.MoveNext Loop adoRS.Close Set adoRS = Nothing adoConn.Close Set adoConn = Nothing |
||||||||||||||||||||||||||||||||||||||||
Navigating Through a Recordset The Move method moves to a record in a database specified by a row number. An optional parameter may be specified to move relative to a bookmark. The following example moves to the fourth record in the Recordset. |
||||||||||||||||||||||||||||||||||||||||
adoRS.Move 4 | ||||||||||||||||||||||||||||||||||||||||
MoveFirst The MoveFirst method moves to the first record in the Recordset. |
||||||||||||||||||||||||||||||||||||||||
adoRS.MoveFirst | ||||||||||||||||||||||||||||||||||||||||
MoveLast The MoveLast method moves to the last record in the Recordset. |
||||||||||||||||||||||||||||||||||||||||
adoRS.MoveLast | ||||||||||||||||||||||||||||||||||||||||
MoveNext The MoveNext method moves to the next record in the Recordset. The EOF property should be checked to prevent an error occurring. EOF is set to True if the current record is after the last record in the Recordset. |
||||||||||||||||||||||||||||||||||||||||
adoRS.MoveNext If adoRS.EOF Then adoRS.MovePrevious MsgBox "At last record", vbInformation, "Database" End If |
||||||||||||||||||||||||||||||||||||||||
MovePrevious The MovePrevious method moves to the previous record in the Recordset. The BOF property should be checked to prevent an error occurring. BOF is set to True if the current record is before the first record in the Recordset. |
||||||||||||||||||||||||||||||||||||||||
adoRS.MovePrevious If adoRS.BOF Then adoRS.Recordset.MoveNext MsgBox "At first record", vbInformation, "Database" End If |
||||||||||||||||||||||||||||||||||||||||
Adding Records The AddNew method adds a new record to the end of the Recordset. |
||||||||||||||||||||||||||||||||||||||||
adoRS.AddNew | ||||||||||||||||||||||||||||||||||||||||
Updating Records The Update method saves the contents of the edit buffer to a specified Recordset object. Use after changing data in the current record. |
||||||||||||||||||||||||||||||||||||||||
response = MsgBox("Update this record", vbYesNo
+ vbQuestion, "Update") If response = vbYes Then adoRS.Update Else adoRS.CancelUpdate End If |
||||||||||||||||||||||||||||||||||||||||
Structured Query Language The Structured Query Language (SQL - pronounced "sequel") is a very simple language used to manipulate relational databases. You can use SQL to retrieve, insert, update and delete records from relational databases. Clauses may be added to restrict the records that are effected. The Select Query The Insert Statement The Update Statement The Delete Statement The SQL Select Query The select query is used to retrieve records from a database. The keywords used in a select query are summarised in the following table. Select Keywords |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
The simplest select query is to retrieve all records from a single table. For example, to retrieve all records from the CD table in the music database: | ||||||||||||||||||||||||||||||||||||||||
SELECT * FROM CD | ||||||||||||||||||||||||||||||||||||||||
To select specific fields from a table, replace the (*) with a comma-separated list of field names. For example, to select the Artist and Title from the CD table: | ||||||||||||||||||||||||||||||||||||||||
SELECT Artist, Title FROM CD | ||||||||||||||||||||||||||||||||||||||||
To limit the records returned according to some criteria, use the WHERE clause. For example, to return all Artists whose name starts with an 'S': | ||||||||||||||||||||||||||||||||||||||||
SELECT * FROM CD WERE Artist LIKE 'S*' | ||||||||||||||||||||||||||||||||||||||||
The condition in the WHERE clause can contain the operators <, >, <=, >=, =, <> and LIKE. Multiple clauses are joined together using the logical AND and OR operators. The results of a query are ordered using the ORDER BY clause. The results may be sorted in ascending order using the ASC keyword or descending order using the DESC keyword. For example, to list the artists in ascending order by name: | ||||||||||||||||||||||||||||||||||||||||
SELECT * FROM CD ORDER BY Artist ASC | ||||||||||||||||||||||||||||||||||||||||
To list the artists in descending order by name: | ||||||||||||||||||||||||||||||||||||||||
SELECT * FROM CD ORDER BY Artist DESC | ||||||||||||||||||||||||||||||||||||||||
The DISTINCT keyword may be used to retrieve unique values from fields where duplicate data may exist. The following example returns a list of unique surnames. | ||||||||||||||||||||||||||||||||||||||||
SELECT DISTINCT surname FROM customer ORDER BY surname | ||||||||||||||||||||||||||||||||||||||||
The SQL Insert Statement The insert statement is used to insert records into a table. The keywords used in an insert statement are summarised in the following table. Insert Keywords |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
The following example inserts the data "Gorillaz" and "Debut Album" into the "Band" and "Title" fields of the "CD" table. | ||||||||||||||||||||||||||||||||||||||||
INSERT INTO CD ([Band], [Title]) VALUES ('Gorillaz', 'Debut Album') | ||||||||||||||||||||||||||||||||||||||||
The next example uses ADO to insert a record into a database. | ||||||||||||||||||||||||||||||||||||||||
Dim adoConn As New ADODB.Connection Dim adoCmd As New ADODB.Command Dim strConn As String, strSQL As String ' Open a Connection object strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" strConn = strConn & "c:\db\music.mdb" adoConn.ConnectionString = strConn adoConn.Open ' Define a query string strSQL = "INSERT INTO Programming ([Band], [Title]) VALUES ('" strSQL = strSQL & txtBand.Text & "', '" strSQL = strSQL & txtTitle.Text & "')" ' Set up the Command object adoCmd.CommandText = strSQL adoCmd.CommandType = adCmdText adoCmd.ActiveConnection = adoConn adoCmd.Execute ' Tidy up Set adoCmd = Nothing adoConn.Close Set adoConn = Nothing |
||||||||||||||||||||||||||||||||||||||||
Further records may be added to the database using the INSERT Statement
with different values. The SQL Update Statement The update statement is used to update records in a database. The keywords used in an update statement are summarised in the following table. Update Keywords |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
The following example updates all records in the "CD" table where the Band is "JJ73" to "JJ72". | ||||||||||||||||||||||||||||||||||||||||
UPDATE CD SET [Band] = 'JJ72' WHERE [Band] = 'JJ73' | ||||||||||||||||||||||||||||||||||||||||
The next example uses ADO to update a record in the database. | ||||||||||||||||||||||||||||||||||||||||
Dim adoConn As New ADODB.Connection Dim adoCmd As New ADODB.Command Dim strConn As String, strSQL As String ' Open a Connection object strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" strConn = strConn & "c:\db\client.mdb" adoConn.ConnectionString = strConn adoConn.Open ' Define a query string strSQL = "UPDATE customer SET [email]='" strSQL = strSQL & txtEmail.Text strSQL = strSQL & "' WHERE [CustNo]=" & CInt(txtCustNo.Text) ' Set up the Command object adoCmd.CommandText = strSQL adoCmd.CommandType = adCmdText adoCmd.ActiveConnection = adoConn adoCmd.Execute ' Tidy up Set adoCmd = Nothing adoConn.Close Set adoConn = Nothing |
||||||||||||||||||||||||||||||||||||||||
The SQL Delete Statement The delete statement is used to delete records in a database. The keywords used in a delete statement are summarised in the following table. Delete Keywords |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
The following example deletes the records from the "CD" table
where the Band and Title are passed from a Form. The following example deletes the record in the "CD" table where the Band is "Trash" and the title of the album is "Deserve To Go". |
||||||||||||||||||||||||||||||||||||||||
DELETE FROM CD WHERE [Band]='Trash' AND [Title]='Deserve To Go' | ||||||||||||||||||||||||||||||||||||||||
The next example uses ADO to delete a set of records from a database. | ||||||||||||||||||||||||||||||||||||||||
Dim adoConn As New ADODB.Connection Dim adoCmd As New ADODB.Command Dim strConn As String, strSQL As String ' Open a Connection object strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" strConn = strConn & "c:\db\active.mdb" adoConn.ConnectionString = strConn adoConn.Open ' Define a query string strSQL = "DELETE FROM ActiveUsers WHERE [Session]='Expired'" ' Set up the Command object adoCmd.CommandText = strSQL adoCmd.CommandType = adCmdText adoCmd.ActiveConnection = adoConn adoCmd.Execute ' Tidy up Set adoCmd = Nothing adoConn.Close Set adoConn = Nothing |
||||||||||||||||||||||||||||||||||||||||
If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon. | ||||||||||||||||||||||||||||||||||||||||
Terms and Conditions / Privacy Policy / Refund Policy / Shipping Policy