Solved

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

Posted on 2008-10-06
6
683 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now