Microsoft KB Archive/210340

From BetaArchive Wiki

Article ID: 210340

Article Last Modified on 6/23/2005



APPLIES TO

  • Microsoft Access 2000 Standard Edition



This article was previously published under Q210340

Advanced: Requires expert coding, interoperability, and multiuser skills.

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


SUMMARY

This article shows you how to create and use a procedure to display the current line or row number in a subform.

NOTE: This article explains a technique demonstrated in the sample file, FrmSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:

233324 Microsoft Access 2000 Sample Reports Available in Download Center


Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

MORE INFORMATION

The following examples demonstrate how to create and use the sample function, GetLineNumber().

NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

How to Create the GetLineNumber() Function

  1. Open the sample database Northwind.mdb.
  2. Create a module and type the following line in the Declarations section:

    Option Explicit
                        
  3. Type the following procedure:

    Function GetLineNumber (F As Form, KeyName As String, KeyValue)
    
       Dim RS As DAO.Recordset
       Dim CountLines
    
       On Error GoTo Err_GetLineNumber
    
       Set RS = F.RecordsetClone
    
       ' Find the current record.
       Select Case RS.Fields(KeyName).Type
          ' Find using numeric data type key value.
          Case dbInteger, dbLong, dbCurrency, dbSingle, dbDouble, dbByte
             RS.FindFirst "[" & KeyName & "] = " & KeyValue
             ' Find using date data type key value.
          Case dbDate
             RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
             ' Find using text data type key value.
          Case dbText
             RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
          Case Else
             MsgBox "ERROR: Invalid key field data type!"
             Exit Function
       End Select
    
       ' Loop backward, counting the lines.
       Do Until RS.BOF
          CountLines = CountLines + 1
          RS.MovePrevious
       Loop
    
    Bye_GetLineNumber:
       ' Return the result.
       GetLineNumber = CountLines
    
       Exit Function
    
    Err_GetLineNumber:
       CountLines = 0
       Resume Bye_GetLineNumber
    
    End Function
                        

The GetLineNumber() function requires the following three parameters:

  • The form object on which to place line numbers.
  • The name of the unique key field in the subform's underlying table. If the record source does not have a single unique key field, add a field with an AutoNumber data type to the underlying table for this purpose.
  • The current key field value.

You can use the following sample expression as the ControlSource property setting of a text box on a subform, if the subform's underlying table has a field called ID as its unique key field:

=GetLineNumber(Form,"ID",[ID])
                

How to Use the GetLineNumber() Function

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Open the Order Details table in Design view, add the following field to the table, and then save the table:

       Field Name: ID
       Data Type:  AutoNumber
                            

    This field will serve as the required single unique field for the table.

  2. Open the Order Details Extended query in Design view, add the ID field from the Order Details table to the query grid, and then save the query.
  3. Open the Orders Subform form in Design view and add the following text box to the form:

       Name: LineNum
       ControlSource: =GetLineNumber([Form], "ID", [ID])
                        
  4. On the View menu, click Tab Order. Drag the LineNum field from the bottom of the Custom Order list to the top, and then click OK.
  5. Save and close the Orders Subform.
  6. Open the Orders form in Form view and move to a record with multiple order line items. Note that the LineNum text box displays the record number for each product in the order.


Keywords: kbhowto kbusage KB210340