Wednesday, 18 September 2013

MS Access - Details form returns same first record

MS Access - Details form returns same first record

I have eigtheen image controls in a form for browsing images in a multiple
column and rows arrangements which I am adopting from [Roggers website].1
It was challenging for me to understand fully how I could modify it for my
purpose but one person here helped me up to the stage where I could open a
details form upon clicking any one of the unbound image controls in the
form.
The problem I have now is that although the form opens it returns the same
data for the first record in the database regardless of which control I
click.
I know how to make a simple criteria for a single control like "[ItemID]
=" & Me.lstItems, but in this case the image controls are multiple and
arranged in a column and rows pattern. I think that the image controls are
populated with the this code, were "Im_" can be one of the eighteen image
controls:
Me("Im_" & Format(Cnt, "00")).Picture = Nz(rst.Fields("ImageFile"), "")
I need to know how I can add a criteria to the event or function so that
the form opens with related data in it.
This is the function for the click event:
Function OpenMyForm(strFormName As String)
DoCmd.OpenForm strFormName
End Function
This is the click event itself included in the code below under Private
Sub P_SetValues(Cnt As Long):
Me("Im_" & Format(Cnt, "00")).OnClick = "=OpenMyForm('F_Details')"
And the whole code behind the main form:
Private Sub P_FillControls()
On Error GoTo ErrTrap
Dim Cnt As Long
Cnt = 1
Do Until (Cnt > BlockSize Or rst.EOF)
P_SetValues Cnt
Cnt = Cnt + 1
rst.MoveNext
Loop
If Cnt <= BlockSize Then
For Cnt = Cnt To BlockSize
P_SetNulls Cnt
Next
End If
ExitPoint:
On Error GoTo 0
Exit Sub
ErrTrap:
MsgBox Err.Number & " - " & Err.Description
Resume ExitPoint
End Sub
Private Sub P_SetValues(Cnt As Long)
On Error GoTo ErrTrap
If RecCount > 0 Then
Me("Rn_" & Format(Cnt, "00")).Caption = (rst.AbsolutePosition + 1)
Else
Me("Rn_" & Format(Cnt, "00")).Caption = ""
End If
Me("Lb_" & Format(Cnt, "00")).Caption = Nz(rst.Fields("ImageName"), "")
Me("Im_" & Format(Cnt, "00")).Picture = Nz(rst.Fields("ImageFile"), "")
Me("ID_" & Format(Cnt, "00")) = rst.Fields("ImageFile")
' Note - For no caption, dot is used in lieu of
' zero length string, so as to prevent the
' label from disappearing
Me("Im_" & Format(Cnt, "00")).OnClick = "=OpenMyForm('F_Details')"
ExitPoint:
On Error GoTo 0
Exit Sub
ErrTrap:
MsgBox Err.Number & " - " & Err.Description
Resume ExitPoint
End Sub
Private Sub P_SetNulls(Cnt As Long)
On Error GoTo ErrTrap
Me("Rn_" & Format(Cnt, "00")).Caption = "."
Me("Lb_" & Format(Cnt, "00")).Caption = "."
Me("Im_" & Format(Cnt, "00")).Picture = "."
Me("ID_" & Format(Cnt, "00")) = Null
' Note - For no caption, dot is used in lieu of
' zero length string, so as to prevent the
' label from disappearing
ExitPoint:
On Error GoTo 0
Exit Sub
ErrTrap:
MsgBox Err.Number & " - " & Err.Description
Resume ExitPoint
End Sub
Public Sub P_Initialize()
On Error Resume Next
RecCount = 0 ' Default
Me.LbNoImage.Visible = False
' Remove any existing instance of the recordset
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
' This recordset will finally get closed in form's
' close event.
Set rst = CurrentDb.OpenRecordset("Q_ImageNormalSort")
' Set rst = CurrentDb.OpenRecordset("Q_Dynamic_Query")
If rst.EOF And rst.BOF Then
' There are no records
P_FillControls
Me.LbNoImage.Visible = True
P_SetStatusNavBtns
Me.CmdAdd.SetFocus
Exit Sub
End If
rst.MoveLast
RecCount = rst.RecordCount
LastID = rst.Fields("ImageFile")
rst.MoveFirst
FirstID = rst.Fields("ImageFile")
' First Load (signified by step size argument = 0)
P_Next 0
Me.LbRecMsg.Caption = "Of " & RecCount
On Error GoTo 0
End Sub
Function OpenMyForm(strFormName As String)
DoCmd.OpenForm strFormName
End Function
I will appreciate.
Joseph

No comments:

Post a Comment