Forum Discussion

pmaas's avatar
13 years ago

VBScript For Loop


I’m writing this vbscript in a in testcomplete.


 


I’m having trouble with a loop…  I don’t know if I need an If…else.. 


 


The script has a ADO connection that looks at a table in the database, I need it to populate fields in a program from that database table. 


 


My for loop fills the first Row/entry correctly.  But it seems that it just loops back to the first Row/Entry, it doesn’t move on to the second Row/entry in the table. I’m wondering what I need to add to the first for loop?


 


 


 


 




'Sales Rep Maintenance 


'Adding New Rep


'USEUNIT Seek


 


Dim txtServer, testCtrlDBase


txtServer = ProjectSuite.Variables.ServerName    


testCtrlDBase = ProjectSuite.Variables.TestControlDB


 


Sub Test1 


  Dim FirstName,LastName,Organization,CommissionType,CommissionRate,FixedFee,SafariUser 


  Dim control   


  Dim control2    ‘These aren’t being called / used


  Dim control3    ‘These aren’t being called / used


  Dim MyServer


  Dim MyDataBase


  Dim TypeID 


  Dim h,i


  TypeID = 1


  MyServer = "ENSQL2005"


  MyDataBase = "SAF416Pete"


  


  Call Aliases.Explorer.wndShell_TrayWnd.ReBarWindow32.MSTaskSwWClass.ToolbarWindow32.CheckItem("EnSite Safari Suite®", True, False)


 


   SeekScreen ("SalesAdmin.ucSalesRepMaint")


   Set control = Aliases.Sys.Safari.FormHost.MDIClient.FLC_SalesRepMX.FLP_SalesRepMX.LCC_SalesRepMX.Embedded.vseContainer 


  


    Set AConnection = ADO.CreateConnection


             AConnection.ConnectionString = "Driver={SQL Server};Server=" + MyServer + ";Database=" + MyDataBase + ";Uid=SafariUser;Pwd=safari" 


             AConnection.Open


         Set Cmd = ADO.CreateCommand


             Cmd.ActiveConnection = AConnection


             Cmd.CommandText = "Exec qa_SelectSalesRepData " & TypeID      ‘stored procedure qa_SelectSalesRepData


             Cmd.CommandType = adCmdText


         


           Set RecSet = Cmd.Execute


           RecSet.MoveFirst


    


          For h = 0 to RecSet.Fields.Count -1   ‘Loop in question Trying to retrieve another name from the database Table


                control.keys("~N") 


 


            


                 For i = 0 to RecSet.Fields.count -1                  


 


                    Select Case RecSet.Fields(i).name


                          Case "FirstName" 


                      control.txtFirst.SetText(RecSet.Fields(i).value)                 


                          Case "LastName"


                      control.txtLast.setText(RecSet.Fields(i).value)


                          Case "Organization"


                      control.txtOrganization.SetText(RecSet.Fields(i).value)


                          Case "CommissionType"          


                      control.cboCommissionType.ClickItem(RecSet.Fields(i).value) 


                          Case "CommissionRate"


                      control.txtCommissionRate.SetText(RecSet.Fields(i).value)


                          Case "FixedFee"                     


                      control.txtFixedFee.SetText(RecSet.Fields(i).value)


                          Case "SafariUser"                   


                      control.cboSafariUser.ClickItem(RecSet.Fields(i).value)  


                          Case else 


                              Log.Message "Need coding to handle " & RecSet.Fields(i).name & " and " & RecSet.Field(i).value &" for Test1: " 


                    End Select                


                 Next


            control.keys("~S")


 


          Next 


End Sub



 

3 Replies


  • Hi,



    i am a newbie with vbscript and would apppreciate if you can help me. I have a for loop below and i am having problem on how the loop will exit whenever the inputbox is blank or the loop will exit if Cancel is click..below is my for loop vbscript....Thanks in Advance.



    Dim fso, tf

    'Dim Msg As Variant

    Dim loopIndex

     Set fso = CreateObject("Scripting.FileSystemObject")

     Set tf = fso.CreateTextFile("c:\Test\pcname.txt", True)

    For loopIndex = 0 To 5

     Msg = InputBox("Input PC Name in FQDN Format (e.g. PC1.domain.com) Then click

    OK button to proceed Or Click Cancel to Terminate.", "Input Window") 

     tf.WriteLine(Msg)

    Next

    tf.Close



    Danny


  • This is for Danny's question (not original post).



    Just before the Next; add a test for Msg = notthing.  If true, set loopindex to 5 (this should terminate the loop).



    I hope that this helps.



    Wayne Homens