正如我在评论中发布的那样,group_concat()
在 MySQL 中使用该功能很容易,但是如果您想在 MS Access 中执行此操作,我认为您必须使用 VBA 来处理这个问题。
我建议你这个功能:
public function concatenatePermits(licNo as integer, year as integer)
dim db as DAO.database, rec as DAO.recordset, strSQL as string
dim ans as string
set db = currentdb()
strSQL = "select permit from [your table] " & _
"where [lic#]=" & licNo & " and year=" & year & ";"
set rec = db.openrecordset(strSQL, dbOpenDynaset, dbReadOnly)
ans = ""
with rec
.moveFirst
do
if ans = "" then
ans = !permit
else
ans = ans & "," & !permit
end if
loop until .EOF
end with
rec.close
db.close
concatenatePermits = ans
end function
此函数可用于任何查询。缺点:如果你的表真的很大,使用这个函数的查询的执行会很慢。我认为更好的方法是创建一个空表,然后使用 VBA 逐行填充它。
希望这对您有所帮助。
使用 VBA 添加行
在您的评论中,您询问如何使用 VBA 向表中添加行。假设该表存在并且您有要输入该表的数据,我建议您这样做:
public sub addData()
dim db as dao.database, recOut as dao.recordset
' Declare all the variables you need for your code'
set db = currentdb()
' recOut will be the table where you want to store your data '
set recIn = db.openRecordset("tblYourOutTable",dbOpenDynaset,dbEditAdd)
' At some point in your code you will need to store data in your table: '
with recOut
.addNew
![A_Field] = value1
![Another_field] = value2
![Yet_another_field] = value3
.update
end with
' Close the recordset and the database objects '
rec.close
db.close
end sub