Micro Focus QTP (UFT) Forums
VBScript - How to get the Function return value - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: VBScript - How to get the Function return value (/Thread-VBScript-How-to-get-the-Function-return-value)



VBScript - How to get the Function return value - iamsekhar - 09-08-2010

Hi All,
I have scenario like
1. i am passing value (rowCnt) and file name (fileName) to the function
2. Inside Function i am reading xls file and getting some row count (row_count)
3. In side the function i can able to compare rowCnt = row_count by using reporter.ReportEvent

My question is: I want to do use "reporter.ReportEvent" out side the function.
4. How to write if condition inside function to satisfy my scenario.

Code:
Window("Lilly Science Grid").Page("Page_3").Link("25").Click
rowCnt = Window("Lilly Science Grid").Page("Page_3").Link("25").GetROProperty("text") '
convLng = CLng(rowCnt) ' Convert to String to Long
[b]retFunc = GetRowCount(convLng, fileName) [/b] // Function calling

fileName = "D:\SaveXlsFile.xls"

[b]Function GetRowCount(convLng,fileName)[/b]
SwfWindow("Lilly Science Grid").Dialog("File Download").Click 285,18
SwfWindow("Lilly Science Grid").Dialog("File Download").Activate
SwfWindow("Lilly Science Grid").Dialog("File Download").WinButton("Save").Click
Dialog("Save As").Activate
Dialog("Save As").WinEdit("File name:").Set "D:\SaveXlsFile.xls"
Dialog("Save As").WinButton("Save").Click
Dialog("Save As_2").WinButton("Yes").Click
'Dialog("Download complete").Click 321,184
'Dialog("Download complete").WinButton("Close").Click

Set xlObj = CreateObject ("Excel.Application") ' Create Excel object
Set wBook = xlObj.workBooks.Open(fileName) ' Pass file name
Set sheetName = wBook.worksheets("Sheet1") ' Pass default sheet name
row_count  = sheetName.usedrange.rows.count -1' 'Return total number of rows

If row_count  = convLng Then
// I want to write below code out side the function but i have to validate [b]if condition here only (If row_count  = convLng )[/b][b]
    reporter.ReportEvent micPass, "Row Count Validation","Row Count matching with column header. Step.Pass"
    else
    reporter.ReportEvent micFail, "Row Count Validation","Row Count is not matching with column header.Step.Fail"
End If
wBook.close
End Function

Kindly suggest how to approach [/b]and i will appreciate your help


RE: VBScript - How to get the Function return value - QTPLearn - 09-08-2010

Hi

Two Ways:

Code:
Function GetRowCount(fileName)
SwfWindow("Lilly Science Grid").Dialog("File Download").Click 285,18
SwfWindow("Lilly Science Grid").Dialog("File Download").Activate
SwfWindow("Lilly Science Grid").Dialog("File Download").WinButton("Save").Click
Dialog("Save As").Activate
Dialog("Save As").WinEdit("File name:").Set "D:\SaveXlsFile.xls"
Dialog("Save As").WinButton("Save").Click
Dialog("Save As_2").WinButton("Yes").Click
'Dialog("Download complete").Click 321,184
'Dialog("Download complete").WinButton("Close").Click

Set xlObj = CreateObject ("Excel.Application") ' Create Excel object
Set wBook = xlObj.workBooks.Open(fileName) ' Pass file name
Set sheetName = wBook.worksheets("Sheet1") ' Pass default sheet name
row_count = sheetName.usedrange.rows.count -1' 'Return total number of rows
[b]GetRowCount=row_count[/b]
End Function

retFunc = GetRowCount( fileName) // Function calling
retFunc will store the value and you can use this in your script.

2nd Approach:
Code:
Function GetRowCount(BYVal convLng , fileName)
SwfWindow("Lilly Science Grid").Dialog("File Download").Click 285,18
SwfWindow("Lilly Science Grid").Dialog("File Download").Activate
SwfWindow("Lilly Science Grid").Dialog("File Download").WinButton("Save").Click
Dialog("Save As").Activate
Dialog("Save As").WinEdit("File name:").Set "D:\SaveXlsFile.xls"
Dialog("Save As").WinButton("Save").Click
Dialog("Save As_2").WinButton("Yes").Click
'Dialog("Download complete").Click 321,184
'Dialog("Download complete").WinButton("Close").Click

Set xlObj = CreateObject ("Excel.Application") ' Create Excel object
Set wBook = xlObj.workBooks.Open(fileName) ' Pass file name
Set sheetName = wBook.worksheets("Sheet1") ' Pass default sheet name
[b]convLng [/b]= sheetName.usedrange.rows.count -1' 'Return total number of rows
End Function

Then Use the convLng variable in your code.

Do let me know if you have any other question.


RE: VBScript - How to get the Function return value - lotos - 09-13-2010

assign to the FunctionName the returnedValue, for e.g. if:

Code:
Public Function DateConversion
    Set DateConversion = New QTPDateConversion
End Function

Class QTPDateConversion

' the function is converting the Todays computer's date to the date format: "1YYMMDD"
    Function TodaysDate
        ' SystemDate contains the current system date from the computer.
            SystemDate = Date
        ' adding one more day to output current date, if CurrentDate = 18, output will be 19
            'strNewDate = DateAdd("d", 1, SystemDate)
        ' converting date to a string ( will be something like e.g. (depends on system date): 02192010 -> ddmmyyyy
            fctReturn = Cstr(SystemDate)
        ' LeftString contains first two elements, from the "02192010" string -> 02
            LeftString = Left(fctReturn, 2)    ' date
        ' MidString contains the next 2 chars starting with the 3rd char.
            MidString = Mid(fctReturn, 4, 2)    ' month
        ' Returns the last two chars from the "02192010" string -> 10
            RightString = Right(fctReturn, 2)    ' year (two numbers)
        ' converting the string into the format we need it
            ConvertedDate = "1" & RightString & MidString & LeftString    ' format = 1100923

        ' assigning the output value
        TodaysDate = ConvertedDate
    End Function
End Class

To call this function we will use next:
DateConversion.TodaysDate

DateConversion = class' name
TodaysDate = function's name