Conc Function在MS Access中不起作用 [英] Conc Function is not working in MS Access

查看:85
本文介绍了Conc Function在MS Access中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT Tb_Sch_TIme_Table.Sch_Date,Tb_Sch_TIme_Table.Session,Tb_Sch_TIme_Table.Course,Conc(Faculty_Code,Session[Session],Tb_Sch_TIme_Table,Sch_Date,[Sch_Date])AS Faculty_Code

FROM Tb_Sch_TIme_Table

GROUP BY Tb_Sch_TIme_Table.Course,Tb_Sch_TIme_Table.Sch_Date,Tb_Sch_TIme_Table.Session;



当我运行上述查询时;


$ MS $ b在MS Access中输出如下;



Sch_Date会话课程Faculty_Code

1 / 12/2013 1 AFF

1/13/2013 1 TFC



输出faculty_Code没有从上面的查询显示什么是错误帮助我。



在上面的查询中我使用了浓缩。对于该浓缩功能代码如下;



选项比较数据库

选项明确



公共函数Conc(Fieldx,Identity,Value,Source,Identity1,Value1)As Variant

Dim cnn As ADODB.Connection

Dim rs As ADODB.Recordset

Dim SQL As String

Dim vFld As Variant



设置cnn = CurrentProject.Connection

设置rs =新ADODB.Recordset

vFld = Null



SQL =SELECT [&Fieldx&]为Fld &_

FROM [&Source&]&_

WHERE [&Identity&] =&Value&_

和[&Identity1&] =&Value1

''开放记录集。

rs.Open SQL,cnn,adOpenForwardOnly,adLockReadOnly



''连接字段。

请勿做rs.EOF

如果不是IsNull(rs!Fld)然后

vFld = vFld&,& rs!Fld

结束如果

rs.MoveNext

循环

''删除前导逗号和空格。

vFld = Mid(vFld,3)



设置cnn = Nothing

设置rs = Nothing



''返回连接字符串。

Conc = vFld

结束功能





请更正我的查询。

解决方案

1)你为什么要使用这个功能?使用子查询 [ ^ ]而不是。

2)您正在使用此函数作为聚合函数(不是 group by 子句的一部分) ,你确定这是你想要的吗?



[更新:发现 [ ^ ]一个很棒的聚合连接函数]



函数DConcat(ConcatColumns As String,Tbl As String,Optional Criteria As String = ,_ 
可选Delimiter1 As String =,,可选Delimiter2 As String =,,_
可选区分为布尔= T rue,可选排序为String =,_
可选限制为长= 0)

''Patrick G. Matthews的功能,基本上修饰了许多
中看到的方法''多年来的化身

''需要引用Microsoft DAO库

''此函数用作连接(和分隔)$的域聚合 b $ b''各种值而不是更常见的Count,Sum,Min,Max等。例如:
''
''选择Field1,DConcat(Field2,SomeTable, [Field1] =''& Field1& '')AS列表
''FROM SomeTable
''GROUP BY Field1
''
''将返回Field1的不同值,以及连续的列表与每个Field1值关联的所有
''不同的Field2值。

''ConcatColumns是一个以逗号分隔的列连接列(通常只需
''一列,但该函数可容纳多个列)。如果字段名称不符合命名DB对象的惯用规则,则将字段名称放在方括号
''括号中
''Tbl是从中提取数据的表/查询。将表名放在方括号中
''如果它们不符合命名DB对象的惯用规则
''标准(可选)是要在分组中应用的标准。一定要使用And
''或Or根据需要构建正确的逻辑,并用单引号包装文本值
''和#
中的日期''Delimiter1(可选)是连接中使用的分隔符(默认为,)。
''Delimiter1应用于代码查询'的结果集中的每一行
''Delimiter2(可选)是用于连接结果中每一列的分隔符
''set if ConcatColumns指定多个列(默认为,)
''Distinct(可选)确定是否连接不同的值(True,
''默认值),或者是否所有值都连接在一起(和因此可能会重复)
''Sort(可选)指示是否对连接的字符串进行排序,如果是,则用于排序的
''列。正如您在ORDER BY子句中所做的那样,使用Asc或Desc来
''指示列是按升序还是按降序排序。如果Asc / Desc是
''省略,则查询引擎假定Asc为
''Limit(可选)限制了连接字符串中放置了多少项。
''Limit参数在SELECT子句中用作TOP N限定符

