Vous êtes sur la page 1sur 5

Edit Custom Color Change the custom color presented in the Color Palette when

BSc-II (Computer Science) the Color Set is set to Windows System Colors.

Font
Visual Basic and Database Notes
VB Controls Properties Font properties that may be changes.
Frames are used to provide a border within the form that includes a caption.
Frames are used to organize forms. Font Choose a font installed on this system. It's wise to choose
standard Windows fonts such as MS Sans Serif that are
universally available.
Frames are also used to organize option buttons into related groups. In
compiled Visual Basic, frames will automatically group option buttons if the
option buttons and frames are created in the correct sequence. In Manifold, Size Size of font, in points.
frames are simply visual artifacts used to organize the form. We can use the
group property of option buttons (see below) to organize them in a standard Effects Bold, Italic, Underline or Strikeout.
VB way.
Sample Text A preview of the selected settings.
To Add an Frame to a Form
File System Controls
1. Click on the Insert Frame button in the Tools toolbar. (DriveListBox, DirListBox, FileListBox)
2. Click and drag the location and size where it is to be created in the
form.
VB provides three native toolbox controls for working with the
3. Continue to create any additional frames desired.
file system: the DriveListBox, DirListBox, and FileListBox. You
4. Right click onto each button and choose Properties from the
context menu.
can use these controls independently, or in concert with one
5. Set the properties for the frame as desired. Note that each frame
another to navigate the file system.
must belong to a group. The DriveListBox control is a specialized drop-down list that
6. Move, resize and align the frame as desired. displays a list of all the valid drives on the user's system. The
Properties most important property of the DriveListBox is
the Drive property, which is set when the user selects an entry
In addition to the properties listed below, a Control tab provides access to
certain frequently used form properties for this control. See the Form
from the drop-down list or when you assign a drive string
Properties topic for information on those properties. (such as "C:") to the Drive property in code. You can also read
the Drive property to see which drive has been selected.
Frame To make a DirListBox display the directories of the currently
General
selected drive, you would set the Path property of the
DirListBox control to the Drive property of the DriveListBox
Caption Text used to label the frame. Font is set in the font tab. control in the Change event of the DriveListBox, as in the
following statement:
MousePointer The type of mouse pointer displayed when over the frame.
Choose from 16 different types plus a custom pointer
Dir1.Path = Drive1.Drive
The DirListBox control displays a hierarchical list of the user's
Appearance 3D or Flat. 3D is the standard Windows look. Flat results in a disk directories and subdirectories and automatically reacts to
rectangular square of background color with a foreground color
border and caption. mouse clicks to allow the user to navigate among them. To
synchronize the path selected in the DirListBox with a
BorderStyle FixedSingle (default) or None. None may be used with FileListBox, assign the Path property of the DirListBox to
Flat Appearance to achieve an uncaptioned rectangle of
background color. the Path property of the FileListBox in the Change event of the
DirListBox, as in the following statement:
OLEDropMode Set to None (default) if the frame does not accept OLE drops and File1.Path = Dir1.Path
is to display the No Drop cursor if an OLE drop is attempted. Set
to Manual, the frame will trigger OLE drop events, allowing The FileListBox control lists files in the directory specified by its
programs to handle the OLE drop operation in code. Path property. You can display all the files in the current
Enabled True or False. True means the object can respond to user-
directory, or you can use the Pattern property to show only
generated events, false prevents it from responding. We can certain types of files.
disable a control like a text box if we wish it to simply display Similar to the standard ListBox and ComboBox controls, you
information in a read-only way.
can reference the List, ListCount, and ListIndex properties to
Color access items in a DriveListBox, DirListBox, or FileListBox
control. In addition, the FileListBox has a MultiSelect property
Back Color Color to be used for the frame's background, normally seen only
when Flat Style is used. which may be set to allow multiple file selection.

