嵌套do而不是基于select查询访问vba的.eof循环

时间:2017-12-11 23:27:53

标签: vba ms-access access-vba

我基本上试图将垂直数据转换为水平数据(如excel表)。我必须使用嵌套循环,但不知道如何去做!到目前为止,这里的代码是有效的,但不是我想要的:

strSQL = "SELECT * FROM tblTimeSheetData WHERE [WorkDate] BETWEEN #" & 
StartDate & "# AND #" & EndDate & "#" _ 
  & "ORDER BY [EmpID], [WorkDate] ;"    

Set rstTime = CurrentDb.OpenRecordset(strSQL)   

Do While Not rstTime.EOF    
    rstTemp.AddNew
    lngEmpID = rstTime![EmpID]
    rstTemp![EmpID] = lngEmpID

    intWeekday = Weekday(rstTime![WorkDate])

    Select Case intWeekday

        Case 2
           rstTemp![MondayWorkDate] = rstTime![WorkDate]
           rstTemp![MondayWorkHours] = rstTime![WorkHours]

        Case 3
           rstTemp![TuesdayWorkDate] = rstTime![WorkDate]
           rstTemp![TuesdayWorkHours] = rstTime![WorkHours]

        Case 4
           rstTemp![WednesdayWorkDate] = rstTime![WorkDate]
           rstTemp![WednesdayWorkHours] = rstTime![WorkHours]

        Case 5
           rstTemp![ThursdayWorkDate] = rstTime![WorkDate]
           rstTemp![ThursdayWorkHours] = rstTime![WorkHours]

        Case 6
           rstTemp![FridayWorkDate] = rstTime![WorkDate]
           rstTemp![FridayWorkHours] = rstTime![WorkHours]

     End Select

     rstTemp.Update
     rstTime.MoveNext
  Loop

我的选择查询完全返回我想要的内容: Select query result

我的代码返回: Code result

我想要它返回: Horizontal view

谢谢。 AG

2 个答案:

答案 0 :(得分:0)

您应该只在员工ID与上一个员工ID不同时添加新记录:

Dim currEID As Long 'tracks the current employee id

'...


currEID = -1

Do While Not rstTime.EOF

    lngEmpID = rstTime![EmpID]

    'new employee?  If yes then add a new record
    If currEID <> lngEmpID Then
        rstTemp.AddNew
        rstTemp![EmpID] = lngEmpID
        currEID = lngEmpID
    End If

    intWeekday = Weekday(rstTime![WorkDate])

    Select Case intWeekday

     '....rest of select case here

    End Select

    rstTime.MoveNext
Loop

rstTemp.UpdateBatch

答案 1 :(得分:0)

我已经解决了!!它确实需要一个嵌套循环。而不是一个查询带来总记录(5)我在嵌套循环中做了两个查询(2然后是3和2),如下所示,这就是诀窍。感谢。

Set rstTemp = CurrentDb.OpenRecordset("tblTimeSheetDataTemp")
Set rstTime = CurrentDb.OpenRecordset("tblTimeSheetData")

'this next query finds two distinct records John and Helen's EmpID
strSqla = "SELECT DISTINCT tblTimeSheetData.EmpID FROM tblTimeSheetData 
WHERE [WorkDate] BETWEEN #" & StartDate & "# AND #" & EndDate & "#"

Set rstCts = CurrentDb.OpenRecordset(strSqla, dbOpenDynaset)

Do While Not rstCts.EOF

  lngEmpID = rstCts![EmpID]
  rstTemp.AddNew

  'This next query finds the number of records with John's (or Helen's) 
  'EmpIDs between Start and End dates
  strSql = "SELECT * FROM tblTimeSheetData WHERE [EmpID] = " & lngEmpID & " 
AND [WorkDate] BETWEEN #" & StartDate & "# AND #" & EndDate & "#"
  Set rstTime = CurrentDb.OpenRecordset(strSql, dbOpenDynaset)

  Do While Not rstTime.EOF

     rstTemp![EmpID] = lngEmpID

     intWeekday = Weekday(rstTime![WorkDate])

     Select Case intWeekday

        Case 2
           rstTemp![MondayWorkDate] = rstTime![WorkDate]
           rstTemp![MondayWorkHours] = rstTime![WorkHours]

        Case 3
           rstTemp![TuesdayWorkDate] = rstTime![WorkDate]
           rstTemp![TuesdayWorkHours] = rstTime![WorkHours]

        Case 4
           rstTemp![WednesdayWorkDate] = rstTime![WorkDate]
           rstTemp![WednesdayWorkHours] = rstTime![WorkHours]

        Case 5
           rstTemp![ThursdayWorkDate] = rstTime![WorkDate]
           rstTemp![ThursdayWorkHours] = rstTime![WorkHours]

        Case 6
           rstTemp![FridayWorkDate] = rstTime![WorkDate]
           rstTemp![FridayWorkHours] = rstTime![WorkHours]

     End Select
  rstTime.MoveNext
  Loop

rstTemp.Update
rstCts.MoveNext

Loop