2016-10-25 2 views
1

Access 2016recordset을 열고 다른 변수에서 데이터를 저장하려고하는데이 오류가 계속 발생합니다. 프로그램 자체에 더 많은 부분이 있지만이 부분에서 오류가 발생하며 단지 database의 데이터 만 업데이트합니다.VBA : 오류 3265 - "이 컬렉션에서 항목을 찾을 수 없습니다."

Option Compare Database 
Option Explicit 


Private Sub btnValidateTimesheet_Click() 

    ' Update timesheet to "Justificat" 

    Dim intIdTimesheet As Integer 

    If IsNull(cmbDraftTimesheets.Value) Then 
     MsgBox("You have to select a timesheet that is Borrador") 
     Exit Sub 
    End If 

    intIdTimesheet = cmbDraftTimesheets.Column(0) 

    DoCmd.SetWarnings False 
    DoCmd.RunSQL "update Timesheets set estat = ""Justificat"" where id=" & intIdTimesheet 
    DoCmd.SetWarnings True 

End Sub 


Private Sub btnValidateTimesheetLines_Click() 

    ' We select the timesheet_lines for employee, project, activity and dates selected 
    ' For each justification, a new "Justificat" Timesheet is generated which hang timesheet_lines 


    ' ------------------------------- Variables ------------------------------- 
    Dim dictTsLines As Object 
    Set dictTsLines = CreateObject("Scripting.Dictionary") 

    ' Form inputs 
    Dim intCodTreb As Integer 
    Dim strCodProj As String 
    Dim dateInici, dateFi As Date 
    Dim intExercici As Integer 

    ' Query strings 
    Dim strSQLFrom, strSQLWhere As String 
    Dim strSQLCount, strSQLJustAct, strSQLTsLines As String 

    ' Recordsets 
    Dim rsCount, rsJustAct, rsTimesheets, rsTsLines As Recordset 

    ' Aux and others... 
    Dim continue As Integer 
    Dim intIdJustificacio, intIdTs As Integer 
    Dim strActivitat As String 

    ' --------------------------------------- Main --------------------------------------------- 
    ' Taking form data 
    intCodTreb = cmbTreballador.Column(0) 
    strCodProj = cmbProjecte.Column(1) 
    dateInici = txtDataInici.Value 
    dateFi = txtDataFi.Value 

    ' We check the dates are correct 
    If IsNull(dateInici) Or IsNull(dateFi) Then 
     MsgBox("Dates can't be null") 
     Exit Sub 
    End If 

    If dateFi < dateInici Then 
     MsgBox("Start date must be earlier or the same as final date") 
     Exit Sub 
    End If 

    If year(dateInici) <> year(dateFi) Then 
     MsgBox("Dates must be in the same year") 
     Exit Sub 
    End If 

    intExercici = year(dateInici) 

    ' Make of the clause FROM and WHERE of the select query of timesheet_lines 
    strSQLFrom = " from (timesheet_lines tsl " & _ 
     " left join timesheets ts on tsl.timesheet_id = ts.id) " & _ 
     " left join justificacions j on j.id = ts.id_justificacio " 

    strSQLWhere = " where ts.estat = ""Borrador"" " & _ 
     " and tsl.data >= #" & Format(dateInici, "yyyy/mm/dd") & "# " & _ 
     " and tsl.data <= #" & Format(dateFi, "yyyy/mm/dd") & "# " 

    If Not IsNull(intCodTreb) Then 
     strSQLWhere = strSQLWhere & " and tsl.cod_treb = " & intCodTreb 
    End If 

    If Not IsNull(strCodProj) Then 
     strSQLWhere = strSQLWhere & " and j.cod_proj=""" & strCodProj & """ " 
    End If 

    ' Alert how much timesheet_lines are going to be validated 
    strSQLCount = "select count(*) " & strSQLFrom & strSQLWhere 
    Set rsCount = CurrentDb.OpenRecordset(strSQLCount) 
    Continue Do = MsgBox(rsCount(0) & " registries are going to be validated" & vbNewLine & _ 
     "Do you want to continue?", vbOKCancel) 

    If continue <> 1 Then 
     Exit Sub 
    End If 

    ' We select the tuples Justificacio, Activitat of timesheet_lines selected 
    strSQLJustAct = "select distinct ts.id_justificacio " & strSQLFrom & strSQLWhere 
    Set rsJustAct = CurrentDb.OpenRecordset(strSQLJustAct) 
    Set rsTimesheets = CurrentDb.OpenRecordset("Timesheets") 

    ' A new timesheet is generated for each tupla 
    Do While Not rsJustAct.EOF 
     intIdJustificacio = rsJustAct(0) 
     strActivitat = rsJustAct(1) 

     rsTimesheets.AddNew 
     rsTimesheets!data_generacio = Now() 
     rsTimesheets!estat = "Justificat" 
     rsTimesheets!Id_justificacio = intIdJustificacio 
     rsTimesheets!activitat = strActivitat 
     rsTimesheets!data_inici = dateInici 
     rsTimesheets!data_fi = dateFi 
     rsTimesheets!exercici = intExercici 
     intIdTs = rsTimesheets!Id 
     rsTimesheets.Update 

     ' We save the related id of the selected timesheet in a dictionary 
     dictTsLines.Add intIdJustificacio & "_" & strActivitat, intIdTs 

     rsJustAct.MoveNext 
    Loop 

    ' We select all the affected timesheet_lines and we update the related timesheet using the dictionary 
    strSQLTsLines = "select tsl.id, tsl.timesheet_id, ts.id_justificacio, ts.activitat " & strSQLFrom & strSQLWhere 
    Set rsTsLines = CurrentDb.OpenRecordset(strSQLTsLines) 
    With rsTsLines 
     Do While Not .EOF 
      .EDIT 
      intIdJustificacio = !Id_justificacio 
      strActivitat = !activitat 
      !timesheet_id = dictTsLines.Item(intIdJustificacio & "_" & strActivitat) 
      .Update 
      .MoveNext 
     Loop 
    End With 

    rsTimesheets.Close 
    Set rsCount = Nothing 
    Set rsJustAct = Nothing 
    Set rsTimesheets = Nothing 
    Set rsTsLines = Nothing 

End Sub 

디버거 :

이 내 코드 오류는 줄에서 올라오고있다 : 나는 recordset가 저장되는 데이터가 존재하고 않는다는 것을 확인

strActivitat = rsJustAct(1) 

.

답변

1

귀하의 레코드는 하나의 열 ("select distinct ts.id_justificacio")를 포함,하지만 당신은 레코드 세트 두 번째 열을 strActivitat = rsJustAct(1)

추가 requred 열을 읽기 위해 노력하고 있습니다.

+0

정말 고마워요! – bimmer55