Fore Color Color to be used for the frame's foreground, the color of the text Unit# 6 Database Programming
caption.
Visual Data Manager
Color Set Choose from Standard Colors (standard Windows non-dithered The Visual Data Manager is a Visual Basic tool for
colors) or Windows System Colors. The latter will be defined by designing databases. Although it’s rather crude, you can
the user's Control Panel settings and is normally the setting used
so that the form changes appearance like the rest of Windows if use the Visual Data Manager to create and modify
the Control Panel settings are changed. tables, to implement security, and to experiment with
SQL. When you open an existing database, the Visual
Color Palette Displays available colors. Click on the property to be changed to
highlight it in the Properties pane, click on the desired color in Data Manager displays a database window listing the
the Color Palettepane and then press Apply. tables and properties of that database.
Entering data into DB: ADO Data Control:
In addition, You can use the Visual Data Manager The ADO (ActiveX Data Object) data control is the primary
for data entry. Double-clicking the name of a table in the interface between a Visual Basic application and a database. It
Database window opens-the table in data entry, mode, can be used without writing any code at all! Or, it can be a
and you can edit, add, and delete records. central part of a complex database management system. This
The Data Control: icon may not appear in your Visual Basic toolbox. If it doesn’t,
The data control is available on the standard toolbar. select Project from the main menu, then click Components.
After placing a data control on your form, you can edit its The Components window will appear. Select Microsoft ADO
properties, just like any other control. You can also set them Data Control, then click OK. The control will be added to your
from code at run time. toolbox.
The important properties of the data control, from the point of The data control can be used to perform the following tasks:
view of data access, are: 1. Connect to a database.
 Connect - (not used when accessing Access 2. Open a specified database table.
databases) 3. Create a virtual table based on a database query.
 DatabaseName 4. Pass database fields to other Visual Basic tools, for
 Exclusive display or editing. Such tools are bound tools
 Options (controls), or data aware.
 ReadOnly 5. Add new records or update a database.
 RecordSource 6. Trap any errors that may occur while accessing data.
DatabaseName: 7. Close the database.
Determines the path and filename of the database file (.MDB ADO Objects:
file for Microsoft Access). ActiveX Data Objects (ADO) is an application program
Exclusive: interface from Microsoft that lets a programmer writing
If this property is set to True, then when you open the Windows applications get access to a relational or non-
database you will have exclusive access to it. If set to False (the relational database from both Microsoft and other database
default) other users (or programs) can access the database providers. For example, if you wanted to write a program that
while you are working on it. If you are working on a single user would provide users of your Web site with data from an
database, set this property to true - it will improve IBM DB2database or an Oracle database, you could include
performance. ADO program statements in an HTMLfile that you then
Options: identified as an Active Server Page. Then, when a user
Determines access constraints for the tables you are working requested the page from the Web site, the page sent back
on. would include appropriate data from a database, obtained
ReadOnly: using ADO code.
When True, you cannot update the database (default is False). OLE DB is the underlying system service that a
RecordSource: programmer using ADO is actually using. A feature of ADO,
Determines the source of the set of records (the RecordSet) Remote Data Service, supports "data-aware" ActiveX controls
retrieved by the data control. This property will be set either in Web pages and efficient client-side caches. As part
to the name of one of the tables in the database, to the text of of ActiveX, ADO is also part of Microsoft's overall Component
a valid SQL query, or to the name of a predefined query in the Object Model (COM), its component-oriented framework for
Access database. putting programs together.
Viewing the data with the Bound Controls: ADO evolved from an earlier Microsoft data interface, Remote
The easiest way of viewing the data is by using one or more of Data Objects (RDO). RDO works with Microsoft's ODBC to
the following 'bound controls' (available in the learning access relational databases, but not non-relational databases
edition). such as IBM's ISAM and VSAM.
Check box - Use for True/False data Data Environment
Image - Use for image data Data Environment object is created and managed using Data
Label - Use for "non user-editable" data Environment designer. A Data Environment object can
Picture box - Use for bitmaps accomplish any of the following tasks.
Text box - Use for "user-editable" data
• Can contain connection and command objects
Data AccessObjects (DAO): • Can establish relationship between command object to
DAO (Data Access Objects) is an application program interface represent hierarchies
(API) available with Microsoft's Visual Basic that lets a • Can group data and get aggregates
programmer request access to a Microsoft Access database. • Allows fields to be dragged and dropped on to a Data Report,
DAO was Microsoft's first object-oriented interface with for report, and a Form for data entry screen
databases. DAO objects encapsulate Access's Jet functions. • Can work as source for Databound control such as
Through Jet functions, it can also access other Structured DataCombo controls and Hierarchical Flexgrid control.
Query Language (SQL) databases. ADO and OLE DB offer a In order to work with Data Environment object, you have to
faster interface that is also easier to program. add Data Environment Designer to your project. Data
Environment Designer allows programmer to accomplish any
of the tasks mentioned above. Data Environment Designer is
invoked when you double click on Data Environment object in Database Management System
Project Explorer.
Chapter: Advanced SQL statements and Procedures
To add Data Environment object to the project: Topic: Defining your own messages in SQL
Select Project-> Add Data Environment. If Add Data
Environment option is not found then select More ActiveX sp_addmessage
Designers and then select Data Environment. Stores a new user-defined error message in an instance of
Introducing the DataGrid: the SQL Server Database Engine. Messages stored by
The DataGrid control combines a data display using sp_addmessage can be viewed by using
function (such as the VB Label control) and a data the sys.messages catalog view.
navigation function (such as the VB Adodc control) into a
single control. DataGrid sports a highly functional Syntax:
spreadsheet-style user interface. It has many special sp_addmessage msg_id , severity , 'msg'
features for formatting, displaying, updating, and [ , 'language' ]
manipulating the data displayed in its rows and columns. [ , [ @with_log= ] { 'TRUE' | 'FALSE' } ]
As an example for this article, I have used DataGrid and [ , 'replace' ]
an extremely small amount of code to access an AS/400
file named ORDERS (the file, contained in a *SAVF object, The actual error message is "msg", which uses a data type of
and example code can be downloaded from the MC Web nvarchar(255). The maximum characters limit is 2,047. Any
site at www.midrangecomputing.com/mc). You can use more than that will be truncated. The language is used if you
ADO objects to open the database connection and file want to specify any language. Replace is used when the same
and to connect those objects to the DataGrid, which will message number already exists, but you want to replace the
string for that ID, you have to use this parameter.
navigate and display the contents of the file.
Data Report Designer:
The Data Report Designer supports hierarchical data Arguments:
commands that return a recordset containing a child
recordset, which is related to the parent by a key. You
“msg_id” is the ID of the message. msg_id is int with a
can create reports from these recordsets and perform
default of NULL. msg_id for user-defined error messages
grouping operations. Hierarchical data commands are
can be an integer between 50,001 and 2,147,483,647.
based on the hierarchical cursor feature in ADO 2.0 and
the Data Environment Designer.
The combination of msg_id and language must be unique;
You create the report using graphical object called Data
an error is returned if the ID already exists for the
report designer controls. Data report designer controls.
specified language.
Include: a data-bound Textbox control, a function control
which displays calculated figures, and Image control for
“Severity” is the severity level of the error. severity is an
inserting graphics, labels that display captions, and a line
int with a default of NULL. Valid levels are from 1 through
and a shape control that graphically organizes the data.
25.
Note: See example from internet.
'msg' is the text of the error message.

