加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用VB实现一些小函数集

发布时间:2020-12-16 23:34:44 所属栏目:大数据 来源:网络整理
导读:1. 类似于EXCEL中的MAX() Public Function getMax( ParamArray item() As Variant ) As Variant Dim oMax As Variant Dim i As Integer oMax = item(i) For i = 1 To UBound (item) If oMax item(i) Then oMax = item(i) End If Next i getMax = oMax End Fu

1. 类似于EXCEL中的MAX()

Public Function getMax(ParamArray item() As Variant) As Variant

Dim oMax As Variant
Dim i As Integer

oMax = item(i)

For i = 1 To UBound(item)
If oMax < item(i) Then
oMax = item(i)
End If
Next i

getMax = oMax

End Function

2. DCONCAT 域字符串合并函数

DCONCAT 函数

可以使用 DCONCAT 函数连接指定的一组记录(域)中的一组值的字符串。
可以在 Visual Basic、宏、查询表达式或者计算控件中使用 DCONCAT 函数。
(ADO/DAO不支持)。

例如,可以在查询中使用 DCONCAT 函数。
select customerID,DCONCAT('orderID','orders','customerID=' & customerID
from customers

DCONCAT(expr,domain,[criteria],[delimiter])
DCONCAT 函数具有下列参数

参数 说明
expr 表达式,用于标识要计算其记录数的字段。它可以是标识表或查询中
字段的字符串表达式,也可以是对该字段中的数据执行计算的表达式。
在 expr 中,可以包含表中字段的名称、窗体上的控件、常量或函数。
如果 expr 包含一个函数,那么它可能是内置或用户定义的函数,但
不是另一个域聚合函数或 SQL 聚合函数。
domain 字符串表达式,用于标识组成域的一组记录。它可以是表名称或不需
要参数的查询的查询名称。
criteria可选字符串表达式,用于限制对其执行 DCONCAT 函数的目标数据的范
围。例如,criteria 通常等价于 SQL 表达式中的 WHERE 子句,但它
没有单词 WHERE。如果 criteria 被省略,那么 DCONCAT 函数将针对
整个域计算 expr。任何包含在 criteria 中的字段必须也是 domain
中的字段;否则 DCONCAT 函数将返回 错误信息。
delimiter 可选字符串表达式,用于分隔各记录的字符串值。默认值为逗号"," 。



函数实现



'*******************************************************************************
' DCONCAT(expr,[delimiter])
'
' Function:to concate the columns string like group_concat() in Access.
' Parameter:
' expr : string,expression which can recognized by SQL.
' domain : string,the row source,can be another query.
' criteria: the ceritera which will be treated as the where clause.
' delimiter : delimiter between the column value,default is ",".
'
' Return: string,a String of concated columns,
' if err,return the err code + desc.
'
' history:
' 2009-Feb-28 ACMAIN New Creation
'
'*********************************************************************************

Public Function DCONCAT(sExpr As String,sDomain As String,Optional sCriteria As String,Optional sDelimiter As String = ",")
On Error GoTo ErrHandler

Dim rs As New ADODB.Recordset

Dim sSQL As String
Dim sResult As String
sResult = ""

sSQL = "
select " & sExpr & " from (" & sDomain & ")"
If sCriteria <> "" Then
sSQL = sSQL & " where " & sCriteria
End If

rs.Open sSQL,CurrentProject.Connection,adOpenForwardOnly,adLockReadOnly

Do While Not rs.EOF
If sResult <> "" Then
sResult = sResult & sDelimiter
End If
sResult = sResult & rs.Fields(0).Value
rs.MoveNext
Loop

rs.Close
Set rs = Nothing

DCONCAT = sResult

Exit Function

ErrHandler:
If rs.State <> adStateClosed Then
rs.Close
End If
Set rs = Nothing

DCONCAT = Err.Number & " : " & Err.Description


End Function

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读