Quality Center Support and News
Showing results for 
Search instead for 
Do you mean 

nested loops in QTP

Occasional Visitor

nested loops in QTP

Hello all:
I am trying to do something like this in QTP
Add a customer (customer # is unique)
Once I add a customer I want to add multiple Orders for this customer in same script.
I am able to use a do -- while loop and add the first customer and first order however I am not able add the 2ne order to the same customer.
My code is something like this

Do while objExcel.Cells(rowNum,1) <> ""
add customer (using data from excel)
search for the customer using the customer #
add PO -- ( Here I need to add more than one PO for same customer )
rownum + 1

I am able to add the first PO but unable to add the 2nd PO

Honored Contributor Honored Contributor

Re: nested loops in QTP

This is how you nest loops in VBScript. you can put a loop of any type inside a loop of any type. If you want specific details about your situation, then paste the code you are trying to use.

cells = True
ct = 0
Do while cells
For i = 0 To 5
msgbox "ct=" & ct &" : inner=" &i
ct = ct +1

If ct = 5 Then
cells = False
End If
Occasional Visitor

Re: nested loops in QTP

Thanks Cody
My excel spreadsheet is something like this
when I put the for loop the same PO is being added again to same customer
The first time I am able to add the custnum and PO but next time it adds the same PO
The PO for each customer could vary.

CustNum CustName PONum PO Name
100 test 10 testpo1
100 test 11 testpo2
101 test2 13 testpo3
101 test2 14 testpo4
101 test2 15 testpo5

Do while DataTable("CustNum") <> ""

REM add customer number and name

TeWindow("TeWindow").TeScreen("yy1 ENTER").TeField("CustNum").Set Datatable("CustNum")
TeWindow("TeWindow").TeScreen("yy3 ENTER").TeField("CustName").Set Datatable("CustName")

REM search for the custnum added above

TeWindow("TeWindow").TeScreen("yy1 DISPLAY").TeField("field722").Set "2"
TeWindow("TeWindow").TeScreen("yy1 DISPLAY").TeField("field802").SetCursorPos

REM add multiple PONum for this customer

For i = 1 to 5
TeWindow("TeWindow").TeScreen("yy1 DISPLAY").SendKey TE_PF6

TeWindow("TeWindow").TeScreen("yy3 ENTER").TeField("PONum").Set "1"
TeWindow("TeWindow").TeScreen("yy3 ENTER").SendKey TE_ENTER

TeWindow("TeWindow").TeScreen("yy2 ADD").TeField("PO#").Set Datatable("PONum")
TeWindow("TeWindow").TeScreen("yy2 ADD").TeField("POName").Set Datatable("POName")

i = i + 1

Loop ' end of first loop
Regular Advisor Regular Advisor

Re: nested loops in QTP

Hi Sireesha,

Implement the following code.

l_RowCount = DataTable.GetRowCount()
For i=1 To l_RowCount
DataTable.SetCurrentRow i
...so on

Honored Contributor Honored Contributor

Re: nested loops in QTP

Yes, you need to add this line to the outer

DataTable.SetCurrentRow i

Also, you need to change the variable of the inner loop from "i" to something else. You cant use the same variable to track the increment of both outer and inner loops since.