如何创建和使用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源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access小作品】简单的待...(04.30)
- 【Access窗体导出PDF】...(04.08)
- 【Access窗体导出PDF】...(04.07)
- Access两种方式实现即时更...(03.01)
- Access隐藏与显示lacc...(01.12)
- 【Access高效办公】将每个...(12.23)
- Access21点游戏源代码(12.13)
- 【Access窗体导出Exce...(11.15)
- 【Access开发】Acces...(11.14)
学习心得
最新文章
- 【Access Call用法学习】...(05.06)
- access软件网站长张志简介 (05.05)
- 精美简单实用的系统面板(05.03)
- 【Access小作品】简单的待办任...(04.30)
- 从另一个ACCESS数据库批量导入...(04.29)
- Access日期格式的数据导出Ex...(04.28)
- ACCESS精华集锦资料.CHM(04.25)
- Access VBA语句If Me...(04.24)
- 【Access修改记录示例】编辑选...(04.22)
- 【Access表名称命名建议】将A...(04.20)