Thursday, April 29, 2010

vb module theory


Module:-

The module is the object used to write the code or for programming. We write down the codes in the modules for functioning particular task.

There are:-

a. Standard module.

b. Class module

a. standard module:-

It is the module where written codes or expressions are public or shareable to the other modules also. The module object is oneself the standard module.

b. Class module:-

The class module is the module of the form or class module object that is also used to write the code for others purposed .It is out of this topic.

The details of the standard module:-

Starting:

a. open the standard module.

b. Click new, you will get the module window.

The coding area:-

The coding area is called procedure but there are three kinds of procedure.

1. Sub procedure

2. Event procedure

3. Function procedure.

We use this kind of procedures for the different purposes. But we use sub procedure and functions procedure only in the standard module.

Creating procedure in standard module:-

1. We can create many procedure in module. But they should be unique.

2. Procedures are the code writing area.

3. We write code in the sub procedure and to create function we use function procedure.

Syntax for procedure:-

Sub procedure_name ()

[Code statement]

End sub

Things to remember while creating procedure:-

I. writes sub and procedure and presses enter.

ii. Procedure ends with end sub.

iii. Procedure's name should be unique.

iv. procedure name does not contain space, number only and system's keyword(loop, end, if, next, while ,wend) or understand if they are not accepted they are the system's word. Like wise it does not contain the symbols except underscore (_).

Writing code in the procedure:-

1. We write basic language.

2. for input use assignment method for it you can use variant or variable.

3. for output use output functions like "debug.Print" or msgbox.

4. For execute the output:-

a. select the code.

b. press F5 or click on run.

c. view output in immediate window.

The key principal of the programming:

1. The user always input the command to the system.

2. System returns the output on the basis of input command.

3. Input means sending the instructions to the memory, the memory saves or displays the input when there is allocated any area .This allocating way is called "Assignment".

Eg.

A =2

Here 2 is inserted to A character that is the allocation of area in memory. Where 2 is saved .

4. The system returns the out when we have input to the system. Like outputting through speaker, printer, we can output the result in program using output command.

Like for output, we use "debug. print " method in module.

Immediate window (ctrl+g):-

It is used to check the immediate output and display output from the module.

Simple programming in immediate window:-

Assignment:-

A=2

B=2

C=a+b

Output:-

Print c

Or

? c

Interest:-

p=1000

t=2

r=1.5

i= (p*t*r)/100

? i

30

Example:-

Variable and variant:-

Variable is the character that stores the value input to the memory. It is changeable while program run. The variable takes less memory comparing with variant. Variable can be defined by differently.

Summary about variable:-

1. Variable is one storage character input value to the system.

2. Variable fix the data type.

3. Variable takes low memory.

4. Variable has own rule to declare (assignment).

5. Variable also is named differently.

6. Declaring variable is called explicit declaration.

Variable declaring methods:-

The variable is declared by three ways:-

1. With in procedure.

2. With in module(window) label.

3. With in project label.

1 .With in procedure:-

Declaring variable within the procedure will be private but it has the flexible and fixes the data type. It is declared in the procedure.

2.With in module label:-

When we declare variable in this label,it becomes common to all the procedure in same module window.

3.With in project label:-

When we declare variable as this kinds will be common to all the projects' objects. This is the dynamic way.

Variable declaring syntax:

[Keyword] [Variable name] as [data type]

Procedure label:-

Sub adding ()

Dim a as string

Static b as integer

Private k as integer

End sub

Module label:-

It is declared in the general declaration area.

Eg.

Dim a as Integer

project label:-

When the variable is declared in the public label it is common to all projects.

Public a as integer

Setting

Description

Decimal precision

Storage size

Byte

Stores numbers from 0 to 255 (no fractions).

None

1 byte

Decimal

Stores numbers from –10^38–1 through 10^38–1 (.adp)
Stores numbers from –10^28–1 through 10^28–1 (.mdb)

28

12bytes

Integer

Stores numbers from –32,768 to 32,767 (no fractions).

None

2 bytes

Long Integer

(Default) Stores numbers from –2,147,483,648 to 2,147,483,647 (no fractions).

None

4 bytes

Single

Stores numbers from
–3.402823E38 to –1.401298E–45
for negative values and from
1.401298E–45 to 3.402823E38 for positive values.

7

4 bytes

Double

Stores numbers from
–1.79769313486231E308 to
–4.94065645841247E–324
for negative values and from
4.94065645841247E–324 to
1.79769313486231E308 for positive values.

15

8 bytes

Data type:-

Data types meaning

String defines text.

Integer defines number.

Date defines date

Image used to store image

Double used for decimal and numbers.

Single used for decimal and negative numbers.

Byte used to store numbers from 0 to 255.

Currency used for currency numbers.

Boolean for true or false

Long for large numbers

Naming variable:-

To name the variable we should remember the following rules:-

a. Variable name should be unique.

b. It does not contain number only, characters except underscore.

c. Name should be in alphabet.

d. System Keywords are restricted.

e. Every variable name should have prefix according to the data types.

Naming way according to the data types:-

Data types- prefix example

String str strname

Integer int intfirstvalue

Double dbl dblsalary

Single sng sngot

Boolean bln blnval

Image img imgphoto

Date dt dtbirt

Currency cur curpayment

Long lng lngnumber

Code window and code managing:-

While Coding code manages is necessary. For this we need to follow these rules:-

1. We should write different statement block in the different indentations.

2. Comment is necessary to write down the extra information, for this we can use single quote or edit box.

a. get edit box from view menu or right click on the standard tool bar.

