I have a bunch of params:
InputParam1, InputParam2, InputParam3, etc...

I would like to loop through them instead of writing out code for each one, but am having some issues getting it to work correctly.

Here is what I have so far:

Dim iCount As Integer
Dim objTemp As Object
Dim strTemp As String
 
For iCount = 1 To 50
    objTemp = "InputParam" & iCount.ToString
    strTemp = CStr(objTemp)
Next

The major issue with what I have is that I cannot assign the string to the object and just creating the string gives me an error too.

Recommended Answers

All 10 Replies

Why not use arrays instead?

All the params are retrieved through a function...
Something like this:

Public Function RunSP(Param1, Param2, etc...)
'All of the stuff I have to do...
end function

Unless you have a better idea I would have to still type everything out to populate the array.

Generally I would agree with you, but in my current case (modding a second hand app) I would have to change a ton of code in a ton of other places to pull it off.

If it is at all possible I would REALLY like to be able to use the concantenated string if anyone has an idea of how to do it... Or if it is even possible.

Well, the eval() function from the scripting control might get you there:
http://support.microsoft.com/kb/184740

If you have to use that function a lot, it might be worth writing your own wrapper function to bundle those parameters into a parameter array.

I have a bunch of params:
InputParam1, InputParam2, InputParam3, etc...

I would like to loop through them instead of writing out code for each one, but am having some issues getting it to work correctly.

Here is what I have so far:

Dim iCount As Integer
Dim objTemp As Object
Dim strTemp As String
 
For iCount = 1 To 50
    objTemp = "InputParam" & iCount.ToString
    strTemp = CStr(objTemp)
Next

The major issue with what I have is that I cannot assign the string to the object and just creating the string gives me an error too.

Is this vb6?

Vb6 example to concatenate String delimited by space

Dim iCount As Integer
Dim strTemp As String
 
For iCount = 1 To 50
    strTemp = strTemp & " " & "InputParam" & iCount
Next

The out put is InputParam1 InputParam2 InputParam3 ...... InputParam50 Use of ParamArray

Public Function Concatenate(ParamArray MyArr() As Variant) As String
   Dim i As Integer
   Dim strTemp As String
   
   For i = 0 To UBound(MyArr)
      strTemp = strTemp & " " & MyArr(i)
   Next

   Concatenate = strTemp
End Function

Usage MsgBox Concatenate("Hi", "Welcome", "To", "VB", 6) Output Hi Welcome To VB 6

Is this vb6?

Vb6 example to concatenate String delimited by space

Dim iCount As Integer
Dim strTemp As String
 
For iCount = 1 To 50
    strTemp = strTemp & " " & "InputParam" & iCount
Next

The out put is InputParam1 InputParam2 InputParam3 ...... InputParam50 Use of ParamArray

Public Function Concatenate(ParamArray MyArr() As Variant) As String
   Dim i As Integer
   Dim strTemp As String
   
   For i = 0 To UBound(MyArr)
      strTemp = strTemp & " " & MyArr(i)
   Next

   Concatenate = strTemp
End Function

Usage MsgBox Concatenate("Hi", "Welcome", "To", "VB", 6) Output Hi Welcome To VB 6

I am currently getting the same output you got (param1, param2...). That is the issue I am having. I need something like strTemp = eval(param1) so that strTemp hold the value of param1. So if param1 = "hello"... StrTemp will then equal "hello" also.

I trying to get the eval to work, but am having no luck...

I currently have:

dim sControl as ScriptControl
dim strTemp as string
dim strTemp2 as string
sControl.Language = "vbscript"
for 1 to 50
strTemp = "inputParam" & iCount
strTemp2 = sControl.eval(strTemp) 'strTemp2 should now have the value of InputParam1, but instead get Runtime Error 91 - Object variable or with block variable not set
next

Any clue on what I am doing wrong or an explanation on how to get the ScriptControl to work? I have checked online, but have only found examples of how to use it, not getting it to work....

If anyone has any other ideas on how to take care of this I would really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.