Function coding and calling

lilminx

Registered User.
Local time
Today, 09:04
Joined
Feb 3, 2009
Messages
10
Hi there,

I need some help setting up a few functions that can be called by any form to display each of the dialog forms in my database. I have included some examples of the current code below.

Currently I use the full code for each instance it is needed via event procedures (the code is used about 6-8 times on each subform, basically if you click on any area of the subform it brings up that specific record). This is frustrating as each time I change my coding I have to change it in numerous places. My basic understanding would be that I need to code a function for each dialog box called.

I’m also unsure as to how I would call this type of function in a subform so any help on this would be appreciated. Currently I have a few pubic functions I have set up in a module called mod_ProcedureLibrary. I then call these via a macro using ‘RunCode’ but I have no idea how to do this for the new functions.

Code for data entry dialog form:
Private Sub CustCode_Click()

If Not Me.NewRecord Then
DoCmd.OpenForm "frm_dialog_IndividualContact", _
, _
, _
"[CustCode]='" & Me.[CustCode] & _
"' And [DelSeqCode]='" & Me.[DelSeqCode] & _
"' And [ContactDate]=#" & Me.[ContactDate] & _
"# And [ContactTime]=#" & Me.[ContactTime] & "#", _
, _
acDialog
Else
DoCmd.OpenForm "frm_dialog_IndividualContact", _
, _
, _
, _
acFormAdd, _
acDialog
End If

End Sub

Code for display dialog form:
Private Sub AS400telesalesInfo_Click()
On Error GoTo Err_AS400telesalesInfo_Click

DoCmd.OpenForm "frm_dialog_AS400telesalesInfo", acNormal, , "[CustCode]='" & Me.[CustCode] & "' And [DelSeqCode]='" & Me.[DelSeqCode] & "'", , acDialog

Exit_AS400telesalesInfo_Click:
Exit Sub

Err_AS400telesalesInfo_Click:
MsgBox Err.Description
Resume Exit_AS400telesalesInfo_Click

End Sub


I am working in Access 2003. Let me know if you require anymore information. Thank you in advance for your help.

Thanks Jodie
 
You can call any function from an event property. For example if you want a function called myCustFunction to run when the user clicks a command button, you type =myCustFuntion() as the setting of the On Click property in the Property window of that command button.
Or you can call it by code typing in your code:
Code:
call myCustFunction
 
As Jardiamj suggested, I always use a public sub/function for that sort of thing.
You could put something like this in a module.

Code:
Public Sub Display(byval sFrm as String, byval sFilter as String)

  DoCmd.OpenForm sFrm,,sFilter, , ,acDialog 

  If IsLoaded(sFrm) = True then
        DoCmd.Close acForm, sFrm
  End If

End Sub

Function IsLoaded(ByVal strFormName As String) As Integer
 ' Returns True if the specified form is open in Form view or Datasheet view.
    
    On Error Resume Next
    
    Const conObjStateClosed = 0
    Const conDesignView = 0
    
    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
        If Forms(strFormName).CurrentView <> conDesignView Then
            IsLoaded = True
        End If
    End If
    
End Function
 

Users who are viewing this thread

Back
Top Bottom