Please explain this code

mcdhappy80

Registered User.
Local time
Today, 13:54
Joined
Jun 22, 2009
Messages
347
This code is a converted macro which opens a form when You click on a hyperlik in datasheet table:
Code:
Option Compare Database

'------------------------------------------------------------
' HyperlinkOpen
'
'------------------------------------------------------------
Function HyperlinkOpen()
On Error GoTo HyperlinkOpen_Err

    With CodeContextObject
        On Error Resume Next
        If (.Form.Dirty) Then
            DoCmd.RunCommand acCmdSaveRecord
        End If
        If (.MacroError.Number <> 0) Then
            Beep
            MsgBox .MacroError.Description, vbOKOnly, ""
            Exit Function
        End If
        On Error GoTo 0
        DoCmd.OpenForm "Expense Report Details", acNormal, "", "[ID]=" & Nz(.ID, 0), , acDialog
        If (Not IsNull(.ID)) Then
            TempVars.Add "CurrentID", "[ID]"
        End If
        If (IsNull(.ID)) Then
            TempVars.Add "CurrentID", "Nz(DMax(""[ID]"",[Form].[RecordSource]),0)"
        End If
        DoCmd.Requery ""
        DoCmd.SearchForRecord , "", acFirst, "[ID]=" & TempVars!CurrentID
        TempVars.Remove "CurrentID"
    End With


HyperlinkOpen_Exit:
    Exit Function

HyperlinkOpen_Err:
    MsgBox Error$
    Resume HyperlinkOpen_Exit

End Function
If someone could explain code line by line.
Thank You.
 
>>>On Error GoTo 0<<<

I can’t see that this line of code does anything particularly useful, again I would be inclined to rem it out do some tests to see if it’s required.


i think that line just clears any active error handler
 
Thank You for Your answers guys, I have a few questions.

This code I've shown You is used to open the form when You click on hyperlink on this picture:

datashetCallTracker.jpg


Can this code be recoded without using the code context object (is there other way of capturing on which record I clicked in the datasheet form and then open the new form filtered with that record)?

Can someone write simplified version of this code (if such can be written)?

Thank You
 
[SOLVED] Please explain this code

I think I've solved the problem, everything works, for now :)
Here's my code:
Code:
Private Sub intZahtevID_Click()

On Error GoTo Obrada_Greske

Dim objekat As Object

Set objekat = Screen.ActiveDatasheet.ActiveControl

DoCmd.OpenForm "frmZahtev", acNormal, "", "[tblZahtev].[intZahtevID] = " & Nz(objekat, 0), acFormEdit, acDialog

If (Not IsNull(Me.intZahtevID)) Then
    TempVars.Add "TrenutniID", "[intZahtevID]"
End If

If IsNull(Me.intZahtevID) Then
    TempVars.Add "TrenutniID", "Nz(Dmax(""[intZahtevID]"",[Form].[Recordsource]),0)"
End If

DoCmd.Requery ""
DoCmd.SearchForRecord , "", acFirst, "[intZahtevID]=" & TempVars!CurrentID
TempVars.Remove "CurrentID"

Izadji_Ovde:
Exit Sub

Obrada_Greske:
MsgBox Err.Number & " - " & Err.Description, vbInformation, "Greska je!"

Resume Izadji_Ovde

End Sub
I had a problem with CodeContextObject from Uncle Gizmos Error example, I received error that object was missing, but I fix that with Screen.ActiveDatasheet.ActiveControl.

Thanks again
 
Thanks for answers Uncle Gizmo, I made this code works. The TempVars where not crucial for this code to work, more important for me here was how to capture the value of the PK field when I click on it in order to filter the opening form correctly.
Thanks again
 

Users who are viewing this thread

Back
Top Bottom