Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
|
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 12th, 2008, 11:12 AM
Authorized User
 
Join Date: Aug 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert a string into a variable

Is there a way to convert a string and return the variable value
Here is some code to show what I'm doing.

dim conn, rs,strSQL,i, tempstr,strSQL1,strSQL2 'etc....

strconn = "connection string"
strSQL = "insert into Guestbook (Name,Comment) "
strSQL1 = "Values('Name 1','Comment 1')"
strSQL2 = "Values('Name 2','Comment 2')"
'etc.

Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
conn.Open strConn


for i=1 to 36
   tempstr = strSQL & Cstr("strSQL" & i)

'at this point I would want tempstr to be
"insert into Guestbook (Name,Comment) Values('Name 1','Comment 1')"
'but I'm getting "insert into Guestbook (Name,Comment) strSQL1

   rs.Open tempstr,conn
   rs.Close
next

conn.Close
set rs = nothing
set conn = nothing
Reply With Quote
  #2 (permalink)  
Old February 12th, 2008, 11:30 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The only way you can achieve this is if you put strSQL1 and strSQL2 into an array and made a call like this:

tempstr = strSQL & Cstr(arr[i])

You can not dynamically reference variables in the manner you have laid out above.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #3 (permalink)  
Old February 19th, 2008, 04:45 PM
Registered User
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I needed this same info for a different use and found an answer that worked. After seeing this answer I'm loading a dictionary and using the constructed string to match the key but....

I seemed to be able to cast the constructed string as my predefined variable by using Eval.

example:

strRegValue = Eval("g_str" & strName & "RegValue")
strMessage = Eval("g_str" & strName & "Msg")
strCommand = Eval("g_str" & strName & "Command")

wscript.Echo strRegValue
wscript.Echo strMessage
wscript.Echo strCommand

Returned my g_str......... variables for each strname in my loop.

I'm new here so hate to speak out but seems to work for me..
Reply With Quote
  #4 (permalink)  
Old February 20th, 2008, 08:46 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Thanks for the input Twisted. I was unaware of Eval() ability to do this. Personally I still prefer some sort of collection as opposed to dynamically referencing a variable name, but that's just me.

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #5 (permalink)  
Old February 21st, 2008, 09:08 AM
Authorized User
 
Join Date: Aug 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks,
I ended up creating an array and it works fine.
I'll give the eval() function a try.
I wasn't aware you could use eval that way.
I wonder if there is any difference in processing time between the two approaches.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert to string prafullaborade XML 7 May 29th, 2008 02:59 PM
Convert a variable John Anthony Access VBA 1 September 19th, 2003 10:58 AM
convert string to integer kcheung Classic ASP Databases 1 July 28th, 2003 04:39 PM
How to convert a string in DateTime elena ADO.NET 3 July 24th, 2003 04:31 PM
Convert variable to boolean stephanel Javascript How-To 2 June 25th, 2003 08:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.