Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post

    Error: Object required

    Hello

    I am getting the following error in a Web page that a) displays MS Access data and b) aims to add other data using a 'submit' button. The idea is that empty fields (that correspond to the fields in the MS Access database) are completed online to add to the table/database.

    The error states:

    Microsoft VBScript runtime error '800a01a8'

    Object required

    /display_records1.asp, line 136
    According to this site:

    http://classicasp.aspfaq.com/general...-required.html the error is generated because:

    because you've tried to close or set to nothing an object that hasn't been defined
    Line 136 is this:

    Recordset.Close
    and 'Recordset' is 'dimmed' earlier on in the code.

    Thanks for any advice.

    Steve

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    DIM'ing a variable in VBScript does *NOT* make it refer to an object.

    The variable will only be a valid object reference after a SET statement.

    e.g.,
    Code:
    Set Recordset = Server.CreateObject("ADODB.Recordset")
    or perhaps
    Set Recordset = someConnectionOrCommandObject.Execute( someSql )
    No code. No help. Show the code from where the DIM occurs to where the CLOSE occurs.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Oh, and that site is dead wrong.
    Code:
    because you've tried to close or set to nothing an object that hasn't been defined
    That error will *NEVER* be generated by doing
    Code:
    Set anyVariable = Nothing
    Any variable can be set to Nothing at any time.

    I would stop trusting that site.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Hello Old Pedant

    OK, I'll disregard that link.

    This is the code (less a few irrelevant bits):

    Code:
    <%@ Language="VBScript" %>
     <% Option Explicit %>
    
    <!DOCTYPE html>
    
     <% 
    Dim Connection, Recordset
    Dim sSQL, sConnString
    
    'declare SQL statement that will query the database
    
    
    sSQL="SELECT TOP 10 ID, firstName, lastName, wolfID, telNo, stAddress, city, postCode, items, received, dispatched FROM myTable"
    
    'Use "select * from ..." if you want to display all the records
     
    
    'define the connection string, specify database 
    'driver and the location of database
     sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ 
    "Data Source=" & Server.MapPath("myDatabase.mdb") 
    
    
    'create an ADO connection and recordset
     Set connection = Server.CreateObject("ADODB.Connection")
     Set recordset = Server.CreateObject("ADODB.Recordset")
    
    'Open the connection to the database
     connection.Open sConnString
    
    'Open the recordset object, execute the SQL statement
     recordset.Open sSQL,connection
    
    
    'HTML table to house and format our results
    
    
     response.write "<table width='100%' border='1'>"
    %>
    
    some table formatting here
    
    <%
    'Now lets determine whether there are any records
     If Recordset.EOF Then
     Response.Write "<tr><td>No records returned.</td></tr>"
     Else
    'If there are records then loop through the fields and format in table rows & cells
    
    Do While Not recordset.EOF
    
    Response.Write statements
    
     Recordset.MoveNext
     Loop
    'Close the HTML table
     response.write "</table>"
     End If
    
     Recordset.Close
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing
     %>
    
    <% 
    
    Dim my_firstName, my_lastName, my_wolfID, my_telNo, my_stAddress, my_city, my_postCode, my_items, my_received, my_dispatched
    
    my_firstName = Request.Form("firstName")
    my_lastName = Request.Form("lastName")
    my_wolfID = Request.Form("wolfID")
    my_telNo = Request.Form("telNo")
    my_stAddress = Request.Form("stAddress")
    my_city = Request.Form("city")
    my_postCode = Request.Form("postCode")
    my_items = Request.Form("items")
    my_received = Request.Form("received")
    my_dispatched = Request.Form("dispatched")
    
    
    '''' Me: read up on SQL injection
    
    sSQL="INSERT INTO myTable (firstName, lastName, wolfID, telNo, stAddress, city, postCode, items, received, dispatched) VALUES ('" & my_firstName & "', '" & my_lastName & "', '" & my_wolfID & "', '" & my_telNo & "', '" & my_stAddress & "', '" & my_city & "', '" & my_postCode & "', '" & my_items & "', '" & my_received & "', '" & my_dispatched & "')" 
    
    'close the connection and recordset objects and free up resources
     Recordset.Close
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing
    %>
    
    <form action="display_records1.asp" method="post">
      <input type="text" name="First Name" />
      <input type="text" name="Last Name" />
     <input type="text" name="Wolf ID" />
     <input type="text" name="Tel No" />
     <input type="text" name="Address" />
     <input type="text" name="City" />
     <input type="text" name="Postcode" />
     <input type="text" name="Items" />
     <input type="text" name="Received" />
     <input type="text" name="Dispatched" />
      <input type="submit" />
    </form>
    It is online here:

    http://www.proofreading4students.com...y_records1.asp

    Thank you again.

    Steve

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Well that's easy to see:
    Code:
    ...
     Recordset.Close
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing
     %>
    
    <% 
    
    Dim my_firstName, my_lastName, my_wolfID, my_telNo, my_stAddress, my_city, my_postCode, my_items, my_received, my_dispatched
    
    my_firstName = Request.Form("firstName")
    my_lastName = Request.Form("lastName")
    my_wolfID = Request.Form("wolfID")
    my_telNo = Request.Form("telNo")
    my_stAddress = Request.Form("stAddress")
    my_city = Request.Form("city")
    my_postCode = Request.Form("postCode")
    my_items = Request.Form("items")
    my_received = Request.Form("received")
    my_dispatched = Request.Form("dispatched")
    
    
    '''' Me: read up on SQL injection
    
    sSQL="INSERT INTO myTable (firstName, lastName, wolfID, telNo, stAddress, city, postCode, items, received, dispatched) VALUES ('" & my_firstName & "', '" & my_lastName & "', '" & my_wolfID & "', '" & my_telNo & "', '" & my_stAddress & "', '" & my_city & "', '" & my_postCode & "', '" & my_items & "', '" & my_received & "', '" & my_dispatched & "')" 
    
    'close the connection and recordset objects and free up resources
     Recordset.Close
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing
    
    ...
    You already set the Recordset to NOTHING via the code in red.

    So when you get to the line in magenta, there is (literally) nothing to close!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Your HTML is both illegal and hard to read.
    Code:
    <td>1</td><td>Peter</td><td>Jones</td><td>123456</td><td>751993298</td><td>25 Craig Gdns</td><td>Glasgow</td><td>G32 8UY</td><td>2 x Hi8 Sony tapes</td><td>4/9/2013</td><td>4/9/2013</td></tr><td>2</td><td>Harry</td><td>Stevens</td>
    You are missing the <tr> at the beginning of each row and the </tr> at the end of each.

    If you are using Response.Write to create that output do something like:
    Code:
    Response.Write "<tr><td>" & id & "</td><td>" & firstname ....& "</td></tr>" & vbNewLine
    I'm not at all clear why your ASP code follows the table output with all the Request.Form stuff.

    You don't *HAVE* any <form> there to get the Request.Form *from*.

    And even if you did (e.g., if the last line of your <table> consisted of a bunch of <input> fields for entering another person), the INSERT into the table should come *BEFORE* you dump out what is in the table. Else the newly inserted record won't appear.

    Finally: There's really only one usable SQL Injection method with ASP and ADO:
    Code:
    ' for string/text values:
    my_firstName = Replace( Request.Form("firstName"), "'", "''" )
    ' for numeric fields:
    my_wolfid = CDBL( Request.Form("wolfid") )
    But then your SQL INSERT is bogus:
    Code:
    ... & my_lastName & "', '" & my_wolfID & "', '" & ...
    Numeric values should *NOT* have apostrophes around them!

    If the field WOLFID in your DB is not a numeric field, then your DB design is, apparently, wrong.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Hello Old Pedant

    Thanks!

    Isn't it the case, that there is also replication here:

    Code:
    'Close the HTML table
     response.write "</table>"
     End If
    
    'close the connection and recordset objects and free up resources
     Recordset.Close
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing %>
    with this (towards the end of the code):

    Code:
    sSQL="INSERT INTO myTable etc
    
    'close the connection and recordset objects and free up resources
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing%>
    In fact, doesn't it seem that I am trying to add records to a MDB (via the form) when the connection to that MDB is broken??

    Steve

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Yes, of course.

    But I don't understand why you are trying to add records into the database table you just displayed.

    Again, shouldn't you do the INSERT *BEFORE* doing the SELECT so you can pick up the just-added record?

    Also, you do not use and ORDER BY clause in your SELECT, so you can't really predict *which* 10 records you will get via the TOP 10. You should *ALWAYS* have an ORDER BY in *any* SELECT that gets more than one record.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Oh, and where the heck is the <form> that you are getting the info from that you use to do the Request.Form()s and the INSERT?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Hello Old Pedant

    I missed it off deliberately in my original post to make things easier to read:

    Code:
    sSQL="INSERT INTO myTable etc
    
    'close the connection and recordset objects and free up resources
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing
    %>
    
    <form action="display_records1.asp" method="post">
      <input type="text" name="First Name" />
      <input type="text" name="Last Name" />
     <input type="text" name="Wolf ID" />
     <input type="text" name="Tel No" />
     <input type="text" name="Address" />
     <input type="text" name="City" />
     <input type="text" name="Postcode" />
     <input type="text" name="Items" />
     <input type="text" name="Received" />
     <input type="text" name="Dispatched" />
      <input type="submit" />
    </form>
    
     </body>
     </html>

  • #11
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Oh, right.

    I will insert the ORDER BY, probably date order.

    Thanks for pointing that out.

    Steve

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    But I still say, then, that you should be doing the INSERT *before* the SELECT, so that the SELECT can show the most recently added (brand new) record.

    By the by, I should point out that you never Execute that SQL INSERt statement.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Hello Old Pedant

    I think I have followed your suggestions to the letter, but for some reason I am unable to add records to my table.

    This is the code as I have it at the moment:

    Code:
    <%@ Language="VBScript" %>
     <% Option Explicit %>
    
    <% 
     Dim Connection, Recordset, sSQL, sConnString, my_firstName, my_lastName, my_wolfID, my_telNo, my_stAddress, my_city, my_postCode, my_items, my_received, my_dispatched
     
    my_firstName = Replace( Request.Form("firstName"), "'", "''" )
    my_lastName = Replace( Request.Form("lastName"), "'", "''" )
    my_wolfID = CDBL( Request.Form("wolfID") )
    my_telNo = CDBL( Request.Form("telNo") )
    my_stAddress = Replace( Request.Form("stAddress"), "'", "''" )
    my_city = Replace( Request.Form("city"), "'", "''" )
    my_postCode = Replace( Request.Form("postCode"), "'", "''" )
    my_items = Replace( Request.Form("items"), "'", "''" )
    my_received = Replace( Request.Form("received"), "'", "''" )
    my_dispatched = Replace( Request.Form("dispatched"), "'", "''" )
    
    
    sSQL="INSERT INTO tblWolf (firstName, lastName, wolfID, telNo, stAddress, city, postCode, items, received, dispatched) VALUES ('" & my_firstName & "', '" & my_lastName & "'," & my_wolfID & ",'" & my_telNo & "', '" & my_stAddress & "', '" & my_city & "', '" & my_postCode & "', '" & my_items & "', '" & my_received & "', '" & my_dispatched & "')"
    
    
    'declare SQL statement that will query the database - use "select * from ..." to display all the records
    
    
    sSQL="SELECT TOP 30 ID, firstName, lastName, wolfID, telNo, stAddress, city, postCode, items, received, dispatched FROM tblWolf"
    
    'define the connection string, specify database driver and  database location
    
     sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ 
    "Data Source=" & Server.MapPath("myDatabase.mdb") 
    
    
    'create an ADO connection and recordset
     Set connection = Server.CreateObject("ADODB.Connection")
     Set recordset = Server.CreateObject("ADODB.Recordset")
    
    'Open the connection to the database
     connection.Open sConnString
    
    'Open the recordset object, execute the SQL statement
     recordset.Open sSQL,connection
    
    
    'HTML table to house and format our results
    
    
     response.write "<table width='100%' border='1'>"
    %>
    
    <tr class="headerRow">
                    <td>ID</td>
    		<td >First Name</td>
    		<td >Last Name</td>
    		<td >Wolf ID</td>
    		<td >Tel No</td>
                    <td >Address</td>
    		<td >City</td>
    		<td >Postcode</td>
    		<td >Items</td>
    		<td >Received</td>
    		<td >Dispatched</td></font>
        </tr>
    
    <%
    'Determine whether there are any records
     If Recordset.EOF Then
     Response.Write "<tr><td>No records returned.</td></tr>"
     Else
    'If there are records then loop through the fields and format in table rows & cells
    
    
    Do While Not recordset.EOF
    
    
    Response.Write "<td>" & recordset("ID") & "</td>"
    Response.Write "<td>" & recordset("firstName") & "</td>"
    Response.Write "<td>" & recordset("lastName") & "</td>"
    Response.Write "<td>" & recordset("wolfID") & "</td>"
    Response.Write "<td>" & recordset("telNo") & "</td>"
    Response.Write "<td>" & recordset("stAddress") & "</td>"
    Response.Write "<td>" & recordset("city") & "</td>"
    Response.Write "<td>" & recordset("postCode") & "</td>"
    Response.Write "<td>" & recordset("items") & "</td>"
    Response.Write "<td>" & recordset("received") & "</td>"
    Response.Write "<td>" & recordset("dispatched") & "</td></tr>" 
    'move on to the next record
     Recordset.MoveNext
     Loop
    'Close the HTML table
     response.write "</table>"
     End If
    
    'close the connection and recordset objects and free up resources
     Recordset.Close
     Connection.Close
     Set Recordset = Nothing
     Set Connection = Nothing
     %>
    
    
    <form action="display_records2.asp" method="post">
    <fieldset>
    <legend>Data input form</legend>
    
     <p><label for="firstName">Name</label> <input type="text" id="firstName" /></p>
    
    <p><label for="lastName">Surname</label> <input type="text" id="lastName" /></p>
    
    <p><label for="wolfID">Wolf ID</label> <input type="text" id="wolfID" /></p>
    
    <p><label for="telNo">Tel No</label> <input type="text" id="telNo" /></p>
    
    <p><label for="stAddress">Address</label> <input type="text" id="stAddress" /></p>
    
    <p><label for="city">City</label> <input type="text" id="city" /></p>
    
    <p><label for="postCode">Postcode</label> <input type="text" id="postCode" /></p>
    
    <p><label for="items">Items</label> <input type="text" id="items" /></p>
    
    <p><label for="received">Received</label> <input type="text" id="received" /></p>
    
    <p><label for="dispatched">Sent</label> <input type="text" id="dispatched" /><br /></p>
    
    
     <p class="submit"><input type="submit" value="Add data" /></p>
    </fieldset>
     </form>
    Is there a debug script that would highlight any errors I may have?

    Thanks

    Steve

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Read what I wrote in post #12: You never ever *EXECUTE* the INSERT statement, so it just gets thrown away!

    Code:
    ...
    sSQL="INSERT INTO tblWolf (firstName, lastName, wolfID, telNo, stAddress, city, postCode, items, received, dispatched) VALUES ('" & my_firstName & "', '" & my_lastName & "'," & my_wolfID & ",'" & my_telNo & "', '" & my_stAddress & "', '" & my_city & "', '" & my_postCode & "', '" & my_items & "', '" & my_received & "', '" & my_dispatched & "')"
    
    connection.Execute sSQL ' *** YOU ARE MISSING THIS! ***
    
    'declare SQL statement that will query the database - use "select * from ..." to display all the records
    ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    ASP == Active Server Pages.
    Created by Microsoft about 1996 or so.
    You can use either JScript (Microsoft's version of JavaScript) or VBScript (a Microsoft-only language that is a variation on the BASIC language) to code in.

    JSP = Java Server Pages
    Created by Sun Microsystems (now part of Oracle Corporation) about 1997 or so.
    Originally JSP looked VERY much like ASP except that it used Java instead of JavaScript as the coding language.
    Today, JSP has many different FRAMEWORKS available and so, even though it is still coded using Java, it doesn't "look" much like the original JSP or ASP.

    ASP is *obsolete* technology. Microsoft is no longer actively developing it, so there won't ever be any major changes or improvements to it.

    JSP is still a very popular technology, especially in larger companies. It is better suited to large projects, with large teams of programmers, than most any other technology except possibly ASP.NET.

    And that brings up ASP.NET: This is also a Microsoft-only technology, but it is much more modern that the original ASP. It also has rich frameworks available, much like JSP. You can code ASP.NET in *many* languages, but the two most popular are C# and VB.NET, both Microsoft-proprietary languages. C# is a really nice language, in my opinion. It has many of the strengths of Java and C++ and relatively few of their weaknesses.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •