You are on page 1of 7

Course Code

CourseTitle AssignmentNumber

:
: :

CS-65
WindowsProgramming BCA(4)65/Assignment/2011

Maximum Marks
LastDateofSubmission

:
:

25
30thApril,2010/30thOctober,2011

www.bronzeacademy.com,info@bronzeacademy.com

CS65

There are five questions in this Assignment. Answer all the questions. You may use illustrations and diagrams to enhance explanations.
Question1: DesignadatabaseapplicationtomaintainBCAStudentsExaminationRecordsandto generatingtheHallTicketsforeligiblestudentsusingVisualBasic.Providenecessary documentation,reports,screenlayoutsetc.fortheproject. Page|2

(7 Marks)
Question2: Whatisasignificanceofdatacontrol?Howaretheseused?Explain.

(3 Marks)
VB itself comes with a decent front-end database tool called VisData which was written with VB version 5.0. Accessing the information in a database with VB is quite simple and actually can be done without writing code. Placing a Data Control on a form provides a link between your program and the database file. By itself, the Data Control won't display any data, you must have bound controls to do that. To find out if a particular control can be data bound, check to see if it has DataSource property. Here's how its done. Draw a data control on a form. Set the DatabaseName property to an existing .MDB database by bringing up the "DatabaseName" dialog box from the properties window. Now go to the RecordSource property of the Data Control and you'll see a list of all the tables in the database. Only one table can be assigned. You'll need additional Data Controls to display more than one table at a time. Draw a Text Box on the form. First set the DataSource property to the name of the Data Control (Data1 if you're using the default). Now set the DataField property, this time choosing from a list of all the fields in the table. Run the project and the Text Box will contain the data from the first record in that field (making it the current record). To display an entire record, you'll need a bound control for each field, or you might want to try one of the database custom controls (DBGrid, DBList, DBCombo). Be aware that making changes to bound controls changes the actual data in the database. Set a Text Box's Locked property to True to avoid that, or use a Label. Moving around the database

www.bronzeacademy.com info@bronzeacademy.com

CS65

The Data Control has four buttons for moving through its records as you can see above. You can also move through the records with code.

Data1.Recordset.MoveFirst Data1.Recordset.MovePrevious Data1.Recordset.MoveNext Data1.Recordset.MoveLast


The Data control will create a Recordset object to represent the table and allow you to use it as a property. The Recordset object has many useful properties and methods of it's own. These four methods require no arguments and will accomplish the same thing as the user clicking the arrow buttons. A Recordset is just what it implies, a set of records. In the simplest case, the Recordset is the entire table, but you can search databases with a query to work with particular tables, fields, and records. This is done by assigning the RecordSource property of the Data Control to an SQL string instead of a table name. Recordsets can be of three different types:

Page|3

Table type (dynamic) Dynaset type (dynamic) Snapshot type (static)

The Table type is one entire table which can be updated (records can be added, deleted, and edited). The Dynaset type allows fields and records from multiple tables and is also updatable. The Snapshot type also allows data from multiple tables, but is not updatable: use this type when you just need to examine data, rather than update it. The RecordsetType property of the Data control can be set to create the desired type. If you do this in code, the following constants are available:

Data1.RecordsetType = vbRSTypeTable Data1.RecordsetType = vbRSTypeDynaset Data1.RecordsetType = vbRSTypeSnapShot


RecordCount property The number of records in your Recordset can be accessed with the RecordCount property of the Recordset object. However, if the Recordset is of Dynaset or Snapshot type, then it will be necessary to populate the Recordset first. Populating a Recordset is accomplished by using the MoveLast method.

Data1.Recordset.MoveLast MsgBox "There are " & Data1.Recordset.RecordCount _ & " records in this Recordset"

www.bronzeacademy.com info@bronzeacademy.com

CS65

It's not necessary to populate a Table type Recordset, nor is it necessary to populate any Recordset unless you need the RecordCount right away. Population of large Recordsets is time consuming. Using the Recordset in other ways will accomplish the population. Field data types

Page|4
Like variables, properties, and functions, Fields in a database must be of a certain data type. All the data types you're familiar with are available. Microsoft Access uses these data types.

Text- String data up to 255 characters Memo- String data up to 1.2 gigabyte Number- Can be further specified as Integer, Long, Single, Double Date/Time- Just like the Date data type Currency- Just like the Currency data type Yes/No- Boolean OLE Object- Used for pictures or other objects.

VB 6.0, ADO Data Control, and Access 2000 The Access 97/Jet 3.x database can still be used with VB 6.0. Access 2000 can convert the file to the new Jet 4 format, but that will not be a good idea if you want to continue using the intrinsic Data control which revolves around DAO. Jet 4 is more comfortable with ADO, the new, easy-to-use data object library. If you're starting a new project and database with VB 6.0, you should go all out and use the Microsoft Jet 4 database, Access 2000, and the new ADO Data Control. VisData cannot read the new format. With ADO, the connect string is now your best friend. Here is a connect string from one of my projects:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\WINDOWS\ Desktop\ASPProj\board\db\board.mdb;Persist Security Info=False


No one is expecting you to write these connect strings from scratch. With VB 5.0 and the Data control, you would use the DatabaseName dialog box to select the database file. With VB 6.0 and the ADO Data control, you'll set the ConnectionString property with a 4-step connect string "wizard". Once that is done, you'll set the CommandType property to 2 - adCmdTable. This is similar to setting the RecordsetType property of the intrinsic Data control to 0 - Table. Now you can set the Recordsource property in much the same manner as before, but now you'll get a dialog box instead of a drop down list.

Question3:

Whatisthedifferencebetweenpassingbyvalue&passingargumentsbyreference inVisualBasic6Procedures?Howdeclarationisdoneineithercase? (5Marks) Procedures and function can accept any type of argument you want. You just need to give the argumenta nameanda datatype,justlikeyouwouldalocal variable.Infact,argumentsarejust localvariablesthathappentobedeclaredonthesamelineastheprocedurename.Youcanusetwo

www.bronzeacademy.com info@bronzeacademy.com

CS65 different methodsforpassinginparameterstoprocedure: callbyvaluecallbyreference. Thers a bigdifferencebetweenthesetwomethods,asyoullseeinthissection. Recallthattheparametersthatappearinthedefinitionofaprocedurearecalledformalparameters. Page|5 CallByValue(PassByValue) Thecallbyvalue(orpassbyvalue)methodcopiesthevaluesofactualparametersintotheformal parameters,thatis,theprocedurecratesitsowncopyofargumentvaluesandthenusesthem.To understandthisconcept,letusconsideroneexample.Totestyourgrammar,yourEnglishTeacher purposelywritesgrammaticallyincorrectpassageonhersheetandgivesittoyouforcorrections.So youcopydownthegivenpassageonyourownsheetandmakecorrectionsthere.Thisisanexample ofcallbyvaluemethod.Here,yourteacherpassesthesheethavinggrammaticallyincorrectpassage (whichistheactualparameterhere)toyou,youcopythepassageonyoursheet(thisistheformal parameter). Whatever changes take place, are not reflected back to the orginal as the value of originaliscopiedontoanotherwhichistheworkcoy.Thus,incallbyvaluemethod,thechangesare notreflectedbacktotheoriginalvalues. InVB,topassargumentsbyvalue,youneedtomentionkeywordByValwhendeclaringarguments asshownbelow: SubByValExample(ByValinNumAsInteger) : : EndSub Onlyacopyofavariableispassedwhenanargumentispassedbyvalue.Iftheprocedurechanges thevalue,thechangeaffectsonlythecopyandnotthevariableitself. CalByReference(PassByReference) Inthecallbyreferencemethod,thecalledproceduredoesnotcreateitsowncopyoforiginalvalues, rather, it refers to the original values only by different names i.e. the references. Thus the called procedureworkswiththeoriginaldataandanychangeinthevaluesgetsreflectedtothedata.To understandthisconcept,letusreviseoursameoldexampleofgrammaticallyincorrectpassage.if your English teacher gives you the original sheet having the grammatically incorrect passage and allowsyoutoworkuponthesamesheet,thenwhatevercorrectionyoumake,willbethereonthe original.Inotherwords,Icansaythechangesarereflectedbacktotheoriginalasthevalueofthe originalisnotcopiedanywhereratheroriginalitselfhasbeenmadetheworkcopy.Thus,incallby referencemethod,theoriginalisnotcopiedanywhereratheroriginalitselfhasbeenmadethework copy.Thus,incallbyreferencemethod,thechangesarereflectedbacktotheoriginalvalues. Thecallbyreferencemethodisusefulinsituationwherethevaluesoftheoriginalvariablesareto bechangedusingafunction. (i) MentionkeywordByRefintheargumentdefinition.(Youmayevenomititasbydefault VBpassesanargumentbyreference). (ii) Passthevalueasanexpressionratherthanadatatype. www.bronzeacademy.com info@bronzeacademy.com

CS65 Letuslearnaboutthesetwoways. (i)throughByRefkeyword SubPostAccounts(ByRefintAcctNumasInteger) Thiskeywordensurethattheargumentispassedbyreference Page|6 EndSub (ii)throughpassingvalueasanexpression Ifyouspecifyadatatypeforanargumentpassedbyreference,youmustpassvalueofthat typefortheargument.Youcanworkaroundthisbypassinganexpression,ratherthanadatatype, foranargument.VisualBasicevaluatesanexpressionandpassesitastherequiredtypeifitcan. Question4: WhatareDynamicarrays?Illustrateusingcodesegmentshowitcanbemanaged? (5Marks)
When using arrays you must be careful not to consume too much memory. For example
Dim MyArray (10000) As Long

Because each element is declared as long, and a long variable occupies 4 bytes of memory, the MyArray requires 40 004 (10 001 x4) bytes of memory. This may not sound like much, but when you have 10 such arrays in your program these arrays consume 400 040 (40 004 x 10) bytes of memory. Therefore, it is wise to always try to set the size of your arrays to the minimum your program requires. Sometimes, however, it is only possible to detemine this during runtime. In these cases you can use the ReDim statement to change the size of an array. An array that changes its size during runtime is called a dynamic array. When you declare a dynamic array, you do not declare it like a fixed array. When you declare a dynamic array the size is not specified. Instead you use the following syntax:
Dim ArrayName() As DataType

Dim is the scope of the array. If you declare it in a Form, use Dim, if you declare it in a Module and you want every procedure to access it, declare it as Global, otherwise use Public. If you declare the array in a procedure use Dim. ArrayName is the name of the array. DataType is a valid datatype. Normally Integer (-32 768 to 32 767), String (A string of characters), Boolean (True or False). You then use the ReDim statement in your procedures or functions, using the following syntax:
ReDim ArrayName(LowerValue To HigherValue)

In fact, it is almost identical to a normal declaration for a fixed array, except that 1), it is not a declaration, as it is executed at runtime, 2) it uses ReDim, and 3) there is no datatype declaration (this cannot be changed). So, the following code declares a dynamic array called gArray, and then sets the size during runtime:
Dim sStringArray() As String Sub Form1_Load() ' Initialise array ReDim sStringArray(1 To 10) End Sub

This code assigns 10 elements to gArray when Form1 loads. Note: When using dynamic arrays, you must set the size of an array using the ReDim statement, before filling the array. However, when using the ReDim statement, any values already in the array (if it has been resized previously), will be deleted. In some cases, this is not what you would want! So, you use the Preserve keyword:
ReDim Preserve ArrayName(LowerValue To HigherValue)

If the array has grown, there will be a number of blank array spaces at the end of the array. If the array has shrunk, you will lose the end items.

Question5:

HowispasswordfieldaddedinVisualBasic?HowcanauserexecuteSQLSELECT WHERE&ORDERBYqueriesfrominsideaform?

(5 Marks)
www.bronzeacademy.com info@bronzeacademy.com

CS65 Note:Assumptionscanbemadewherevernecessary.

Page|7

www.bronzeacademy.com info@bronzeacademy.com

You might also like