VB 91-102

Embed Size (px)

Citation preview

  • 7/29/2019 VB 91-102

    1/6

    Q91. What actions trigger the initialize and the terminate event of the object ?

    Ans.

    The Class_initialize event is triggered when an object is created and the Class_initialize event occurs when anobject goes out of scope or is terminated with Set ObjectName = Nothing. These event procedures are usefulfor doing any setup work for making sure that the memory allocated for an object is released.

    Public Sub Class_Initialize()

    Create the collection object

    Set mProducts = New Collection

    End Sub

    Public Sub Class_Terminate()

    Remove the collection from memory

    Set mProducts = Nothing

    End Sub

    Q92. For which type(s) of Record Set is a Find valid(FindFirst, FindNext, FindLast, FindPrevious)?What types of data can be searched for with a Find?

    Ans:

    You can search for a particular value in any field in a dynaset or snapshot. For example, you could request acertain name, account number, phone number, book title field stored in the Database and bound to one of yourcontrols. Use the FindFirst, FindNext, FindLast, FindPrevious to locate any data.

    Q93. What do you mean by error handling, debugging.

    Error handling is an essential procedure in Visual Basic programming because it can help make the programerror-free. An error-free program can run smoothly and efficiently, and the user does not have to face all sortsof problems such as program crash or system hang.

    Errors often occur due to incorrect input from the user. For example, the user might make the mistakeof attempting to ask the computer to divide a number by zero which will definitely cause system error.

    Therefore a good programmer should be more alert to the parts of program that could trigger errors and shouldwrite errors handling code to help the user in managing the errors. Writing errors handling code should beconsidered a good practice for Visual Basic programmers, so do try to finish a program fast by omitting theerrors handling code.

    Debugging

    Visual Basic provides many tools to help analyze how your code operates. These debugging tools areparticularly useful in locating the source of errors, or bugs. You can also use them to help you learn how codewritten by other people works. When you write your code, you will never be able to create code that works firsttime round perfectly. Or if you do, if you change another section of code, this may well affect code you havealready written which then does not work.

    Q94. When would an On Error statement be used?

    We must place an On Error statement at the beginning of the any procedure where error might occur, such asbefore opening sequential file for input.

    An on error statement will trap the error and it will direct the execution control to the error trap level. Actually ,

    there are four different statements that we can use when error occurs.

  • 7/29/2019 VB 91-102

    2/6

    (i) Resume Next:It tells VB that if an error occurs,the program should simply skip to the next lineofthe code and bypass the line where the error has occured.

    Example:

    Private sub cmdDelete_Click( )

    on Error Resume Next

    Stop

    Kill txtFile

    if err.Number

    MsgBox " such a file doesn't exit "

    Endif

    End Sub

    'Kill statement delete the specified file in the textbox

    (ii) Resume (Label Name ):This statement is used , if you want to transfer execution control.To somedifferent location or you just want to ignore the error

    Example:

    private sub cmdOpen_Click( )

    OnError GoTo ErrorTrap

    set db = OpenDatabase("C:\emp.mdb")

    Normal Exit:

    Exit Sub

    Error Trap:

    Msgbox " Failed to open the file "

    Resume NormalExit

    End sub

    (iii) Resume :When Error occurs and if you want to give the user a chance to fix the error , In that caesabove statement is used.

    Example:

    If your tries to access a floppy without putting it intothe disk drive , you application may ask you to insert it andthen continue with the execution again.

  • 7/29/2019 VB 91-102

    3/6

    private sub cmdResume_Click( )

    On Error Go To ErrorTrap

    MsgBox" Length of the file is " & FileLen( txtFile )

    Normal Exit:

    Exit sub

    Error Trap:

    If err.Number then

    If MsgBox (" Floppy drive error try again ") = vbYes then

    Resume

    Endif

    End Sub

    (iv) The errObject:Every time an error occurs vb creates an object, which contain erroe informationthis error object can be used to retrive the information about the type of error , which has occuredthe important properties of errorobject are as follows:

    Q95. What is cursor? Explain different types of Cursors.

    A cursor's type, in ADO terminology, indicates some facts about how it behaves, what you can and can't do withit, and how thrifty or wasteful it is with system resources.

    You can set a Recordset's cursor type by setting its CursorType just before you open it,

    The following sections discuss the four cursor types available from the ADO Cursor library.

    1. Forward-Only Cursors

    2. Static Cursors

    3. Keyset Cursors

    4. Dynamic Cursors

    Forward-Only Cursors

    A Forward-Only cursor behaves a lot like sequential file access: It only furnishes one record at a time, and thenonly in strict order from the beginning to the end of the rowset.

    In other words, you can't use a Forward-Only cursor to skip around in a Recordset's rows. You can only move

    forward one record at a time until you reach the EOF condition at the end of the Recordset.

    If you attempt to use any other Recordset navigation method besides MoveNext, you will generate a runtimeerror.

    A Forward-Only cursor is the default ADO cursor, because it consumes the least resources of all cursor types.

    Static Cursors

    Static cursors are less economical than Forward-Only cursors, but they allow greater flexibility of movementthrough the rowset. A Static cursor supports navigation in all directions, and it enables you to make repeatvisits to the same record during the same session.

    The biggest drawback to a Static cursor is the fact that its rowset doesn't get updated with concurrent changesmade by other users.

    http://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#forward_only_cursorshttp://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#static-cursorshttp://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#keyset_cursorshttp://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#dynamic_cursorshttp://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#forward_only_cursorshttp://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#static-cursorshttp://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#keyset_cursorshttp://visualbasic.freetutes.com/learn-vb6-advanced/lesson9/p14.html#dynamic_cursors
  • 7/29/2019 VB 91-102

    4/6

    If User A opens a Static cursor on a set of records and User B makes changes to the records during User A'ssession, for example, User A will not see the changes made by User B. To see User B's changes, User A's Staticcursor would have to close and then be reopened.

    The user can make changes to the Static cursor's Recordset, but (once again) the user cannot see changesmade by others during the time that the cursor is open. This includes additions and deletions to the records aswell as editing changes to individual records.

    Keyset Cursors

    Keyset-type cursors have the same freedom of movement in any direction as Static cursors. In addition, Keysetcursors can immediately see changes to existing records made by other users. However, additions anddeletions made by other users are not visible to a Keyset-type cursor.

    Dynamic Cursors

    Dynamic cursors have all the flexibility and visibility of Keyset-type cursors with an extra enhancement:Additions and deletions made by other users are visible to a Dynamic cursor. Dynamic cursors are, however,the biggest resource hogs, so you should be very sure that you absolutely need a Dynamic cursor beforedeciding to use one.

    Q96. Explain different debugging tools.

    There are several debugging tools available for use in Visual Basic. Debugging tools are designed to help youfor:

    1. Finding logic and run time errors2. Observing the behaviour of code that has no errors.

    The tools include Breakpoints: Definds a line in the code window where Visual Basic suspends execution of the

    application. watch window: Displays the value of selected expression.

    Immediate window: Allows you to execute code or query values while the application is in breakmode.

    Step into: Executes the next executable line of code in the application and steps into procedures.

    Step over: Executes the next executable line of code in the application without stepping intoprocedures.

    Step out: Executes the remainder of the current procedure and breaks at the next line in the callingprocedure.

    Local window: Displays the current value of local variables.

    Call stack: While in break mode presents a dialogue box that shows all procedures that have beencalled but not yet run to completion.

    Q97. Explain different types of Locktype.

    To support data integrity and avoid conflicts between users trying to update the same data at the same time(concurrency conflicts), most modern DBMSs support some sort of locking scheme.

    ADO recognizes four different types of data locking, represented by four enumerated constants:

    adLockReadOnly (default) - When a recordset is opened, the user may not make any changes to the

    data. This ensures that concurrency conflicts with other users are avoided.

    adLockPessimistic - Provider guarantees that a record under editing will be able to have its changes

    saved. This is usually accomplished by locking the record as soon as it becomes the current recordunder a cursor. The lock is released when the cursor moves off the record or the recordset is closed.

    adLockOptimistic - Provider does not guarantee that a record under editing will have its changes

    saved. Provider locks the record only during the update process.

    adLockBatchOptimistic - For server-side cursors, this option guarantees that all cursor options will be

    supported in the most efficient way.

    You can set the type of lock on the data underlying a Recordset by setting the Recordset's LockType propertyto one of the previously mentioned values before you open it.

  • 7/29/2019 VB 91-102

    5/6

    Q98. Explain how BOF and EOF properties are set and how they might be used in a project.

    Ans:

    1. Two handy properties of the record set are BOF (beginning of file) and EOF (end of file). The BOF property isautomatically set to true when the record pointer is before the first record in the record set. This conditionhappens when first record is current and the user choose MovePrevious. The BOF property is also true if therecord set is empty

    2. The EOF property is similar to BOF; it is true when the record pointer moves beyond the last record in therecord set and when the record set is empty.

    3. When you are doing your own navigation in code, you need to check for BOF and EOF so that run time errorsdo not occur. If the user clicks MoveNext when on the last record, what do you to do? Have the program cancelwith a run time error? Display a message? Wrap around the first record? Keep the record pointer to last record?

    4. In the following example, we will wrap-around method. If the user clicks on the MoveNext button from theend of the table, the first record becomes active record. Private Sub cmdNext_Click() , Move to Next record

    datBooks.Recordset.MoveNext

    If datBooks.Recordset.EOF Then

    datBooks.Recordset.MoveFirst

    End If

    End Sub

    Q99. What are database components?

    There are mainly three kinds of database components in UDA architecture. They are

    1) Data provider: It is a control or object that provides data for use with another control or program. Itmakes connectivity much easier by hiding most of the implementation from the user so that the usercan concentrate of on utilising data.

    2) Service components: Service component perform the task of processing the data and transmitting itto a data consumer.

    3) Data consumers: It is an application that retrieve data from a service component.

    Q100. What are the steps needed to add a new record to database?

    Ans:

    1. When you want to add new record you have couple of choices. If you are using data controls navigationbuttons, you can allow Visual Basic to do the adds automatically. Set the data controls EOF action property to2 AddNew. When the user moves to end of the file and clicks the arrow buttons, the Update method is

    automatically executed and the new record is added.

    2. You need a different approach when you use code to accomplish record navigation. Assume that you have acommand button to menu choice to add a new record. In the click event for the command button use AddNewmethod:

    3. datBooks.Recordset.AddNew

    4. When this statement executes, all bound controls are cleared so that the user can enter the data for the newrecord. After the data fields are entered, the new record must be saved in the file. You can explicitly save itwith an update method; or, it the user moves to anther record, the update method is automatically executed.

    5. You may want to use two buttons for adding a new record an Add button and s save button. For the Addbutton, use an AddNew method; for the Save button, use the Update method.

    6. When adding new records, some conditions can cause errors to occur.

    7. For example, if the key field is blank on a New record, a run time error holts the program

  • 7/29/2019 VB 91-102

    6/6

    execution.

    Q101. What are the steps needed to delete a record from database?

    Ans:

    1. The delete method deletes current record. The user should display the record to delete and click a Deletecommand button or menu choice.

    2. When a record is deleted, the current record is no longer valid. Therefore, a delete method must be followedby MoveNext method.

    With datBooks.Recordset

    .Delete

    .MoveNext

    End With

    3. But what if the record being deleted is the last record in the table?

    4. Remember that if the navigation buttons are used to navigate, moving beyond EOF just resets the currentrecord to the last record.

    Q102. What steps are needed to change the data in a database?

    Ans:

    The record set object has an Update method, which you can use to save any changes in the data. Most of thetime, updating is automatically executes the Update method any time the user clicks one of the navigationbuttons or one the Move methods executes.