Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I call a VB Function call from an include file?

Posted on 2008-10-06
6
Medium Priority
?
697 Views
Last Modified: 2012-05-05
Hi guys,

I am coding in ASP using VBSript but I have this problem.

I am  trying to call a function that I have written in another file but I seems to have problem calling it at all.

Please help.
<!--#include file="scripts/utility.asp"-->
<head>
</head>
 
<body>
<% 
openDB()
%>
</body>
 
I am trying to call this function openDB() from this file.
 
<%
Function openDB()
    sConnection = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=mydb; UID=root;PASSWORD=password; OPTION=3"
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open(sConnection)
End Function
%>

Open in new window

0
Comment
Question by:ricteo
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:jose_juan
ID: 22650311
What's problem?

Is correct but... try

<%
   openDB
%>

[ not openDB() ]

Good luck!
0
 

Author Comment

by:ricteo
ID: 22651391
Hi

I cannot seems to call the function properly.
What if I put a msgbox inside the function?
Possible?

Thanks
0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22651688
No ricteo,

a msgbox in a asp page not is possible, I recommend to you read around client-server applications using http petitions...

1. first the client send a message (tcp/ip) requesting a resource (the url).
2. the server run a application for get the resource (in this case the asp runtime).
3. the server return to client (tcp/ip) the resource content (the html generated page).
4. the browser show the html to user.

how is possible show a dialog box? only the resultant html is available. If you need a dialog box, you must generate it on point 2 (using html) and the browser client will show it on point 4.

do you understand?

you can try it

<html>
<body>
<% 
openDB()
%>
</body>
</html> 
 
( in the other file... )
<%
Function openDB()
On Error Goto GetError
    sConnection = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=mydb; UID=root;PASSWORD=password; OPTION=3"
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open(sConnection)
    Return
GetError:
    Response.Write "Error connecting to database! <br>" & err.Description
End Function
%>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ricteo
ID: 22656738
Hi jose_juan,

Function openDB()
On Error Goto GetError
    sConnection = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=mydb; UID=root;PASSWORD=password; OPTION=3"
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open(sConnection)
    Return
GetError:
    Response.Write "Error connecting to database! <br>" & err.Description
End Function

In this code, what does the return returns? IS it the objConn or objConn.open(sConnection)?

Many Thanks:)
0
 
LVL 5

Accepted Solution

by:
jose_juan earned 300 total points
ID: 22656966
Hi ricteo,

I suggest to you understand the basis of VB language (reading manuals).

In VBScript for return a value in a function you must assign the value to the name of function (not return are available in vbscript, I put it by error), if you like return the connection object you must do (see code).

For programming learning not is bad your code (create a function for return connection object) but is better abstract all access to databases like (is a example)

function GetPerson( person_code )
       ... access to database...
       Set GetPerson = new Person( name, age, height, weight, ... )
end function
function SetPerson( person_code, name, age, ... )
       ... access to database ...
end function

in your program you never have a reference to database structure, only use the abstract functions

   ' my program
   Dim person
   Set person = GetPerson( '234243' )
   Response.Write "Name: " & person.name
   ...
   SetPerson( '23243', 'john', 23, 1.82, 87, ... )
   ...

Good luck! (and read manuals for learn a programming language)


Function openDB()
On Error Goto GetError
    sConnection = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; DATABASE=mydb; UID=root;PASSWORD=password; OPTION=3"
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open(sConnection)
    Set openDB = objConn ' Ok, We have object.
    Exit Sub
GetError:
    Response.Write "Error connecting to database! <br>" & err.Description
    Set openDB = Nothing ' Error!
End Function

Open in new window

0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22656970
ohggg! Sorry, I had other error.

When you see "Exit Sub" is "Exit Function".

Sorry...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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/…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

885 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