Search code examples
qtphp-uft

QTP/UFT - Access multiple datasheets in loop


I am comparatively new to QTP/UFT. I am writing a test and need to use data from Global as well as Local Data Sheet in same test.

My for loop is something like:

Datatable.GetSheet("Global")
RowCount = Datatable.GetRowCount
For Cntr = 1 to RowCount
    Datatable.SetCurrentRow(Cntr)
    msgbox Datatable("Form", dtGlobalSheet) 'Form is my column Name from Global Data Sheet'

    Datatable.GetSheet("Action1")
    RowCount2 = Datatable.GetRowCount
    For Cntr2 = 1 to RowCount2
        Datatable.SetCurrentRow(Cntr2)
        msgbox Datatable("Number", dtGlobalSheet) 'Number is my column Name from Action1 Data Sheet'
    Next
Next

My column values are getting messed up from both sheets.


Solution

  • You need to assign your datatables to variables, in order to better work with it.

    • You are calling Datatable.GetSheet("Global") but not assigning it to anywhere.
    • When you use Datatable.GetRowCount you are not actually telling UFT from which datatable to get the rowcount and that is probably one of your problems.
    • In the end of your code you are using msgbox Datatable("Number", dtGlobalSheet) but you should probably be using msgbox Datatable("Number", dtLocalSheet) (assuming you assigned those variables somewhere in your code).

    Check this possible solution (not tested):

    Dim dtGlobal : Set dtGlobal = Datatable.GetSheet("Global")
    Dim dtLocal : Set dtLocal = Datatable.GetSheet("Action1")
    RowCount = dtGlobal.GetRowCount
    For Cntr = 1 to RowCount
        'Working with global datatable
        dtGlobal.SetCurrentRow(Cntr)
        msgbox dtGlobal.GetParameter("Form") 'Form is my column Name from Global Data Sheet'
    
        'Working with local datatable
        RowCount2 = dtLocal.GetRowCount
        For Cntr2 = 1 to RowCount2
            dtLocal.SetCurrentRow(Cntr2)
            msgbox dtLocal.GetParameter("Number") 'Number is my column Name from Action1 Data Sheet'
        Next
    Next
    

    PS.: After defining exactly what data is coming from which datatable, you may want to check your loop to make sure your logic is correct, as I did not check that part. Let me know if this works for you.

    Happy coding.