DBPix Sample Source Code: frmMain Back to sample
Option Explicit
Option Compare Database
' When the user deletes a record we have to save the current Id in order to delete the image files
' after the delete is confirmed
Dim DeleteImgId As Long
Dim arrDeleteSubImgIds As Variant
Dim EditMode As Boolean
Private Sub Form_Load()
' Initialize EditMode
EditMode = False
End Sub
Private Sub Form_Current()
Dim DetailImgPath As String
DBPixThumb.ImageViewBlob (Null)
If IsNull(Me!Id) Then
DBPixMain.ImageViewBlob (Null)
Else
DetailImgPath = GetImgFolder & "\images\main\" & Me!Id & ".jpg"
If Dir(DetailImgPath) <> vbNullString Then
DBPixMain.ImageViewFile DetailImgPath
Else
DBPixMain.ImageViewBlob (Null)
End If
End If
End Sub
Private Sub DBPixMain_ImageModified()
Dim DetailImgPath As String
Dim ThumbImgPath As String
'In order to have access to the autonumber value (for the filename) we must have modified the record,
' so update the ImageModified field
Me!ImageModified = Now()
DetailImgPath = GetImgFolder & "\images\main\" & Me!Id & ".jpg"
ThumbImgPath = GetImgFolder & "\images\main\" & Me!Id & "t.jpg"
If DBPixMain.ImageBytes < 1 Then
' Image is empty/cleared/deleted
' Clear the thumbnail control
DBPixThumb.ImageViewBlob (Null)
' Delete the detail and thumbnail image files if they exist
If Dir(DetailImgPath) <> vbNullString Then Kill DetailImgPath
If Dir(ThumbImgPath) <> vbNullString Then Kill ThumbImgPath
Else
' Save the image
If DBPixMain.ImageSaveFile(DetailImgPath) Then
DBPixThumb.ImageLoadBlob (DBPixMain.Image)
DBPixThumb.ImageSaveFile (ThumbImgPath)
Else
' Error saving - main image - clear the thumbnail control
DBPixThumb.ImageViewBlob (Null)
End If
End If
End Sub
' Open the Zoom (detail) view when the main image is clicked
Private Sub DBPixMain_Click()
Dim ImgPath As String
ImgPath = GetImgFolder & "\images\main\" & Me!Id & ".jpg"
' Don't open the zoomform unless the image exists
If Dir(ImgPath) <> vbNullString Then
Dim strWhereCategory
strWhereCategory = "[Id ]=" & Me!Id
DoCmd.OpenForm "frmZoomMainImg", acNormal, , strWhereCategory, , acDialog
Else
Beep
End If
End Sub
' Switch between sub-image 'View' mode and 'Edit' mode (swaps between the 2 subforms)
Private Sub btnEditSubImages_Click()
If EditMode Then
subSubImages.SourceObject = "fsubViewSubImg"
subSubImages.LinkChildFields = "ItemId"
subSubImages.LinkMasterFields = "Id"
EditMode = False
btnEditSubImages.Caption = "Edit"
Else
subSubImages.SourceObject = "fsubEditSubImg"
subSubImages.LinkChildFields = "ItemId"
subSubImages.LinkMasterFields = "Id"
EditMode = True
btnEditSubImages.Caption = "View"
End If
End Sub
Private Sub Form_Delete(Cancel As Integer)
' Record delete about to occur - save the record Id's so we can delete the image files if the delete is confirmed
DeleteImgId = 0
If IsArray(arrDeleteSubImgIds) Then Set arrDeleteSubImgIds = Nothing
If Not IsNull(Me!Id) Then
' Save the Id of the main image so that the image files can be deleted in AfterDelConfirm
DeleteImgId = Me!Id
' Fetch the Id's of the sub images and save in an array, so that the image files can be deleted in AfterDelConfirm
Dim cn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cn = CurrentProject.Connection
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = cn
.Source = "SELECT ImageId FROM tblItemSubImages WHERE ItemId = " & Me!Id
.LockType = adLockOptimistic
.CursorType = adOpenStatic
.Open
End With
If rst.RecordCount > 0 Then arrDeleteSubImgIds = rst.GetRows
rst.Close
cn.Close
Set rst = Nothing
Set cn = Nothing
End If
End Sub
Private Sub Form_AfterDelConfirm(Status As Integer)
If Status = acDeleteOK Then
If DeleteImgId > 0 Then
Dim DetailImgPath As String
Dim ThumbImgPath As String
DetailImgPath = GetImgFolder & "\images\main\" & DeleteImgId & ".jpg"
ThumbImgPath = GetImgFolder & "\images\main\" & DeleteImgId & "t.jpg"
' Delete the detail and thumbnail image files if they exist
If Dir(DetailImgPath) <> vbNullString Then Kill DetailImgPath
If Dir(ThumbImgPath) <> vbNullString Then Kill ThumbImgPath
End If
If IsArray(arrDeleteSubImgIds) Then
Dim x As Integer
For x = 0 To UBound(arrDeleteSubImgIds, 2)
DetailImgPath = GetImgFolder & "\images\sub\" & arrDeleteSubImgIds(0, x) & ".jpg"
ThumbImgPath = GetImgFolder & "\images\sub\" & arrDeleteSubImgIds(0, x) & "t.jpg"
If Dir(DetailImgPath) <> vbNullString Then Kill DetailImgPath
If Dir(ThumbImgPath) <> vbNullString Then Kill ThumbImgPath
Next x
End If
End If
Set arrDeleteSubImgIds = Nothing
DeleteImgId = 0
End Sub
Back to sample
|