3. every different code statement should be broken by one enter press.

Edit box:-

Indent (tab)-it is used to decrease the indentation in one tab order.

Outdent (shift+tab): it is used to increase the indention out in each one tab order.

Comment block:-

It is used to comment the codes at once.

Uncomment block:-it is used to uncomment the codes at once.

Complete word (ctrl+space):-

It displays the complete keyword or variable by the first unique letter.

Module view:-

There are two views.

1. Procedure view:-it is used to view procedures where our cursor is only.

2. Full module view:-we can view all the procedure created in the module.

#they are in the left bottom corner.

Window label variable declaration:-

Functions:-

Functions are the set of arguments that outputs the results when user passes the value in to the functions.

There are many kinds of functions:-

a. String functions.

b. Numeric functions.

c. Date and time functions.

d. Color functions.

a. string functions:-

It is used to manipulate the string data i.e. text, numbers and text, characters etc.

The string functions are given below:-

1. Len ()

2. left()

3. right()

4. mid()

5. strreverse()

6. Ucase () and Lacese()

1. Len ():-This function finds out the length of the given string character.

Syntax:-

Len (string data) or Len (expression)

Note:-string data is always quoted by double quotation mark.

Example:-

Sub stdname()

Dim strname As String

strname = "Prakash Gautam"

L = Len (strname)

Debug.Print L

End Sub

'Output =13

B .Left ():-

This function finds out the left value of the given string character.

Syntax:

Left (string, length as long)

Or

Left (string character, number for retrieving character)

B .right ():-

This function finds out the right value of the given string character.

Syntax:

right (string, length as long)

Or

right (string character, number for retrieving character)

Example:-

Sub stdnameright()

Dim strname As String

strname = "Prakash Gautam"

rt = Right(strname, 6)

Debug.Print rt

End Sub

'Output =gautam

b. Mid:-

It is used to find out the mid value from the given character.

Syntax:

Mid (string, start as long, length)

Start as long:-number from where starting

Length:-number for how many character.

Example:-

Sub stdnamemid()

Dim strname As String

strname = "PrakashGautam"

md = Mid(strname, 3, 5)

Debug.Print md

End Sub

'Output=akash

d. strreverse:-

It makes the given string character opposite.

Syntax:-

Strreverse (string)

Example:-

E. Lcase and ucase:-

They make the string characters from lower case to upper case and uppercase to lower case:-

Syntax:-

Lcase( string)

Ucase (string)

Examples:

Sub lucase()

strn = "Kathmandu"

u = UCase(strn)

Debug.Print u

stRN1 = "BHAIRAHAWA"

l = LCase(stRN1)

Debug.Print l

End Sub

Concatenation:-

For combining the string characters and variable we use concatenation i.e. ampersand (&).

Sub find percent()

Dim inteng As Integer

Dim intnep As Integer

Dim intot As Integer

Dim dblper As Double

inteng = 80

intnep = 90

intot = inteng + intnep

dblper = intot / 2

Debug.Print dblper & "%"

Debug.Print "you have got" & " " & dblper & "%"

End Sub

2. Numeric Functions:-

Solve the some numeric problems. Some numeric functions are:-

1. Int or cint:-

It changes the string data types to the number.

Syntax:-

Int (string)

Eg.

Sub changenum()

a = 5

b = 5

c = Int(a) + Int(b)

'c=cint(a)+cint(b)

Debug.Print c

End Sub

2.Val:-It also changes the string into number.

Syntax:-

Val(string)

e.g.

Sub changenum()

a = 5

b = 5

c = Val(a) + Val(b)

Debug.Print c

End Sub

c.sqr:-

It is used to find out the square root.

Syntax:-

Sqr (number)

Eg.

Sub findsqrt()

Dim a As Integer

a = 9

sq = Sqr(a)

Debug.Print sq

End Sub

'Output:-3

d. Round:-

It rounds off of the decimal number in the given place.

Syntax:-

Round (number as double, place value)

Eg.

Sub makeroundoff()

n = 5.25487

r = Round(n, 2)

Debug.Print r

End Sub

E.rnd:-

It randomizes the value from the given number.

Syntax:-

Rnd(number)

Eg.

F.ABS:-

Find out the absolute value from the given value.

Syntax:-

Abs(negative number)

e.g

Sub abolute()

a = -125.555

Debug.Print Abs(a)

End Sub

Date and time functions:-

It is used to retrieve and customize the date and time of the system.

Some date and time functions:-

a. Date:- It executes the date of the system.

Syntax:-

Date()

b. Time:-It executes the time of the system.

Syntax:-

time()

C. Hour/minute/second :

-They execute the hour, minute, second from the time.

Syntax:-

Hour (time)

Minute (time)

Second (time)

Example:-

Sub finddatestime()

Debug.Print Date

Debug.Print Time

Debug.Print Hour(Time)

Debug.Print minute(Time)

Debug.Print Second(Time)

End Sub

Year:-

It displays the current year.

Year: Year(date)

Month:-month (date)

Day: - day (date)

Now:-show complete date and time.

Syntax:-Now ()

Formatting date and time:-

To customize the date and time function to our requirement we use the following function:-

Syntax:-

Format (function, "formatting characters")

Formatting characters:-

Characters-example

Dd -23

Ddd -sun

Dddd -Sunday

Yy -08

Yyyy -2008

Mm -12

Mmm -feb

Mmmm -february

HH:MM:SS-for time -04:12:23

Am/pm or a/p-gives am or pm.


For Further Reading,
Software Developing

0 comments:

Post a Comment

 

Popular Posts

Man Behind This Blog