Solved

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

Posted on 2008-10-06
6
693 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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