DAO访问记录集未更新

时间:2012-03-28 20:15:45

标签: ms-access vba dao recordset

我有一个访问报告,它根据从目录中获取图像的表来更新4个图像控件。该报告生成每个记录的页面,但是在第1页之后图像控件不会更改(仅显示所有其他页面的相同图像)。在公寓上,代码在Windows XP上运行良好,现在不适用于Windows 7操作系统(均使用Office 07)。这是代码:

Private Sub Report_Current()

    UpdateImages
End Sub

Private Sub Report_Load()

    UpdateImages
End Sub
Private Sub Report_Page()

    UpdateImages
End Sub

Private Sub UpdateImages()
On Error GoTo errHandler
    Dim RS As DAO.Recordset

    Set RS = CurrentDb.OpenRecordset("SELECT Image_Loc, Image_Name FROM HH_Media WHERE InspectionID = " & CInt(Me.InspectionID.Value) & " ORDER BY MediaID ASC")

    If Not RS.BOF And Not RS.EOF Then
        Dim i As Integer
        For i = 1 To 4
            If Not RS.EOF Then
                Dim pictureCtrl As Image
                Set pictureCtrl = GetControl("Image" & i)

                Dim strImage As String
                strImage = RS.Fields("Image_Loc").Value & "\" & RS.Fields("Image_Name").Value


               If Not pictureCtrl Is Nothing Then
                    If FileExists(strImage) Then
                        pictureCtrl.Picture = strImage
                        SetLabel "lblImage" & i, RS.Fields("Image_Name").Value
                    Else
                        pictureCtrl.Picture = ""
                        SetLabel "lblImage" & i, "Does not exist"
                    End If
               End If

              RS.MoveNext

            Else
                Exit For
            End If

        Next

    End If

    RS.Close
    Set RS = Nothing

Exit Sub

errHandler:
    MsgBox "An error occurred while updating the form display." & vbNewLine & Err.Description, vbApplicationModal + vbCritical + vbDefaultButton1 + vbOKOnly, Me.Name
    Resume Next
End Sub

图像确实存在于表中引用的目录中。什么缺失的想法?

谢谢

2 个答案:

答案 0 :(得分:2)

每当我需要做一些动态内容时,我总是使用[section] _Format事件 - 所以如果你的控件在Detail部分,那么:

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

  If FormatCount = 1 then  'only need to do this once per record
    UpdateImages
  Endif

End Sub

答案 1 :(得分:0)

我从来没有见过GetControl方法,而且我没有很多使用Image控件的经验,但看起来Dim语句应该更像是:

Dim pictureCtrl as Control
Set pictureCtrl = Me.Controls("Image" & i)

我会插入一个中断并验证

strImage = RS.Fields("Image_Loc").Value & "\" & RS.Fields("Image_Name").Value

返回您期望的值。您还可以将这些缩短为:

strImage = rs!Image_Loc & "\" & rs!Image_Name

有时Access不喜欢添加的“.value”,因为这已经是默认返回。