Solved

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

Posted on 2008-10-06
6
692 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Writing comments on <p></P> or paragraph 2 23
XML and version of MS XML Dom Document versions 5 43
Html split(text) 2 45
JQuery on multiple lines 3 45
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
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/…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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