 |
| 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
|
|
|

April 1st, 2009, 08:44 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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
is ASp, but in VBSCRIPT how do I do this??
I am sure it is very simple to do.
thx
|

April 1st, 2009, 08:07 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
|

April 1st, 2009, 08:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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???
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
|

April 2nd, 2009, 01:53 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Old Pedant
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!
|

April 3rd, 2009, 08:41 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
|

April 13th, 2009, 07:14 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Old Pedant
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
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
|
|
 |