Sunday, October 11, 2009

QTP Script: Checking if Data table sheet exists

Is there any way to check if a DataTable sheet exists? Yes, offcourse. Open the DataTable and see manually. Now, is there any way to check through code if a DataTable sheet exists? Hmmmm.. Yes it is.

I have created a small algorithm to check if a HP QTP DataTable sheet exists or not. The algorithm goes here:

Step I : Use the On Error Resume Next statement to disable any error popups.
Step II : Use the DataTable's GetSheet method to return the specified sheet. The following syntax should be used:
DataTable.GetSheet("Your Sheet")
Step III : Check for an error number using Err.Number. If case of no error, Err.Number should return 0.

With the above algorithm, our QTP Script goes here:

'Disable Error Reporting in QTP
On Error Resume Next

'Check for the existence of a sheet inside QTP

If Err.Number<>0 Then
Msgbox "The specified Sheet does not exists"
Msgbox "The specified Sheet exists"
End If

'Enable Error Reporting in QTP
On Error Goto 0

Click here to see the script for Checking if Data table Parameter exists