Monday, January 12, 2009

Querying Table Data Using Visual Basic Code in Ms Access

In order to fully utilize the capabilities of MS Access, one must learn not only learn the Visual Basic (VB) programming language, but should also learn Standard Query Language (SQL). Once a grasp of these two languages have been obtained, MS Access users can begin to build faster and more efficient databases.

One tool that has proved itself very useful to me over the years is querying data from tables or queries using VB and SQL code. A brief introduction to this process is presented in this article. To best understand this process, an example is provided below along with an explanation of its parts.

‘*********CODE***********
Dim rstTemp As Recordset
Dim strSQL As String
Dim routeNum As Integer

strSQL = "SELECT [Route], [Main Route PM], [Intersecting Route], [IntBeginPM], [IntEndPM] “

strSQL = strSQL + “FROM Intersections_list WHERE (((CStr([Route])) = """ + cmbRouteQuery + """));"

Set rstTemp = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)

If (Not (rstTemp.EOF)) Then
rstTemp.MoveFirst
routeNum = rstTemp(0)

‘************************

After the initial variable declarations, the code assigns an SQL statement to the string variable strSQL. This statement directs Access to gather all the data in the Route, Main Route PM, Intersecting Route, IntBeginPM, and IntEndPM fields of the table named Intersections_list. Furthermore, it directs Access to only gather information from these fields where the Route field is equal to a value held in the combo box cmbRouteQuery.

Once the SQL statement has been set, it is passed to the next line of code which executes it. It should be noted that the dbOpenDynaset variable is built into Access and holds an integer value that changes the type of recordset to open. For most general purposes, using dbOpenDynaset will work just fine.

The “if statement” in the code example verifies that the recordset just created contains information. If information is present, the code directs Access to move to the first record in the recordset. The code then stores the route in the first record (routeNum = rstTemp(0)) in the variable routeNum to be used for later use.


Read more ...............

Create Database in Sql Server 2005, Database Statements

Following is the full syntax used for creating database in Sql Server 2005.

CREATE DATABASE database_name
[ ON [ PRIMARY ] [ < filespec >] ]
{ LOG ON [ < filespec > ] ]
< filespec > : : =
( [ NAME = logical_file_name , ]
FILENAME = 'os_file_name'
[ , SIZE = size ]
[ , MAXSIZE = { max_size | UNLIMITED } ]
[ , FILEGROWTH = growth_increment ] ) [ ,.... n ]
Description ::-


ON specifies the disk file used to store the data portion of the database.

LOG ON specifies the disk files used to store log files.
FILENAME=os_file_name specifies the operating-system file name for the file.
PRIMARY specifies the associated list that defines files in the primary filegroup.
NAME=logical_file_name specifies the logical name for the file.
MAXSIZE=max_size specifies the maxium size to which the file defined in the list can grow.
FILEGROWTH=growth_increment specifies the growth increment of the file defined in the list.
Remember - " FILEGROWTH setting for a file cannot exceed the MAXSIZE setting"

NOTE : Everything written between [ ] is optional .

But remeber that to create a database you must be a member of the dbcreator server role.

After a database is created, to check details of the database use following command :

sp_helpdb [ database_name ]

To drop a user defined database use following command:

DROP DATABASE database_name

Note: you cannot delete a system-defined database.

To rename a user-defined database use :

sp_renamedb old_database_name , new_database_name

Fortunately you can also create a database by a right click on the object explorer, Thanks to SQL SERVER Management studio 2005.

SQL SERVER 2005 contains the following system database :

1. master
2. tempdb
3. model
4. msdb
5. Resource
--------------------------------------------------------------------------------------------------------------------------------------------
If u have any query feel free to ask. If u like this post , rate & comment it.

Regards,

Anuj Tripathi

Read more ...............

Enter your email address:

Delivered by FeedBurner

Blog Archive

Followers

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP