ACCESS 2003中,有图中一张表A,现在想通过SQL语句生成表B,请各路大侠相助!!! 5
2个回答
展开全部
Dim rs As Recordset
Public Function GetStr(TableName As String, sField As String, sValue As Variant, tField As String, Optional c As String)
If IsMissing(c) Then c = ";"
Set rs = CurrentDb.OpenRecordset("select [" & tField & "] from [" & TableName & "] where [" & sField & "]='" & sValue & "'")
While Not rs.EOF
GetStr = GetStr & rs.Fields(0).Value & c
rs.MoveNext
Wend
If Len(GetStr) > 0 Then GetStr = Left(GetStr, Len(GetStr) - Len(c))
End Function
SELECT F1, Getstr('A','F1',F1,'F2',',')
FROM A GROUP BY F1;
前面的代码放到Access的模块里面去,可以在Access界面按ALT+F11进去,左边右击插入模块
后面的语句用来查询。
函数的参数解释分别为:表名、查找的列名、查找的值、连接字串的列名、分割字符
在这里表示把表A中的F2列根据F1列的值用逗号连起来
Public Function GetStr(TableName As String, sField As String, sValue As Variant, tField As String, Optional c As String)
If IsMissing(c) Then c = ";"
Set rs = CurrentDb.OpenRecordset("select [" & tField & "] from [" & TableName & "] where [" & sField & "]='" & sValue & "'")
While Not rs.EOF
GetStr = GetStr & rs.Fields(0).Value & c
rs.MoveNext
Wend
If Len(GetStr) > 0 Then GetStr = Left(GetStr, Len(GetStr) - Len(c))
End Function
SELECT F1, Getstr('A','F1',F1,'F2',',')
FROM A GROUP BY F1;
前面的代码放到Access的模块里面去,可以在Access界面按ALT+F11进去,左边右击插入模块
后面的语句用来查询。
函数的参数解释分别为:表名、查找的列名、查找的值、连接字串的列名、分割字符
在这里表示把表A中的F2列根据F1列的值用逗号连起来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询