Forum Discussion

nnguyen_1's avatar
nnguyen_1
Contributor
11 years ago

How do I set project variable (DB table type) by scripting?

In regards to the instruction at http://support.smartbear.com/viewarticle/28726, I should be able to set value for project variables by scripting. In VBScript, looks like this:



Project.Variables.AddVariable "MyVar", "String"

Project.Variables.MyVar = "Hello, world!"




My question is how do I set a DB table variable (particularly an instance of Excel file) by scripting? Is it possible to do something like this?




ProjectSuite.Variables.AddVariable "Test", "DB Table"        


ProjectSuite.Variables.Test.WorkBook = "C:\test.xlsx"


ProjectSuite.Variables.Test.WorkSheet = "TestDataSheet"



I tried but TestComplete throws run-time error all the time (no syntax error though). Any advice pls? 



P/S: I saw some code snippets to add column by column, value by value at http://support.smartbear.com/viewarticle/33254/. However, I think it's too much work, especially my excel file is created on the fly with hundreds of columns and rows, I cannot specify them all one by one. 




7 Replies

  • I sent a support ticket in november 2013 (TestComplete 9) and here is their answer:







    De : SmartBear QA Support [mailto:QA-Support@SmartBear.com]

    Envoyé : 8 novembre 2013 00:39

    À : Cadieux, Anik

    Objet : RE: [Issue# M0118168] Web Submission - How do I set project variable (DB table type) by scripting?

     

    Hello,

     

    Currently, assigning a source cannot be done in script. We have registered this request in our database. Thank you.

     

    You need to initialize a DB Table type variable in the Edit DB Table Variable Value wizard:

    http://support.smartbear.com/viewarticle/33544/

     

    Best regards,

    Vince





    I doubt this behavior has changed in TC 10. 
  • I found the solution, it required manually manipulating process, but I can get rid of the constants in project suite. 



    Pseudo code: 



      'Create an Excel.Application object


      Set objExcel = CreateObject("Excel.Application") 



      'Open the excel file


      objExcel.Workbooks.Open(strExcelFilePath) 



      'Check if the worksheet exists

     

      'If yes, activate it, if no, create and activate it



      'Move to the last row to append data (intLastRow)

      

      'Loop and Append data to Cells (intLastRow, column)



      'Save active worksheet and close it

     

      'Quit excel and destroy objExcel





    Note:  CreateObject("Excel.Application")  may return errors without reason in VBScript since VBScript doesn't support Early Binding (Read more here: http://msdn.microsoft.com/en-us/library/0tcf61s1.aspx). I handled it by using On Error Resume Next. 
  • I am looking for the exact same thing, Viper.

    If you ever find a way to set DB Table variable workbook and worksheet  directly from TestComplete's  functions in script, please let me know !
  • temich6059's avatar
    temich6059
    New Contributor
    I too am having the same sort of problem.  I've looked all over the Internet, but have not found any solution.  Would anyone here have an answer?



  • TCARREON14's avatar
    TCARREON14
    New Contributor

    Does anyone know if this issue has been resolved yet? is there a link to escalate or promote this so that they can include this in a future release? I am running TC11.33.2298 and am getting a type mismatch error when trying to set the variable.

     

    here is a snippet of my code:  I am passing over the Variable Name, Variable Type, and the Value. In this case is the path to a CSV file.

     

    if varType = "DB Table" then

        Project.Variables.VariableByName(varName) = "{CSV file: " & varValue & "}"

    else

        Project.Variables.VariableByName(varName) = varValue

    end if