【问题标题】:DAO access recordset not updatingDAO 访问记录集未更新
【发布时间】:2012-03-28 20:15:45
【问题描述】:

我有一个访问报告,它根据从目录中获取图像的表更新 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

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

谢谢

【问题讨论】:

  • 我不知道,但似乎微软在新版本中随机删除了各种 API 功能。当我从 Access 2000 更新到 2007 时,由于 FileDialog 和 RecordSet.RecordCount 不再存在,导致许多功能被破坏。
  • 您确定在第 1 页之后调用了 UpdateImages - 有时页面事件不会触发...
  • @DJ.,似乎它只点击了 UpdateImage 程序 4 次。如何让每页触发事件?

标签: ms-access vba dao recordset


【解决方案1】:

每当我需要做一些动态内容时,我总是使用 [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

【讨论】:

    【解决方案2】:

    我从未见过 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”,因为这已经是默认返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多