DBPix Sample Source Code: fsubViewSubImage Back to sample
Option Compare Database
Option Explicit
Const PageSize = 8 ' Number of images displayed per 'page'
Dim CurrentPage As Long ' The current 'page' number
Dim rst As ADODB.Recordset
' Array of Id's for each record (image), used to open Detail (Zoom) form at a specific record
Dim Id(1 To PageSize) As Long
Private Sub Form_Close()
Set rst = Nothing ' Free any open recordset
End Sub
' Fires when this subform initially opened, and when navigating through *master* records
Private Sub Form_Current()
CurrentPage = 1
ClearControls
' ItemId is the Id of the master record, via the subform linked Master/Child fields.
If Not Me!ItemId And Me!ItemId <> "" Then ' Make sure master record Id not Null or empty
Set rst = Nothing ' Free any open recordset
Set rst = New ADODB.Recordset ' Create new recordset
With rst
.PageSize = PageSize
.ActiveConnection = CurrentProject.Connection
.Source = "SELECT ImageId FROM tblItemSubImages WHERE ItemId = " & Me!ItemId
.LockType = adLockOptimistic
.CursorType = adOpenStatic
.Open
End With
UpdateControls
Else
' No sub-images for this record
NextBtn.Enabled = False
PrevBtn.Enabled = False
PageLabel.Value = "Page 1 of 1"
End If
End Sub
Private Sub NextBtn_Click()
ClearControls
CurrentPage = CurrentPage + 1
UpdateControls
End Sub
Private Sub PrevBtn_Click()
ClearControls
CurrentPage = CurrentPage - 1
UpdateControls
End Sub
Private Sub ClearControls()
' Loop through the DBPix controls, clear the image and reset the record Id
Dim i As Long
For i = 1 To PageSize
Me("ActiveXCtl" & i).ImageViewBlob Null ' Clear the image
Id(i) = -1 ' Store a blank Id
Next i
End Sub
Private Sub UpdateControls()
' Get the number of pages, and update the text and Prev/Next button states
Dim NumPages As Long
NumPages = rst.PageCount
PageLabel.Value = "Page " & CurrentPage & " of " & NumPages
' Check CurrentPage within valid limits
If CurrentPage > NumPages Then CurrentPage = NumPages
If CurrentPage < 1 Then CurrentPage = 1
' Move recordset to the page we want to display
rst.AbsolutePage = CurrentPage
'Update Prev/Next buttons
If CurrentPage = NumPages Then
DoCmd.GoToControl "ActiveXCtl1" ' Make sure focus not on 'NextBtn' before disabling
NextBtn.Enabled = False
Else
NextBtn.Enabled = True
End If
If CurrentPage = 1 Then
DoCmd.GoToControl "ActiveXCtl1" ' Make sure focus not on 'PrevBtn' before disabling
PrevBtn.Enabled = False
Else
PrevBtn.Enabled = True
End If
Dim BasePath As String
Dim ImgPath As String
BasePath = GetImgFolder & "\images\sub\"
' Loop through the DBPix controls displaying the images. The DBPix controls must be named
' ActiveXCtl1, ActiveXCtl2, ActiveXCtl3 etc (up to the value of 'PageSize').
Dim i As Long
For i = 1 To PageSize
If (Not rst.EOF) Then
ImgPath = BasePath & rst("ImageId") & "t.jpg"
If Dir(ImgPath) <> vbNullString Then
Me("ActiveXCtl" & i).ImageViewFile ImgPath
End If
Id(i) = rst("ImageId")
rst.MoveNext
End If
Next i
End Sub
' Generic handler for clicks - opens 'Zoom' view
Private Sub HandleClick(Id As Long)
If (Id > -1) Then
Dim ImgPath As String
ImgPath = GetImgFolder & "\images\sub\" & Id & ".jpg"
' Don't open the zoomform unless the image exists
If Dir(ImgPath) <> vbNullString Then
' Open Zoom form, passing a 'WHERE' condition to specify which image to show.
DoCmd.OpenForm "frmZoomSubImg", acNormal, , "[ImageId ]=" & Id, , acDialog
Else
Beep
End If
Else
Beep
End If
End Sub
' Click event handlers for each control
Private Sub ActiveXCtl1_Click()
HandleClick (Id(1)) ' Open Zoom form, using Id associated with this image
End Sub
Private Sub ActiveXCtl2_Click()
HandleClick (Id(2)) ' Open Zoom form, using Id associated with this image
End Sub
Private Sub ActiveXCtl3_Click()
HandleClick (Id(3)) ' Open Zoom form, using Id associated with this image
End Sub
Private Sub ActiveXCtl4_Click()
HandleClick (Id(4)) ' Open Zoom form, using Id associated with this image
End Sub
Private Sub ActiveXCtl5_Click()
HandleClick (Id(5)) ' Open Zoom form, using Id associated with this image
End Sub
Private Sub ActiveXCtl6_Click()
HandleClick (Id(6)) ' Open Zoom form, using Id associated with this image
End Sub
Private Sub ActiveXCtl7_Click()
HandleClick (Id(7)) ' Open Zoom form, using Id associated with this image
End Sub
Private Sub ActiveXCtl8_Click()
HandleClick (Id(8)) ' Open Zoom form, using Id associated with this image
End Sub
Back to sample
|