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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌套do而不是基于select查询访问vba的.eof循环相关的知识,希望对你有一定的参考价值。

我基本上试图将垂直数据转换为水平(如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

答案

您应该只在员工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
另一答案

我已经解决了!!它确实需要一个嵌套循环。而不是一个查询带来总记录(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

以上是关于嵌套do而不是基于select查询访问vba的.eof循环的主要内容,如果未能解决你的问题,请参考以下文章

访问VBA代码不将变量设置为查询值

如何在文件夹而不是 Excel VBA 中的 CurrentDB 中定义访问数据库的路径?

访问 VBA。检测记录集条目是不是会溢出

基于已回答问题的访问报告

从 Excel VBA 运行嵌套的 Access SQL 查询

在基于非绑定列重新查询后访问 VBA 还原列表框选择