如何创建和使用DStart() 和 DEnd() 自定义域函数-54.℡80後2oO
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


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

发表时间:2008/9/8 13:47:18 评论(0) 浏览(6854)  评论 | 加入收藏 | 复制
   
摘 要:如何创建和使用自定义域函数类似于 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群(群号:198465573)
 
 相关文章
【Access自定义函数】字符串中数字相关的几个自定义函数  【网行者  2013/2/6】
【Access自定义函数】不规则提取日期数据的示例  【红尘如烟  2013/2/13】
【Access自定义函数】测算星座,根据日期算出星座,十二星座查询...  【麥田  2013/4/6】
自定义时间分段函数示例  【杜超-2号  2013/6/1】
右边选项卡\选项页自定义  【杜超-2号  2013/7/27】
常见问答
技术分类
相关资源
文章搜索
关于作者

54.℡80後2oO

文章分类

文章存档

友情链接