Dim rs As DAO.Recordset
Dim SQL As String
Dim ThisItem As String
Dim FieldCounter As Long

On Error GoTo ErrHandler

''Initialize to Null

DConcat = Null

''构建查询以获取连接所需的信息

SQL =SELECT& IIf(Distinct,DISTINCT,)& _
IIf(限制> 0,TOP& Limit&,)& _
ConcatColumns& & _
FROM& Tbl& & _
IIf(Criteria<>,WHERE& Criteria&,)& _
IIf(Sort<>,ORDER BY& Sort,)

''打开记录集并循环遍历:
'' 1)连接记录集的每一行中的每一列
''2)在函数的返回值中连接生成的连接行

设置rs = DBEngine(0)(0) .OpenRecordset(SQL,dbOpenForwardOnly)
使用rs
Do Until .EOF

''此行的初始化变量

ThisItem =

''此行上的连接列

For FieldCounter = 0到rs.Fields.Count - 1
ThisItem = ThisItem& Delimiter2& Nz(rs.Fields(FieldCounter).Value,)
下一个

''修剪前导分隔符

ThisItem = Mid(ThisItem,Len(Delimiter2) + 1)

''将行结果连接到函数返回值

DConcat = Nz(DConcat,)& Delimiter1& ThisItem
.MoveNext
Loop
.Close
End with

''Trim leading delimiter

If Not IsNull(DConcat )然后DConcat = Mid(DConcat,Len(Delimiter1)+ 1)

GoTo Cleanup

ErrHandler:

''错误最有可能是Criteria中的数据库对象名称无效或语法错误

DConcat = CVErr(Err.Number)

清理:
设置rs = Nothing

结束函数





注释中的示例查询是针对字符串类型外键的,如果您有数字则删除多余的引号类型。


SELECT Tb_Sch_TIme_Table.Sch_Date, Tb_Sch_TIme_Table.Session, Tb_Sch_TIme_Table.Course, Conc("Faculty_Code","Session"[Session],"Tb_Sch_TIme_Table","Sch_Date",[Sch_Date]) AS Faculty_Code
FROM Tb_Sch_TIme_Table
GROUP BY Tb_Sch_TIme_Table.Course, Tb_Sch_TIme_Table.Sch_Date, Tb_Sch_TIme_Table.Session;

when i run the above query;

output as follows in MS Access;

Sch_Date Session Course Faculty_Code
1/12/2013 1 AFF
1/13/2013 1 TFC

in that output faculty_Code is not showing from the abovequery what is the mistake help me.

in the above query i use conc. for that conc function code as follows;

Option Compare Database
Option Explicit

Public Function Conc(Fieldx, Identity, Value, Source, Identity1, Value1) As Variant
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim SQL As String
Dim vFld As Variant

Set cnn = CurrentProject.Connection
Set rs = New ADODB.Recordset
vFld = Null

SQL = "SELECT [" & Fieldx & "] as Fld" & _
" FROM [" & Source & "]" & _
" WHERE [" & Identity & "]=" & Value & _
" and [" & Identity1 & "]=" & Value1
'' open recordset.
rs.Open SQL, cnn, adOpenForwardOnly, adLockReadOnly

'' concatenate the field.
Do While Not rs.EOF
If Not IsNull(rs!Fld) Then
vFld = vFld & ", " & rs!Fld
End If
rs.MoveNext
Loop
'' remove leading comma and space.
vFld = Mid(vFld, 3)

Set cnn = Nothing
Set rs = Nothing

'' return concatenated string.
Conc = vFld
End Function


please correct my query.

解决方案

1) Why are you using this function at all? Use subquery[^] instead.
2) You are using this function as aggregate function (not part of the group by clause), are you sure this is what you want?

[Update: found[^] a great aggregate concat function]

