[thelist] help: Object required

Keith Gaughan keith at digital-crew.com
Wed May 25 12:58:58 CDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dena wrote:

> Why do I get "object required" error when I call this subroutine?
> I've looked through microsoft info and googled the problem, and it
> should work.

The problem is that the 'Dim' statements make the connection object
local to that subroutine. Here's the corrected code:

<%
Function OpenDBConnection()
    Dim conn, path, connStr

    ' Build the connection string for the database.
    path = Server.MapPath("/datastore/library.mdb")
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & path & ";" & _
        "Persist Security Info=False"

    ' Connect to it.
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open connStr

    ' Return the connection to the caller.
    Set OpenDBConnection = conn
End Function
%>

And here's the corrected page code:

<%
Option Explicit
Response.Buffer = True

Dim conn, rs, sql

' ...

If Len(iDocId) > 0 And IsNumeric(iDocId) Then
    Set conn = OpenDBConnection()
    sql = "SELECT doc_checkedout, doc_title, doc_callno " &_
          "WHERE  doc_seq = " & iDocId
    Set rs = conn.Execute(sql)
End If
%>

I added IsNumeric() above to ensure that the above code isn't vunerable
to an SQL Injection attack.

K.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFClLzimSWF0pzlQ04RAl6yAKDAXrQYpBfpwxNwfmAWhGcNKG2p9QCdEW52
xO3s+sGXAjtBWXIKTW2TYEA=
=rsru
-----END PGP SIGNATURE-----


More information about the thelist mailing list