将查询的Recordset附加到Access中的表

时间:2016-06-16 23:53:23

标签: sql vba ms-access

我正在查询Active Directory以列出Access中的用户和其他字段。有没有办法将查询结果附加到现有表中?目前我正在尝试使用INSERT INTO,但是我的Object变量没有被设置或阻塞变量。

Private Sub Command0_Click()

Dim objRecordSet As Object
Dim objCommand As Object
Dim objConnection As Object
Dim dbs As Database

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Sort On") = "whenCreated"

objCommand.CommandText = _
"SELECT Name,Title,PhysicalDeliveryOfficeName,WhenCreated,Mail FROM 'LDAP://OU=Standard Users,OU=Active Users,OU=All Users,DC=contoso,dc=local' WHERE objectCategory='user'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
  dbs.Execute " INSERT INTO ADUsers" & "(Name,Title,Site,Created,Email) VALUES " & "(objRecordSet.Fields('Name').Value,objRecordSet.Fields('Title').Value,objRecordSet.Fields('physicalDeliveryOfficeName').Value,objRecordSet.Fields('whenCreated').Value,objRecordSet.Fields('Mail').Value);"
  dbs.Close
  Debug.Print objRecordSet.Fields("Name").Value; "," & objRecordSet.Fields("Title").Value; "," & objRecordSet.Fields("physicalDeliveryOfficeName").Value; "," & objRecordSet.Fields("whenCreated").Value; "," & objRecordSet.Fields("Mail").Value

objRecordSet.MoveNext

Loop

End Sub

2 个答案:

答案 0 :(得分:0)

doublequotes "中的所有内容都被解释为字符串而不是代码和字符串(objRecordSet.Fields("myFieldName").Value的值)必须在insert语句中引用。

dim strSQLInsert as String

strSQLInsert = "INSERT INTO ADUsers(Name,Title,Site,Created,Email) VALUES ('" & _ 
  objRecordSet.Fields("Name").Value & "','" & _
  objRecordSet.Fields("Title").Value & "','" & 
  objRecordSet.Fields("physicalDeliveryOfficeName").Value & "','" & _
  objRecordSet.Fields("whenCreated").Value & "','" & _
  objRecordSet.Fields("Mail").Value & "');"

Debug.Print strSQLInsert

dbs.Execute strSQLInsert

将sql语句存储在字符串中,然后可以使用Debug.Print进行检查。

答案 1 :(得分:0)

使用querydefs考虑​​参数化查询以避免引用的需要。还要确保初始化可能是您的主要问题的数据库对象:set dbs = CurrentDb

...
Dim strSQL As String
Set dbs = CurrentDb

strSQL = "PARAMETERS NameParm TEXT(255), TitleParam TEXT(255), SiteParam TEXT(255)," _
             & " CreatedParm Date, EmailParam TEXT(255);" _
             & " INSERT INTO ADUsers (Name, Title, Site, Created, Email)" _
             & " VALUES ([NameParm], [TitleParam], [SiteParam], [Created], [Email]);"

Do Until objRecordSet.EOF

   Set qdef = dbs.CreateQueryDef("", strSQL)

   qdef!NameParam = objRecordSet![Name]
   qdef!TitleParam = objRecordSet![Title]
   qdef!SiteParam = objRecordSet![PhysicalDeliveryOfficeName]
   qdef!CreatedParam = objRecordSet![WhenCreated]
   qdef!EmailParam = objRecordSet![Mail]

   qdef.Execute (dbfailOnError)
   Set qdef = Nothing

   objRecordSet.MoveNext
Loop