Function DConcat(ConcatColumns As String, Tbl As String, Optional Criteria As String = "", _
    Optional Delimiter1 As String = ", ", Optional Delimiter2 As String = ", ", _
    Optional Distinct As Boolean = True, Optional Sort As String = "", _
    Optional Limit As Long = 0)
    
    '' Function by Patrick G. Matthews, basically embellishing an approach seen in many
    '' incarnations over the years
    
    '' Requires reference to Microsoft DAO library
    
    '' This function is intended as a "domain aggregate" that concatenates (and delimits) the
    '' various values rather than the more usual Count, Sum, Min, Max, etc.  For example:
    ''
    ''    Select Field1, DConcat("Field2", "SomeTable", "[Field1] = ''" & Field1 & "''") AS List
    ''    FROM SomeTable
    ''    GROUP BY Field1
    ''
    '' will return the distinct values of Field1, along with a concatenated list of all the
    '' distinct Field2 values associated with each Field1 value.
    
    '' ConcatColumns is a comma-delimited list of columns to be concatenated (typically just
    ''   one column, but the function accommodates multiple).  Place field names in square
    ''   brackets if they do not meet the customary rules for naming DB objects
    '' Tbl is the table/query the data are pulled from.  Place table name in square brackets
    ''   if they do not meet the customary rules for naming DB objects
    '' Criteria (optional) are the criteria to be applied in the grouping.  Be sure to use And
    ''   or Or as needed to build the right logic, and to encase text values in single quotes
    ''   and dates in #
    '' Delimiter1 (optional) is the delimiter used in the concatenation (default is ", ").
    ''   Delimiter1 is applied to each row in the code query''s result set
    '' Delimiter2 (optional) is the delimiter used in concatenating each column in the result
    ''   set if ConcatColumns specifies more than one column (default is ", ")
    '' Distinct (optional) determines whether the distinct values are concatenated (True,
    ''   default), or whether all values are concatenated (and thus may get repeated)
    '' Sort (optional) indicates whether the concatenated string is sorted, and if so, the
    ''   columns used for the sort.  As you would in an ORDER BY clause, use Asc or Desc to
    ''   indicate whether the column is sorted ascending or descending.  If Asc/Desc is
    ''   omitted, Asc is assumed by the query engine
    '' Limit (optional) places a limit on how many items are placed into the concatenated string.
    ''   The Limit argument works as a TOP N qualifier in the SELECT clause
    
    Dim rs As DAO.Recordset
    Dim SQL As String
    Dim ThisItem As String
    Dim FieldCounter As Long
    
    On Error GoTo ErrHandler
    
    '' Initialize to Null
    
    DConcat = Null
    
    '' Build up a query to grab the information needed for the concatenation
    
    SQL = "SELECT " & IIf(Distinct, "DISTINCT ", "") & _
            IIf(Limit > 0, "TOP " & Limit & " ", "") & _
            ConcatColumns & " " & _
        "FROM " & Tbl & " " & _
        IIf(Criteria <> "", "WHERE " & Criteria & " ", "") & _
        IIf(Sort <> "", "ORDER BY " & Sort, "")
        
    '' Open the recordset and loop through it:
    '' 1) Concatenate each column in each row of the recordset
    '' 2) Concatenate the resulting concatenated rows in the function''s return value
    
    Set rs = DBEngine(0)(0).OpenRecordset(SQL, dbOpenForwardOnly)
    With rs
        Do Until .EOF
            
            '' Initialize variable for this row
            
            ThisItem = ""
            
            '' Concatenate columns on this row
            
            For FieldCounter = 0 To rs.Fields.Count - 1
                ThisItem = ThisItem & Delimiter2 & Nz(rs.Fields(FieldCounter).Value, "")
            Next
            
            '' Trim leading delimiter
            
            ThisItem = Mid(ThisItem, Len(Delimiter2) + 1)
            
            '' Concatenate row result to function return value
            
            DConcat = Nz(DConcat, "") & Delimiter1 & ThisItem
            .MoveNext
        Loop
        .Close
    End With
    
    '' Trim leading delimiter
    
    If Not IsNull(DConcat) Then DConcat = Mid(DConcat, Len(Delimiter1) + 1)
    
    GoTo Cleanup

ErrHandler:
    
    '' Error is most likely an invalid database object name, or bad syntax in the Criteria
    
    DConcat = CVErr(Err.Number)
    
Cleanup:
    Set rs = Nothing
    
End Function



The sample query from the comment is for string type "foreign key", remove extra quotes if you have numeric type.


这篇关于Conc Function在MS Access中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