Subscript out of range in MSFlexgrid - vb6

I am using msflexgrid in VB6. How can I remove or resolve the following error:
Subscript out of range.
With flxData(0)
For i = 1 To .Rows - 1
Do While cboselect <> .TextMatrix(i, 1)
.RemoveItem (i)
Loop
Next i
End with

Just realized that the Do While loop inside of the For loop will cause the error to display - once a row doesn't have the desired cboselect value, it's going to call RemoveItem for that and all the remaining rows, until it's deleted all of them, then it'll display the message when it tries to delete a (now) non-existent row.
I'm guessing that you want to be removing just the rows that don't match the cboselect value, so that would call for an If statement. You also do need to run the For loop backwards. Try this:
With flxData(0)
For i = .Rows - 1 to 1 Step -1
If cboselect <> .TextMatrix(i, 1) Then
.RemoveItem (i)
End If
Next i
End With

Related

Error handling in UFT

Please suggest me how to handle the scenario. Problem is supposed I got an error
at statement 2 during execution of the first iteration for Row 1 from excel then how to skip remaining statement and start the execution of statement 1 with excel row 2.
Browser("ABC").Page("ABC").WebEdit("ABC").Set "123"
Dim i
Dim iRow
iRow = datatable.GetRowCount
For i = 1 to iRow
Statement 1- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 2- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 3- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 4- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 5- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
datatable.SetNextRow
Next
Excel sheet
Row 1 Row 2 Row 3
step1) create a new Action with Name "Action2" and put the code from inside for loop
step2) import data to Action2 local data table
step3) create a recovery scenario enable check on every line with recovery function as
function RecoveryFunction()
ExitActionIteration
End function
step4) from Action1 call Action2 with allIterations
Firstly, if your test is set to Run on all rows in File->Settings->Run then you do not require the loop you have in the code - UFT knows it needs to iterate for each row in your data table.
Secondly, all you need is the statement followed by a check on its success and any error handling. IN the event of the error, just exit the test iteration, and UFT will move onto the next one.
My reworked version of your code:
Browser("ABC").Page("ABC").WebEdit("ABC").Set "123" ' sets an initial value
Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT1", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("ABC").GetROProperty("value") <> DataTable("DT1", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("CDE").Set DataTable("DT2", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("CDE").GetROProperty("value") <> DataTable("DT2", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("FGH").Set DataTable("DT3", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("FGH").GetROProperty("value") <> DataTable("DT3", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("JKL").Set DataTable("DT4", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("JKL").GetROProperty("value") <> DataTable("DT4", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("MNO").Set DataTable("DT5", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("MNO").GetROProperty("value") <> DataTable("DT5", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
I have assumed that your repeated setting of Statement 1 etc in the question is supposed to emulate setting a number of different WebEdit text boxes and coded 5 sets as an example. If, as the iteration progresses, any of the text boxes is not set to the specified data table column value (DT1 to DT5 in my example) then the If condition will be satisfied, causing the attempt to handle an error popup (you'd need to amend my guessed code to suit your requirements) followed by the exiting of the test iteration. Exiting the iteration will trigger a new one to be started, so just be sure you handle any error popup before you exit.
If all WebEdits are completed successfully, then UFT will just repeat for all iterations defined in your data table.
You need to decide one thing: are you going to use the UFT way of Data-Driven testing or you would do the whole controlling yourself
Option 1 - Do the UFT Way
Tell Uft to interpret the DataTable properly - meaning execute the Test Case as many times as many rows there are in the Global Sheet: File->Settings->Run-> Run on all rows should be checked
Tell UFT that we are in a Datat-Driven mode. At the same page set the Setting; When error occurs during run session to proceed to Next Action Iteration - uft will start the whole thing with the beginning but next line
Add the five statements in Action 1 (That's all, UFT Takes care of the rest)
Option 2 - Do it in your way
Seems like you are already doing. You implement the Loop and iterate over all the lines of the Table
Extract the 5 Statements into a function (and Function Library)
Create a function that calls the previous one in a supervised manner (and Function Library)
PSEUDO-CODE
ACTION1
ForEach row In DataTable.GlobalSheet
ExecuteStatementsSupervised row
Next
FUNCTION LIBRARY
Function ExecuteStatementsSupervised(row)
On Error Resume Next
ExecuteStatements row
If Err.Number <> 0 Then
Reporter.ReportEvent micFail, "OOPS Failed"
End If
On Error GoTo 0
End Function
SubExecuteStatements(row)
Statement1
Statement2
....
Statement5
End Sub
So in Action1 we iterate over the DataTable, in the Supervisor function we catch the error if anything happens and make sure that the iterations can go on. The real function per se, just executes the steps

Looping error, too many records added

Ive been trying to write Access VBA code to automate the addition of replicates for germination tests.
Basically I have a form where I enter the total number of Reps (NoofReps) and the number of seeds per rep (RepSize) (e.g. 50 seeds). For each record added I want it to automatically add a record for each rep and automatically calc the Rep Number (i.e if i have 4 reps then it should add 4 records, numbered 1-4 reps) as well as the RepSize (e.g 50).
I have been trying out various loops based on information from this forum and other but am still getting errors with the number of records that it generates. I have tried both the "Do while" and "Do Until" but get the same result below either way.
Could someone please let me know where I am going wrong?...If i want 2 reps then it adds 2, If i want 3 then its 246, and if i want 4 it adds >30,000!!!
For the purposes of trying to fix the code I have started to type the number of reps manually into the code in the iNoofReps so that I know the error is in the code and not from the form.
Private Sub CmdAddReps3_Click()
Dim iRepNo As Integer ' stores the current value in the series
'Open the table
Set db = CurrentDb()
Set rstGReps = db.OpenRecordset("tblGReplicates")
' Initialise the variables
iRepNo = 1
iNoofReps = 3 'iNoofReps = Me.txtNoofReps
' Add the records using a loop
rstGReps.movefirst
Do 'Until rstGReps("RepNo") = (iNoofReps + 1) ' always want to include at least 1 repNo
rstGReps.AddNew
rstGReps("GTestID") = Me.GTestID
rstGReps("RepNo") = iRepNo
rstGReps("NoofSeed") = Me.txtNoOfSeeds
' Calculate the next RepNo value in the loop
iRepNo = iRepNo + 1
rstGReps.Update
rstGReps.moveNext
Loop Until rstGReps("RepNo") = (iNoofReps) + 1 ' so that the loop includes the final repNo.
MsgBox "Finished Looping"
rstGReps.Close
Set rstGReps = Nothing
Set db = Nothing
End Sub
Any help would be appreciated!!!
Well, you're moving next here: rstGReps.moveNext, and then you're comparing rstGReps("RepNo") = (iNoofReps) + 1 after moving next, thus being on an empty record, thus always equating to false.
Loop Until iRepNo = (iNoofReps) + 1 should fix it, then you're no longer referring to the recordset, which has already been set to the next record by the time you're referring to it.
You could also fix it by just eliminating this line:
rstGReps.moveNext
Since rstGReps.AddNew already moves the recordset to a new blank record, moving it forward after adding the record doesn't make much sense. If you remove it, you might want to remove the + 1 in Loop Until rstGReps("RepNo") = (iNoofReps) + 1

Assign values in a loop in Classic ASP

I have this setup here:
'highest number of days and lowest
niedrigsterTag = 8
hoechsterTag = 8
dim tageV(), tageB()
redim tageV(7), tageB(7)
'day-mapping
tageV(0) = replace(rs("TagVon"),"Mo", 1)
tageV(1) = replace(rs("TagVon"),"Di", 2)
tageV(2) = replace(rs("TagVon"),"Mi", 3)
tageV(3) = replace(rs("TagVon"),"Do", 4)
tageV(4) = replace(rs("TagVon"),"Fr", 5)
tageV(5) = replace(rs("TagVon"),"Sa", 6)
tageV(6) = 7
'for example: mo - fr
for each item in tageV
'save smallest weekday
if(isNumeric(item)) then
if(item <= niedrigsterTag) then
niedrigsterTag = item
Response.write(niedrigsterTag)
response.end()
end if
end if
next
As you might see, I'm pretty new into classic ASP. I don't understand what I'm missing on my loop. In pseudocode, it looks fine:
for each numeric value in my array, check if the current value of item is <= the current maxValue (hoechsterTag) - which is in the first iteration 8. If so, override the current value.
Now I'm stuck. I added a response.end() in the most-inner if. However, niedrigsterTag has a value of 7 instead of 1. Also, during the 1st iteration, item should be 1, right? For me it is 7. I imagined response.end() is an equivalent to PHP's die()
What I'm trying to realize:
if current iteration < current value, override it, so I'm ending up with the smallest value.
I know this is pretty basic, and so far I hadn't problems doing stuff like this in other languages. Don't know why this makes it so special.
Thank you for any hints and advices
When you are assigning the values to your tageV array, you are assigning them as strings and then comparing them to integers. You need to compare like datatypes for one.
Also, they way it is written is like this: In the first iteration, if the item is 1 and niedrigsterTag is 8 then niedrigsterTag is changed to 1 and response.end stops the loop and exits. What you need is more like this:
'highest number of days and lowest
niedrigsterTag = 8
hoechsterTag = 8
dim tageV(), tageB()
redim tageV(7), tageB(7)
'day-mapping
tageV(0) = replace(rs("TagVon"),"Mo", 1)
tageV(1) = replace(rs("TagVon"),"Di", 2)
tageV(2) = replace(rs("TagVon"),"Mi", 3)
tageV(3) = replace(rs("TagVon"),"Do", 4)
tageV(4) = replace(rs("TagVon"),"Fr", 5)
tageV(5) = replace(rs("TagVon"),"Sa", 6)
tageV(6) = 7
'for example: mo - fr
for each item in tageV
'save smallest weekday
if(CInt(item) <= niedrigsterTag) then
niedrigsterTag = CInt(item)
end if
next
Response.write("niedrigsterTag = " & niedrigsterTag)
This loops through the array and every time it finds a smaller value, the variable is assigned that value. Once the loop is done, the variable will hold the smallest value.
By the way, the reason you were getting 7 was because that was the only value that was making it past the if statements.

VBA code with multiple For loops running very slow

my code is running very slow, is it the 2 For loops causing it?
Thanks
For x = LBound(dataArray) To UBound(dataArray) 'define start and end of array, lower bound to upper bound
For Each rngcell In Range("A:B") 'lookup each cell in row 1
If dataArray(x) = rngcell.Value Then ' if cells in header row match with values in array
rngcell.EntireColumn.Copy ' then copy whole column of data for that parameter
Sheets(3).Select ' select sheet to paste data
Range("A:B").End(xlUp).Offset(rowOffset:=0, columnOffset:=x).Select 'select area to paste, paste in next column - no. x
Selection.PasteSpecial xlPasteValues ' paste
End If
Next rngcell ' next header row cell
Next x
End Sub
Just a few suggestions:
Doing .Select causes Excel to update the UI, which is expensive. Try to calculate the target cell/range ONCE and use that to call PasteSpecial and not Selection.
Selecting the Sheet(3) could be done before the loop, as it doesnt change.
IF (!) max. ONE dataArray Element matches ONE rngcell.Value, you could abort the rest of the inner loop by using Exit For before the End If, saving the useless rest of the loop.
You're using Range(A:B) which is definitely slowing your code down.
Excel will basically read every cell in that range according to your code.
That's 2 million cells.
Try to limit the range on the B by using something like Replace(Range("B").End(Xldown).Address,"$B$","").

Search WebList for particular item count from Excel sheet

I need a help in VBScript or either in QTP for the below case.
For example:
I have nearly 40 items in the weblist. I have only one item in the Excel sheet that is one among the 40 in the weblist. If I run the script, the one in the Excel should be select in the weblist. How do I perform this? I tried many scenarios, but couldn't get it to work.
Below are some of the sample pieces of code I tried in QTP:
ocount=Browser("name:=brw").Page("title:=brw").WebList("htmlid:=tabContainerBrandSite_123&rtyoh").GetROProperty("items count")
msgbox ocount
var7=mySheet2.Cells(2,"C")
For k=2 to ocount
ocount2=Browser("name:=brw").Page("title:=brw").WebList("html id:=tabContainerBrandSite_123&rtyoh").GetItem(k)
msgbox ocount2
merchantName = DataTable("Merchant_Name","Global") 'an example if value is saved in global sheet
items_count = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetROProperty("Items Count") 'This will get all the items from your weblist.
i = 1
Do
webListName = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetItem(i)
'this will get first value from the web list
If merchantName = webListName Then 'comparing first value from your value from global sheet
Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").Select(i) 'selects that value
Exit do 'because it has found your only value from the local sheet, it exits
else
i = i + 1
End If
Loop While i <= items_count

Resources