[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Database validation in VBScript

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
khatrij
Asked:
khatrij
  • 6
  • 5
  • 2
  • +2
2 Solutions
 
sciber_dudeCommented:
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
 
sciber_dudeCommented:
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
 
sciber_dudeCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gladxmlCommented:
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
 
sciber_dudeCommented:
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
 
ActiveMediaCommented:
@

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
 
sciber_dudeCommented:
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
 
gladxmlCommented:
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
 
gladxmlCommented:
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
 
khatrijAuthor Commented:
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
 
khatrijAuthor Commented:
And both of this function stays in the same page.
0
 
gladxmlCommented:
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
 
sciber_dudeCommented:
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
 
gladxmlCommented:
khatrij,

KIndly post the code so that I can help in any way that I can...
0
 
GaryCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now