ACC2000: AutoLookup Does Not Work on a Data Access Page (202144)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q202144
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

SYMPTOMS

If you base a data access page on an AutoLookup query, the recordset will not be updateable.

CAUSE

The information on the one-side of the AutoLookup relationship is not updated until the record is committed.

RESOLUTION

To use AutoLookup on a data access page in such a way that the page remains updateable, you must use script. For an example of how to do so, follow these steps:
  1. Open the sample database Northwind.mdb.
  2. Create a new data access page based on the Orders table. Be sure to include the OrderID and CustomerID fields.
  3. Add two unbound text boxes to the page, and then set the following properties:
       First Label
           InnerText: Address
    
       First Textbox
           ID: Address
    
       Second Label
           InnerText: CityStateZip
    
       Second Textbox
           ID: CityStateZip
    					
  4. On the Tools menu, point to Macro, and then click Microsoft Script Editor.
  5. Insert the following script directly after the MSODSC control in the HTML Editor, including the Script tag.
    <SCRIPT language=VBScript>
     <!--
      Sub AutoLookup
       Dim x
    
       x = MSODSC.DefaultRecordset.Fields.Item("CustomerID") 
    
       'Check to make sure the CustomerID field is not null.
       If IsNull(x) = False Then
        'Select all Customer records with matching
        'CustomerID values and create a  recordset
        Dim rs
        Dim conn
        Dim sql
    
        Set conn = MSODSC.Connection
        Set rs = CreateObject("ADODB.recordset")
    
        sql = "SELECT Customers.CustomerID, Customers.CompanyName, "
        sql = sql & "Customers.Address, Customers.City, Customers.Region, "
        sql = sql & "Customers.PostalCode "
        sql = sql & "FROM Customers "
        sql = sql & "WHERE (((Customers.CustomerID)='" & x & "'));"
    
        'Open the recordset.
        rs.Open sql,conn,3,3
    
        'Set the Address1 and Address2 textboxes to the
        'appropriate values for the current record.
        Document.All.Item("Address").Value = rs("Address")
        Document.All.Item("CityStateZip").Value = rs("City") & _
            ", " & rs("Region") & " " & rs("PostalCode")
    
        'Close the current recordset.
        rs.Close
       Else
        'Set the Address1 and Address2 textboxes to empty strings.
        Document.All.Item("Address").Value = ""
        Document.All.Item("CityStateZip").Value = ""
       End If
      End Sub
     -->
    </SCRIPT>
    					
  6. On the View menu, point to Other Windows, and then click Script Outline.
  7. In the Script Outline pane, expand the CustomerID node, and then double-click onafterupdate.
  8. Add the following line of code to this event:
    AutoLookup
    					
  9. At the bottom of the Microsoft Script Editor, click the Quick View tab, and then click the New Record button on the navigation bar.
  10. Type a customer ID into the CustomerID text box (for example, type BERGS), and then press TAB. Note that the Address and CityStateZip text boxes are automatically populated with the matching customer information.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Open the sample database Northwind.mdb.
  2. Create a new query that includes both the Customers table and the Orders table.
  3. Include all of the fields from the Orders table and the following fields from the Customers table in the query:
    • CompanyName
    • Address
    • City
    • Region
    • Postal Code

  4. On the View menu, click Datasheet View.
  5. Add a new record, selecting a customer name from the Customer column. Note that the fields CompanyName, Address, City, Region, and Postal Code are automatically filled in.
  6. On the File menu, click Save, save the query as MyLookup, and then close the query.
  7. With the name of this new query (MyLookup) still selected in the Database window, click AutoForm on the Insert menu.
  8. Click the new record button at the bottom of the form, and then select an existing Customer from the combo box. Note that the behavior on the form is identical to the behavior in the query. Close the form without saving it.
  9. Create a new data access page based on this same query. Note that the data access page is not updateable. You cannot add a new record.

Modification Type:MajorLast Reviewed:6/28/2004
Keywords:kbDAP kbprb KB202144