Sunday, July 25, 2010

Access VBA Programming - How to use SQL and VBA modules to retrieve data

If you have been using Access for a while ', you may be familiar with SQL. SQL (Structured Query Language) is the language used to manipulate the records in the database. It 's the tongue behind the query and the recordset. Indeed, visual basic when he built a query in Access, you actually use a SQL statement.
You can also use SQL statements in VBA (Visual Basic for Applications) programming language. VBA and SQL work very well together and are powerful tools to use in yourAccess applications. With them, you can add more functionality to your program.
Sometimes it is useful to limit number of records that appear in a form, based on certain criteria. You can also order information about a column.
How can this be done?
First, you open a form in design view. visual basic Then associate a query for this module. Let 's say you have a query named qryCustomers, which contains the names of your customers and the names of the cities wherecounts.
Next, you define which records from this query will be selected when the user chooses an option on the form. For example, visual basic if the user can choose between three options, you write a SQL Select where three diverse and Order clauses to retrieve data.
In addition, it offers a option group with three option buttons on the form. Each option to select another set of records to display the form.
Finally, name the control group of options.visual basic Right-click the Control menu and click Build Event. Then Builder menu, visual basic select Code Builder. You open the VBA editor. Select the event after the upgrade. Now put the SQL statements in the code module.
First, define two variables to compose an SQL string.
Dim strQuery As String
Dim string as strWhere
Then, put a string in a SELECT statement to retrieve all records from the query.
strQuery = "Select * From qryCustomers"
Now, to write a WHERE clause toeach option. visual basic For example, you want to display only those customers on the basis of a city and ordered by name.
Select Case Me! OptionGroupName
Case 1
strWhere = "where the order of the city = 'New York' by Name '
Case 2
strWhere = "Where City = 'Order to Washington' by Name '
Case 3
strWhere = "where the order of the city = 'Atlanta' by Name '
End Select
Finally, attach the string to the form.
Me.Recordsource = strQuery & strWhere
Me.Requery
When the form is opened and the user selectsoption, the event after update of this control is activated and the record source of the module is changed. The module is reloaded with the resulting data, according to the option that was clicked.visual basic
shooter games buy csharp and media player color laser printer

No comments:

Post a Comment