|
给你写了个过程,假设你的表是表1,执行查询后的值写入表2.代码如下:
Sub MyQuery()
Dim conn As New ADODB.Connection
Dim rec As New ADODB.Recordset
Dim rec2 As New ADODB.Recordset
Dim rec3 As New ADODB.Recordset
Dim strSQL As String
Dim strText As String
Set conn = CurrentProject.Connection
conn.Execute "DELETE 表2.* FROM 表2;" '清除原表的内容
strSQL = "SELECT DISTINCT 表1.xm FROM 表1;"
rec.Open strSQL, conn, adOpenKeyset, adLockPessimistic
Do While Not rec.EOF
strSQL = "SELECT * FROM 表1 "
strSQL = strSQL & "WHERE xm = '" & rec("xm") & "'"
rec2.Open strSQL, conn, adOpenKeyset, adLockPessimistic
strText = ""
Do While Not rec2.EOF
strText = strText & rec2("aa") & " "
rec2.MoveNext
Loop
strSQL = "SELECT * FROM 表2;"
rec3.Open strSQL, conn, adOpenKeyset, adLockPessimistic
rec3.AddNew
rec3("xm") = rec("xm")
rec3("aa") = strText
rec3.Update
rec3.Close
rec2.Close
rec.MoveNext
Loop
rec.Close
Set rec = Nothing
Set rec2 = Nothing
Set rec3 = Nothing
Set conn = Nothing
End Sub
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|