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 April 1st, 2009, 08:44 AM
Authorized User
 
Join Date: Nov 2005
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
Default Adding dynamic data to a VBscript line

Hi,

As a newbie to VBscript I was wondering how to do the next:
I have some VBscript and want to make it dynamic. So I added some SQl script, but have no ide how to add it. this is my code:
Code:
Set conn = server.createobject("adodb.connection")
conn.open "DSN=XXXX"
set rs = server.createobject("adodb.recordset")
sql="Select * from table"
rs.open sql,conn
IF rs.eof THEN
ELSE
rr="offer.jpg"
%>

pic[0] = new banner("&rr&",102,offer2.asp')
Normally I use
Code:
("&rr&",
is ASp, but in VBSCRIPT how do I do this??

I am sure it is very simple to do.

thx
Reply With Quote
  #2 (permalink)  
Old April 1st, 2009, 08:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

If banner is some kind of ActiveX object, then *probably* you will do
Code:
pic[0] = new banner(rr,102,"offer2.asp")
But if banner is a VBScript class, you can't construct and initialize it in a single statement, unfortunately.

You'd have to do something like
Code:
b = New banner
b.initialize rr, 102, "offer2.asp"
pic[0] = b
But it's not clear to me how you are going to use an ASP page (that is, "offer2.asp") in a ".vbs" file.
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
rtr1900 (April 13th, 2009)
  #3 (permalink)  
Old April 1st, 2009, 08:08 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Wait a minute!!!

pic[0] isn't legal VBScript code!

And you can't use %> (or <%) any place *except* in ASP pages.

What the heck are you doing here???
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
rtr1900 (April 13th, 2009)
  #4 (permalink)  
Old April 2nd, 2009, 01:53 AM
Authorized User
 
Join Date: Nov 2005
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
Wait a minute!!!

pic[0] isn't legal VBScript code!

And you can't use %> (or <%) any place *except* in ASP pages.

What the heck are you doing here???
Hi Old Pedant, thx for responding.

I have an ASP file and I needed to make a kind of banner. Which I found some code which is as a script
Code:
<script>.....</script>
As it is static I wanted to connect it in a way to a table so that I can dynamically change the content of the banners.

And the only part I have to change is o make it from this
Code:
pic[0] = new banner('//192.168.100.1/offers/offer.jpg',102,'//192.168.100.1/offer.asp')
to something like
Code:
pic[0] = new banner('"&rr&"',102,'"&zz&"')
where rr and zz are data taken form a DB using SELECT

So maybe to make my question simpler: Is there a way where I take data for a Db using SELECT, and than "THAT" data put it in a variable in a a script? For using it.

Thx already!
Reply With Quote
  #5 (permalink)  
Old April 3rd, 2009, 08:41 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

So how come your original posted code doesn't work???

Let's look at it again. With minor corrections/adaptations.

Code:
<script type="text/javascript">
function banner( image, number, url )
{ 
     ... this is the constructor for a banner ...
}

var pic = new Array( );
<%
Set conn = server.createobject("adodb.connection")
conn.open "DSN=XXXX" ' If possible, AVOID using DSN's for connections!!!
sql="Select bannerImage, bannerNumber, bannerURL from table"
Set rs = conn.Execute( sql )
counter = 0;
Do Until rs.EOF
    image = rs("bannerImage")
    num   = rs("bannerNumber")
    url   = rs("bannerURL")
%>
    pic[<%=counter%>] = new banner("<%=image%>","<%=num%>","<%=url%>");
<%
    counter = counter + 1
    rs.MoveNext
Loop
rs.close
%>
Now, there *IS* a slightly more elegant way to do this, which is also a little faster, but it's harder to follow.

Code:
<script type="text/javascript">
function banner( image, number, url )
{ 
     ... this is the constructor for a banner ...
}

<%
Set conn = server.createobject("adodb.connection")
 conn.open "DSN=XXXX" ' If possible, AVOID using DSN's for connections!!!
sql="Select bannerImage, bannerNumber, bannerURL from table"
Set rs = conn.Execute( sql )
prefix = "new banner("""
midfix = ""","""
suffix1 = """)"
suffix2 = "," & vbNewLine

jsData = rs.getString( , , midfix, suffix1 & suffix2 & prefix )
jsData = prefix & Left( jsData, Len(jsData) - Len(suffix2) - Len(prefix) )

rs.Close
%>
var pic = new Array(
<%=jsData%>
    );
Here's an old FAQ I wrote that explains the above.
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
rtr1900 (April 13th, 2009)
  #6 (permalink)  
Old April 13th, 2009, 07:14 AM
Authorized User
 
Join Date: Nov 2005
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
So how come your original posted code doesn't work???

Let's look at it again. With minor corrections/adaptations.

Code:
<script type="text/javascript">
function banner( image, number, url )
{ 
     ... this is the constructor for a banner ...
}
 
var pic = new Array( );
<%
Set conn = server.createobject("adodb.connection")
conn.open "DSN=XXXX" ' If possible, AVOID using DSN's for connections!!!
sql="Select bannerImage, bannerNumber, bannerURL from table"
Set rs = conn.Execute( sql )
counter = 0;
Do Until rs.EOF
    image = rs("bannerImage")
    num   = rs("bannerNumber")
    url   = rs("bannerURL")
%>
    pic[<%=counter%>] = new banner("<%=image%>","<%=num%>","<%=url%>");
<%
    counter = counter + 1
    rs.MoveNext
Loop
rs.close
%>
Now, there *IS* a slightly more elegant way to do this, which is also a little faster, but it's harder to follow.

Code:
<script type="text/javascript">
function banner( image, number, url )
{ 
     ... this is the constructor for a banner ...
}
 
<%
Set conn = server.createobject("adodb.connection")
 conn.open "DSN=XXXX" ' If possible, AVOID using DSN's for connections!!!
sql="Select bannerImage, bannerNumber, bannerURL from table"
Set rs = conn.Execute( sql )
prefix = "new banner("""
midfix = ""","""
suffix1 = """)"
suffix2 = "," & vbNewLine
 
jsData = rs.getString( , , midfix, suffix1 & suffix2 & prefix )
jsData = prefix & Left( jsData, Len(jsData) - Len(suffix2) - Len(prefix) )
 
rs.Close
%>
var pic = new Array(
<%=jsData%>
    );
Here's an old FAQ I wrote that explains the above.
Hi Old Pedant,

Sorry for my late response, we had a holidayweek here.
Why my code didn´t work, I don´t know. But I used the first part which you posted and made some little modification, and it works perfectly.
This is the code:
Code:
<Script type="text/javascript">
function banner(name, width, link){
 this.name = name
 this.width = width
 this.link = link
   }
var pic = new Array();
<%
   
Set conn = server.createobject("adodb.connection")
conn.open "DSN=FARMA"
set rs = server.createobject("adodb.recordset")
sql="Select * from informe_oferta"
rs.open sql,conn
counter=0
DO WHILE NOT rs.EOF
%>
 pic[<%=counter%>] = new banner('<%=response.write(rs("foto"))%>',102,'<%=response.write(rs("url"))%>');

<%
counter=counter+1
rs.Movenext
LOOP
rs.close
%>
And it works now perfectly. may becauase my original had
Code:
<Script >
instead of
Code:
<Script type="text/javascript">
As I am very new to Javascript/VBscript, I can´t tell. But anyway, I have learned a lot with your help (like using ";" in Javascripts)

Again Old Pedant, thx again..... And next time I will vote for you again as best helper!!!

regards
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP vbscript dynamic image display johneecc Classic ASP Professional 10 January 31st, 2008 01:34 PM
help writing dynamic form data to dynamic table ublend SQL Server ASP 1 June 1st, 2007 08:09 AM
Adding a blank line to a memo field Scootterp Access VBA 3 October 31st, 2006 01:15 PM
Adding line feeds and apostrophes to a database RiffAff Classic ASP Professional 1 January 21st, 2006 05:40 PM





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