?
Solved

Database validation in VBScript

Posted on 2003-03-18
16
Medium Priority
?
319 Views
Last Modified: 2007-12-19
Hi All,

I want to bbuilt the following VBScript function

function ValidateRTN( sRTN )
    {  
                'Connect to the fileNet library database
          Dim cnFN2
          Dim RSFN2
         
          Set cnFN2 = Server.CreateObject("ADODB.Connection")
          Set RSFN2 = CreateObject("ADODB.Recordset")
         
          cnnFN2.Open C_DB_CNNSTR_FNETLIB2
         
          Dim strSQL
          sSQL = "SELECT * from TEMP_CUBS WHERE E_NAME = " & sRTN              
          RSFN2.Open strSQL, cnFN2, adOpenStatic, adLockReadOnly
         
          If  ( RSFN2.EOF and RSFN2.BOF ) then  
               response.write "return false"
          else
              response.write "return true"
          end if    
         
          RSFN2.Close
          Set RSFN2 = Nothing
          Set cnFN2 = Nothing    
         
   
    }



Since I'm new to this whole world of VBScript, How can I access the database within my VBScript function? How can I return a value from one vbscript fn to another vbscript function? Why can't I simply write plain sql statements like above in a vbscript function??

Any help is greatly appreciated.

0
Comment
Question by:khatrij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +2
16 Comments
 
LVL 11

Accepted Solution

by:
sciber_dude earned 252 total points
ID: 8163095
A function is a client side property where as ASP is server side. So you cannot write SQL statements within a function.

A possible way of doing it is redirecting it to an ASP page and capturing the output.
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 8163118
when the function gets triggered,
redirect the page to this asp page. which has your code.

========================================================
 <%
 Dim cnFN2
 Dim RSFN2
 
 Set cnFN2 = Server.CreateObject("ADODB.Connection")
 Set RSFN2 = CreateObject("ADODB.Recordset")
 
 cnnFN2.Open C_DB_CNNSTR_FNETLIB2
 
 Dim strSQL
 sSQL = "SELECT * from TEMP_CUBS WHERE E_NAME = " & sRTN
 RSFN2.Open strSQL, cnFN2, adOpenStatic, adLockReadOnly
 
 If ( RSFN2.EOF and RSFN2.BOF ) then
 response.write "return false"
 else
 response.write "return true"
 end if
 
 RSFN2.Close
 Set RSFN2 = Nothing
 Set cnFN2 = Nothing
 %>
 
=====================================================
and change the following code

 If ( RSFN2.EOF and RSFN2.BOF ) then
 response.write "return false"
 else
 response.write "return true"
 end if

TO

 If ( RSFN2.EOF and RSFN2.BOF ) then
 response.write "return false"
 else
 response.redirect "firstpage.asp?return=true"
 end if
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 8163133
oops.. forgot to mention..

you also need to have Request.Querystring("return") on the original page and that can be used to validate the other stuff.  

A good source for ASP / ADO is www.w3schools.com
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Assisted Solution

by:gladxml
gladxml earned 248 total points
ID: 8163138
khatrij,

Dont confuse yourself of server side and client side vbscripting...

What you are trying to accomplish here is a server side validation with interaction witht the db..

Also your syntax is incorrect cause does not use this { } but instead jscript...

tyr to check out the link below might help...

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20554164.html
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 8163147
If you want more info, to would help if you could copy and paste more code of how you were originally planning to use the

response.write "return true"

from the function ValidateRTN( sRTN )
0
 
LVL 2

Expert Comment

by:ActiveMedia
ID: 8163153
@

khatrij

You CAN have server-side functions or procedures (sub) written in ASP/VBScript and using SQL.  

But they must be run on the server, not the client browser.  The output from the function/sub can of course be sent to the browser using response.write .

If you are using ASP/VBscript, don't use those curly brackets { } as they are for use with JavaScript.

HTH

@
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 8163175
good point gladxml @ { and }

VBScript has function and end function where as Javascript has function { and }.

khatrij,
www.w3schools.com is also a great source for VBScript, JavaScript, SQL and of course ASP / ADO

Hope this helps
0
 
LVL 15

Expert Comment

by:gladxml
ID: 8163193
Like this this will not return return true and return false is only applicable for client side scritpting...

this will write the data return true and return false... but will not

If ( RSFN2.EOF and RSFN2.BOF ) then
response.write "return false"
else
response.write "return true"
end if


try to check out the link might help...

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20546591.html

0
 
LVL 15

Expert Comment

by:gladxml
ID: 8163284
Like the below code

If ( RSFN2.EOF and RSFN2.BOF ) then
response.write "return false"
else
response.write "return true"
end if

return true and return false assuming that it is a client side validation will make the page not submit if there are errors encountered... This kind of validation is called when you hit a button or do an action that will make the page to be submitted...

On the code above the only thing that it will be doing is that it will only write on the browser the return true and return false...

The link on my previous will help understand on hoe you can use the client side validation...

Also as an advise it is much better to use javascript on client validation since it is cross platform...

And since VBscript is default language of asp many prefer and almost all tutorial uses vbscript (but not all cause some uses jscript) on server side...


HTH...

HAppy programming...





0
 

Author Comment

by:khatrij
ID: 8163292
oops, I don't want to do any response.write but instead,

If  ( RSFN2.EOF and RSFN2.BOF ) then  
   return false
else
    return true
end if  

And, I'm calling this function from another Vbscript function like this:

function validateAll()
{
if ( !ValidateRTN( sRTN ) )
{
  document.all.item("ididmDocCustom4").focus();
  var agree = confirm("Invalid RTN.\nSave anyway?");
  if ( agree != true ) return false;
     document.all.item("idIdmDocCustom13").checked = true;
     return true;  
}
}

How can I return true or false??
0
 

Author Comment

by:khatrij
ID: 8163328
And both of this function stays in the same page.
0
 
LVL 15

Expert Comment

by:gladxml
ID: 8163345
khatrij,

As I had told on my previous post you dont confuse yourself on server side and client side scripting


client side is run on the client machine



server side is run after submitting the page this also include interaction with the database like query upadte insert and other server side validation...

Also cannot be run like that... You need to use Onsubmit event to call this javascript function... just like in the code in the link that I had posted...

function validateAll()
{
if ( !ValidateRTN( sRTN ) )
{
 document.all.item("ididmDocCustom4").focus();
 var agree = confirm("Invalid RTN.\nSave anyway?");
 if ( agree != true ) return false;
    document.all.item("idIdmDocCustom13").checked = true;
    return true;  
}
}


0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 8163374
could you paste the whole code so that we can give you the a response that would work?

It seems to me that on clicking submit, you want to validate the variable sRTN if it is present in the database table.

If its is not present, it will pop up an alert if you wish to continue.

If yes, some checkbox(es) get checked.

Is that all? or do u want it to write to the database too?
0
 
LVL 15

Expert Comment

by:gladxml
ID: 8163383
khatrij,

KIndly post the code so that I can help in any way that I can...
0
 
LVL 58

Expert Comment

by:Gary
ID: 9315869
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split Points - gladxml / sciber_dude

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question