Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

如何创建和使用DStart() 和 DEnd() 自定义域函数

时 间:2008-09-08 13:47:18
作 者:chenlugen   ID:2399  城市:东莞
摘 要:如何创建和使用自定义域函数类似于 DFirst() 和 Dlast()的自定义域函数...
正 文:

Option Compare Database
Option Explicit

'本文仅适用于 Microsoft Access 数据库 (.mdb)。

'由于 DFirst() 和 DLast() 函数始终返回第一个和最后一个您输入在基础表中的记录,但不是返回基础表中按表索引或字段排序后的第一个和最后一个记录。

'即DFirst(), DLast() 忽略索引,甚至主键和排序顺序。

'本文介绍如何编写自定义的域功能,从而可以使用一种类似于DFirst()和 DLast()的域函数。

'本文中的 DStart() 和 DEnd() 自定义域示例返回则能够返回已排序的查询中的第一个和最后一个记录。

'==================================================================================================================================================

'创建和使用自定义域函数类似于 DFirst() 和 Dlast(),请按照下列步骤操作:

'1. open sample database Northwind.mdb。

'2. 创建一个新的模块并输入下面的两个函数:

'--------------------------------------
' Use DStart()instead of DFirst() to return
' the first sorted record in a domain.
'--------------------------------------
Function DStart(FieldName As String, DomainName As String, Optional _
  Criteria As Variant)

  Dim MyDB As Database, MySet As Recordset

  ' Error out if there is no fieldname sent.
  If Len(FieldName) = 0 Then
     MsgBox "You Must Specify a Field name", , "DStart"
     Exit Function
  End If

  ' Error out if there is no domain sent.
  If Len(DomainName) = 0 Then
     MsgBox "You Must Specify a Domain name", , "DStart"
     Exit Function
  End If

  Set MyDB = CurrentDb()
  Set MySet = MyDB.OpenRecordset(DomainName, dbOpenDynaset)

  ' Apply a filter to the recordset if a criteria is sent.
  If Not IsMissing(Criteria) Then
     MySet.Filter = Criteria
     Set MySet = MySet.OpenRecordset()
  End If

  ' If there are no records, return the null, else return the value
  ' of the first record.
  If MySet.EOF Then
     DStart = Null
  Else
     MySet.MoveFirst
     DStart = MySet(FieldName)
  End If

  MySet.Close
  MyDB.Close
End Function

'-------------------------------------------
'Use DEnd()instead of DLast() to return
'  the last sorted record in a domain.
'-------------------------------------------

Function DEnd(FieldName As String, DomainName As String, Optional _
  Criteria As Variant)

  Dim MyDB As Database, MySet As Recordset

  ' Error out if there is no fieldname sent.
  If Len(FieldName) = 0 Then
     MsgBox "You Must Specify a Field name", , "DEnd"
     Exit Function
  End If

  ' Error out if there is no domainname sent.
  If Len(DomainName) = 0 Then
     MsgBox "You Must Specify a Domain name", , "DEnd"
     Exit Function
  End If

  Set MyDB = CurrentDb()
  Set MySet = MyDB.OpenRecordset(DomainName, dbOpenDynaset)

  ' Apply a filter to the recordset if a criteria is sent.
  If Not IsMissing(Criteria) Then
     MySet.Filter = Criteria
     Set MySet = MySet.OpenRecordset()
  End If

  ' If there are no records, return the null, else return the value
  ' of the last record.
  If MySet.EOF Then
     DEnd = Null
  Else
     MySet.MoveLast
     DEnd = MySet(FieldName)
  End If
  MySet.Close
  MyDB.Close
End

Access软件网官方交流QQ群 (群号:483923997)       Access源码网店


常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助