'language' is the language for this


message. language is sysname with a default of NULL.
Because multiple languages can be installed on the same
server, language specifies the language in which each
message is written. When language is omitted, the
language is the default language for the session.

[ @with_log = ] { 'TRUE' | 'FALSE' }


Is whether the message is to be written to the Windows
application log when it occurs. If TRUE, the error is always
written to the Windows application log. If FALSE, the error
is not always written to the Windows application log but
can be written, depending on how the error was raised.
Only members of the sysadminserver role can use this
option.
Note Understanding GRANT, DENY, and REVOKE in SQL

If a message is written to the Windows application log, it GRANT


is also written to the Database Engine error log file. In order for a user to be able to do something, he or she must
be given permission to do it. We do this via the GRANT
[ @replace = ] 'replace' command. Let's do some setup of a test role and a test user in
If specified as the string replace, an existing error message a test database I've created (aptly called TestDB):
is overwritten with new message text and severity
level. replace is varchar(7) with a default of NULL. This Create Test User
USE TestDB;
option must be specified if msg_id already exists. If you
GO
replace a U.S. English message, the severity level is CREATE ROLE TestRole;
replaced for all messages in all other languages that have GO
the same msg_id. CREATE USER TestUser WITHOUT LOGIN;
GO
EXEC sp_addrolemember @rolename =
Return Code Values 'TestRole', @membername = 'TestUser';
GO

0 (success) or 1 (failure) Create Tables and Permissions

Now let's create a schema, a couple of tables, and let's


