记录集类型不匹配问题

时间:2014-03-26 14:48:52

标签: sql vba ms-access access-vba

我处于这个项目的尾端,通过ADO连接下载给我一些最终的速度颠簸。我一直收到

的类型不匹配错误
Set targetrs = CurrentDb.OpenRecordset("tblEmpData", dbOpenTable)

整个代码是:

Public Sub DownloadData()

Dim targetrs As New ADODB.Recordset

Set targetrs = CurrentDb.OpenRecordset("tblEmpData", dbOpenTable)

Dim Con As New ADODB.Connection
Dim sqlStr As String
Con.Open _
    "Provider = sqloledb;" & _
    "Data Source=SERVER;" & _
    "Initial Catalog=CAT;" & _
    "User ID=ID;" & _
    "Password=PW;"

Dim rsSource As New ADODB.Recordset

rsSource.Open "SELECT dbo.TMS_employee_HR.Region AS REG_NR, dbo.TMS_employee_HR.District AS DIS_NR, dbo.TMS_employee_HR.GEMSID, dbo.TMS_employee_HR.TMS_ID," & _
           " dbo.TMS_employee_HR.Last_Name + ',  ' + dbo.TMS_employee_HR.First_Name AS EMP_NA, dbo.TMS_employee_HR.Gender AS EMP_SEX_TYP_CD," & _
           " dbo.TMS_employee_HR.EEOC AS EMP_EOC_GRP_TYP_CD, dbo.TMS_employee_HR.Division AS DIV_NR, dbo.TMS_employee_HR.Center AS CTR_NR," & _
           " dbo.TMS_employee_HR.Job_Title AS JOB_CLS_CD_DSC_TE, dbo.TMS_employee_HR.Job_Group_Code AS JOB_GRP_CD, dbo.TMS_employee_HR.Job_Function," & _
           " dbo.TMS_employee_HR.Job_Group, dbo.TMS_Data_Latest_Career_Meeting_Rating.Meeting_Readiness_Rating," & _
           " dbo.TMS_Data_Latest_Career_Meeting_Rating.Manager_Readiness_Rating" & _
           " FROM  dbo.TMS_Data_Latest_Career_Meeting_Rating RIGHT OUTER JOIN" & _
           " dbo.TMS_employee_HR ON dbo.TMS_Data_Latest_Career_Meeting_Rating.Employee_ID = dbo.TMS_employee_HR.TMS_ID" & _
           " WHERE (dbo.TMS_employee_HR.Employment_Status = 'Active') AND (dbo.TMS_Data_Latest_Career_Meeting_Rating.Meeting_Year = '2013') OR" & _
           " (dbo.TMS_Data_Latest_Career_Meeting_Rating.Employee_ID IS NULL)" & _
           " GROUP BY dbo.TMS_employee_HR.Region, dbo.TMS_employee_HR.District, dbo.TMS_employee_HR.GEMSID, dbo.TMS_employee_HR.TMS_ID," & _
           " dbo.TMS_employee_HR.Gender, dbo.TMS_employee_HR.Last_Name + ',  ' + dbo.TMS_employee_HR.First_Name, dbo.TMS_employee_HR.EEOC," & _
           " dbo.TMS_employee_HR.Division, dbo.TMS_employee_HR.Center, dbo.TMS_employee_HR.Job_Title, dbo.TMS_employee_HR.Job_Group_Code," & _
           " dbo.TMS_employee_HR.Job_Function, dbo.TMS_employee_HR.Job_Group, dbo.TMS_Data_Latest_Career_Meeting_Rating.Meeting_Readiness_Rating," & _
           " dbo.TMS_Data_Latest_Career_Meeting_Rating.Manager_Readiness_Rating; ", Con

Do Until rsSource.EOF
    targetrs.AddNew
'        For Each Field In rsSource
'            targetrs.Fields(Field.Name) = rsSource.Fields(Field.Name)
'
'        Next
    targetrs!Region = rsSource!REG_NR
    targetrs!District = rsSource!DIS_NR
    targetrs!EmployeeID = rsSource!TMS_ID
    targetrs!EmployeeName = rsSource!EMP_NA
    targetrs!Gender = rsSource!EMP_SEX_TYP_CD
    targetrs!EEOC = rsSource!EMP_EOC_GRP_TYP_CD
    targetrs!Division = rsSource!DIV_NR
    targetrs!Center = rsSource!CTR_NR
    targetrs!JobTitle = rsSource!JOB_CLS_CD_DSC_TE
    targetrs!JobGroupCode = rsSource!JOB_GRP_CD
    targetrs!Function = rsSource!Job_Function
    targetrs!JobGroup = rsSource!Job_Group
    targetrs!MeetingReadinessRating = rsSource!Meeting_Readiness_Rating
    targetrs!ManagerReadinessRating = rsSource!Manager_Readiness_Rating
    targetrs.Update
    rsSource.MoveNext
Loop

rsSource.Close
targetrs.Close


End Sub

我是否错误地指定了记录集?

2 个答案:

答案 0 :(得分:3)

OpenRecordset是DAO方法并返回DAO记录集。

'Dim targetrs As New ADODB.Recordset
Dim targetrs As DAO.Recordset
Set targetrs = CurrentDb.OpenRecordset("tblEmpData", dbOpenTable)

答案 1 :(得分:2)

坚持使用adodb。 你已经在其他地方使用它了。

dim targetrs as new adodb.recordset
targetrs.open "Select * From tblEmpData", CurrentProject.Connection