Web Analytics Made Easy -
StatCounter How do I call a VBscript function? - CodingForum

Announcement

Collapse
No announcement yet.

How do I call a VBscript function?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I call a VBscript function?

    I have this function in VBscript that interogates an access database and I want to call the function so that it returns back th value as the function name. How do I also include it in my VBscript?

    The function:

    '===========================================================================
    ' This function looks up the IP number in the database and returns the
    ' name of the country.
    '===========================================================================
    Function IP2Country(IP)
    Dim objCon
    Dim objRs
    Dim llng
    Dim lstrIP
    Dim llngNum
    Dim lstrSQL


    Set objCon = Server.CreateObject("ADODB.Connection")
    objCon.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("IP2Country.mdb") & ";"
    objCon.Open

    lstrIP = IP
    llngNum = 256 * 256 * 256
    llng = 0
    GetNext lstrIP, llng, llngNum
    GetNext lstrIP, llng, llngNum
    GetNext lstrIP, llng, llngNum
    GetNext lstrIP, llng, llngNum

    lstrSQL = "SELECT IsoCountry.Land FROM IP2Country INNER JOIN IsoCountry ON IP2Country.ISO=IsoCountry.Code WHERE IPFrom<=" & llng & " AND IPTo>=" & llng & ""
    Set objRs = objCon.Execute(lstrSQL)
    If Not objRs.EOF Then
    IP2Country = objRs("Land")
    Else
    IP2Country = "Onbekend"
    End If
    objRs.Close
    Set objRs = Nothing

    objCon.Close
    Set objCon = Nothing

    End Function

  • #2
    You'd call it using a holding variable:

    myIP2Country = IP2Country(myIPValue)


    BUT - a word on your function:
    Code:
    If Not objRs.EOF Then
    IP2Country = objRs("Land")
    Else
    IP2Country = "Onbekend"
    End If
    objRs.Close
    Set objRs = Nothing
    
    objCon.Close
    Set objCon = Nothing
    In the code above, the line where you actually set the value will make the function exit; before it reaches any lines that close the recordset or the connection. This will cause problems

    A more server-friendly way would be:
    Code:
    If Not objRs.EOF Then
    rv= objRs("Land")
    Else
    rv= "Onbekend"
    End If
    objRs.Close
    Set objRs = Nothing
    
    objCon.Close
    Set objCon = Nothing
    
    IP2Country =rv

    Comment


    • #3
      Originally posted by Spudhead
      A more server-friendly way would be:
      Code:
      If Not objRs.EOF Then
      rv= objRs("Land")
      Else
      rv= "Onbekend"
      End If
      objRs.Close
      Set objRs = Nothing
      
      objCon.Close
      Set objCon = Nothing
      
      IP2Country =rv
      And an even better way would be:
      Code:
      If Not objRs.EOF Then
           rv= objRs.Fields("Land").Value
      Else
           rv= "Onbekend"
      End If
      objRs.Close
      Set objRs = Nothing
      
      objCon.Close
      Set objCon = Nothing
      
      IP2Country =rv
      OracleGuy

      Comment


      • #4
        There's absolutely NO difference - the default property of the Field object (i.e. objRs("Land")) is .Value anyway.

        And besides, there was *nothing* wrong with the original code anyway - no exiting of any sort will occur, contrary to what has been claimed (and I defy you to show me otherwise):
        Code:
        If Not objRs.EOF Then
        IP2Country = objRs("Land")
        Else
        IP2Country = "Onbekend"
        End If
        objRs.Close
        Set objRs = Nothing
        
        objCon.Close
        Set objCon = Nothing
        Tot ziens en slaap lekker!
        Marcus Tucker / www / blog
        Web Analyst Programmer / Voted SPF "ASP Guru"

        Comment


        • #5
          True, setting the function's return value doesn't force the function to return:
          Code:
          <%
          function aa
          aa = "1"
          aa = "2"
          aa = "3"
          end function
          
          dim res
          res = aa
          response.write res
          %>
          That prints out 3.

          shmoove

          Comment


          • #6
            Originally posted by shmoove
            True, setting the function's return value doesn't force the function to return
            Precisely - of course not. That's why Spudhead's point was a red herring, and then oracleguy went along with it, which I thought was odd too! Perhaps they were thinking in Javascript?

            Guys, any explanation? Have I missed something?!


            Marcus Tucker / www / blog
            Web Analyst Programmer / Voted SPF "ASP Guru"

            Comment


            • #7
              I was really only commenting on the use of the recordset. Of which, I wasn't sure if there was a difference. I suppose I only started doing it since it is bad practice to do like Request("variable"), not that the recordset has that problem.

              So yeah...
              OracleGuy

              Comment


              • #8
                lol!
                Marcus Tucker / www / blog
                Web Analyst Programmer / Voted SPF "ASP Guru"

                Comment


                • #9
                  Ummmm. Ahhhh..... Oh.

                  Well I guess I was wrong then

                  I only mentioned it as something I'd had pointed out to me before; by the lead developer at my current job, a senior guy who's been programming since the year dot, and someone with whom I shall certainly be raising this issue again. Not only because of the complete roasting I received at the time, but because this "don't set a function's return value until you've closed your recordset/connection" is central to most of the function libraries - originally written by Mr Lead Developer above - that we use. He seemed aghast at the time that I was unfamiliar with this apparently basic rule; that if you set a function's return value, then the function would exit on that line.

                  Another day, another learning experience

                  Comment


                  • #10
                    It's perfectly true (i.e. good programming practice) that you shouldn't exit a function/sub without explicitly killing any local object variables (because the automatic garbage collection is flaky), but exiting doesn't happen in VBScript when you set the function's return value, only at the end of the procedure or when you explicitly call Exit Sub/Exit Function.

                    So don't let him get away with claiming that he "got confused with JavaScript"!!! A man in his position should know better (and should be *damn sure* of the facts if he's giving you a dressing-down)

                    If you'd like to post some of his code, I'd be happy to check it over and see if there are any glaring errors or particularly bad programming techniques in use...!
                    Last edited by [email protected]; Feb 9, 2004, 07:26 AM.
                    Marcus Tucker / www / blog
                    Web Analyst Programmer / Voted SPF "ASP Guru"

                    Comment

                    Working...
                    X