Example GRANT the ability to select against the first table.
Defining a custom message
The following example adds a custom message CREATE SCHEMA Test;
to sys.messages. GO
CREATE TABLE Test.TestTable (TableID int);
GO
GRANT SELECT ON OBJECT::Test.TestTable TO
USE master;
TestRole;
GO GO
EXEC sp_addmessage 50001, 16, CREATE TABLE Test.TestTable2 (TableID
N'Percentage expects a value between 20 and int);
GO
100.
Please reexecute with a more appropriate
value.'; REVOKE
GO REVOKE undoes a permission, whether it's a GRANT or a DENY.
If you issue the following REVOKE and then check the
permissions, you'll note that the GRANT that was previously
present for Test.Table1. After issuing the revoke command, re-
sp_addmessage msg_id , severity , 'msg' run the test harness(join/ attach) queries above against that
[ ,'language' ] // table and you'll see that the user cannot query the table any
Optional longer.
[ , [ @with_log= ] { 'TRUE' | 'FALSE' } ]
[ , [ @replace= ] 'replace' ] -- Let's undo the permission using REVOKE;
REVOKE SELECT ON OBJECT::Test.TestTable FROM TestRole;

Remember, REVOKE doesn't cancel a GRANT. It doesn't block a


GRANT. It removes a permission at the level specified to the
security principal (user or role) specified. That's why we say it
undoes the permission.

DENY
DENY blocks access. If a user has both a GRANT and a DENY on
a given object, by whatever means, the DENY will take effect.
For instance, let's consider the case of a GRANT SELECT against
the Test schema. This would give the ability to issue a SELECT
against any table or view in the Test schema. Try just applying
this permission, re-checking the permission, and then testing WHEN DOB < = ‘30’
the user's access to both Test.TestTable and Test.TestTable2. THEN ‘Young’
You'll see the user can now issue a SELECT query against both ELSE ‘Old’ FROM
tables. STUDENT

Using CAST ( ) and CONVERT ( ) Procedure


In SQL server, there are two ways to convert from one data A procedure is a subprogram that performs a
type to another data type. specific action. It is a collection of SQL statements.
i. CAST ( ) ii. CONVERT ( ) Why use (stored) procedures?
Procedures provide improved performance because
i. CAST ( ) fewer calls need to be sent to the database. For example if
Syntax:
a procedure has four SQL statements then there only
CAST( expression as data_type [length] )
In the above syntax, the “expression” is the column name needs a single call to DB instead of calling four
whose data type is to be changed. The “data_type” is in which statements one by one.
you want to convert the data type. The “[length]” is used to set Two types of procedures are:
length of expression and this is optional to use. i. User defined procedures ii. System stored
Example: Suppose we have a table STUDENT or Defined procedures
User defined procedures are defined by the user itself.
ID Name DOB System stored procedures:
101 Ali 1980-12-30 00:00:00 AM These are useful in performing administrative and
102 Ahmed 1985-10-02 12:05:16 PM informational activities in SQL server. Some system
103 Akmal 1990-05-08 04:02:40 PM stored procedures are as under;
Query example:  Sp_help: Reports about DB objects
SELECT ID, Name, DOB, CAST( DOB as nvarchar ) FROM
STUDENT
and user defined data type
Output:  Sp_table: Informs about list of tables
ID Name DOB (after conversion) from DB
101 Ali Dec 30, 1980 12:00 AM  Sp_helptext: Displays the definition that
102 Ahmed Oct 02,1985 12:05:16 PM is used to create an object in rows
103 Akmal May 08 1990 04:02:40 PM  Sp_depends: Used to get dependent
object details
ii. CONVERT( )
Syntax:
CONVERT( data_type[length], expression [,style] )
In the above syntax the “length” and “style” are optional to
use. The “style” specifies how the expression will be shown.
Different styles are available to use in SQL server i.e. different
date styles like mm/dd/yyyy, yy/mm/dd, and dd/mm/yyyy etc.
can be used for expression.

Query example:
SELECT ID, Name, DOB, CAST( nvarchar, DOB ) FROM
STUDENT
Output:
The output will be same as of CAST( ).

THE SYSMESSAGES
The sysmessages are used to generate system messages and
executed automatically when a specific event is occurred in
order to warn or notify the user. “Raiserror” is a sysmessage
which is used to notify about the error. Similarly throw is an
also error handler in sysmessges.

The CASE expression


This statement is used with numbers, text and dates etc. The
CASE expression compares expressions and used with “when”
and “then” statements. This is equivalent to IF….THEN in
Excel.
Example:
SELECT ID, Name, DOB, CASE WHEN DOB < = ‘10’
THEN ‘Child’

Vous aimerez peut-être aussi