Solved

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

Posted on 2008-10-06
6
691 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
Independent Software Vendors: 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!

 

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FileUp - Classic ASP 5 24
Pass asp session variable to a js function in an asp script 6 32
Connection to multiple databases 13 35
Ajax on ASP 2 36
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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