How to reuse code in different forms when the only variable is the form name

vicsar

https://about.me/vicsar
Local time
Today, 09:32
Joined
Jul 25, 2012
Messages
35
Hello,

I have this code:


Code:
[FONT=Verdana][SIZE=2]  If Forms!frm_Input_Form.[Process_Name] = "Process1" And  Forms!frm_Input_Form.[Thread] = "Thread1" And  Forms!frm_Input_Form.[Affected_Parameter1] = "Time" Then
        Forms!frm_Input_Form.[CCRP_Parameter_Number] = "Parameter_Code_1"
        Forms!frm_Input_Form.cboParameter_affected.Requery
[/SIZE][/FONT]


it works fine under
frm_Input_Form but I have other forms that could use this code as they use the same criteria to determine which parameter code (number) is being affected. I know I could replace frm_Input_Form with the name on the other form but I do not want to do that as it is cumbersome I rather make a call from each form to a procedure in a module and have it update the cboParameter_affected combo box.

How can that be achieved?
How can I reuse code in different forms when the only variable is the form name? (This is driving me crazy)

P.s.: I tried using the ActiveForm property (.Name) to no avail. Following is a some sample code:


Code:
Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm
MsgBox "Current form is " & frmCurrentForm.Name



Thanks, looking forward to your reply.
 
I'd probably create a function that accepted the form name as a string variable, then:

Forms(VariableName).[Process_Name]
 
Hello Paul, thanks for your reply.

I had tried your suggestion, I was coding something along these lines (but I got a type mismatch):

Code:
    Dim frmCurrentForm As Form
    Set frmCurrentForm = Screen.ActiveForm.Name
Then I tried the following (invalid qualifier)

Code:
    Dim frmCurrentForm As Form
    Set frmCurrentForm = Screen.ActiveForm

If Forms!frmCurrentformName[COLOR=SeaGreen][I].Name.[/I][/COLOR][Process_Name] = "AP" A......
Finally I tried this (but it says it cannot find the form named frmCurrentForm )

Code:
Dim frmCurrentForm As Form
Set frmCurrentForm = Screen.ActiveForm

  If Forms!frmCurrentForm.[Process_Name] = "Process1" And  Forms!frmCurrentForm.[Thread] = "Thread1" And  

Forms!frmCurrentForm.[Affected_Parameter1] = "Time" Then
        Forms!frmCurrentForm.[CCRP_Parameter_Number] = "Parameter_Code_1"
        Forms!frmCurrentForm.cboParameter_affected.Requery
So by now you can see how this is getting to my nerves :-) Is beyond me I cannot make it work O_O


What's your suggestion? I will highly appreciate it.


I'd probably create a function that accepted the form name as a string variable, then:

Forms(VariableName).[Process_Name]
 
I have been observing this thread, however have not understood the exact source of the dilemma.

I will, however, offer some code I have on an Edit form which has smarts to be able to send an update notification back to one of two parent forms which could have opened the edit record form. Perhaps you will get some ideas from my example:

Code:
  Dim strDocName As String
  Dim frm As Access.Form

  'Call the Update event of the Validation Object
  If ObjUIValidationMEToolingTbl.Update(Me) Then
    If Me.OpenArgs = "subform_metooling_gaging" Then
      strDocName = "quality"
      'Refresh the prior window UI
      'Needed since this form was opened via its class and the
      'Forms Collection does not have the form name set as the key
      For Each frm In Forms
        If frm.Name = strDocName Then
          Call frm.Tool_Refresh_Click(ObjUIValidationMEToolingTbl.id)
          Exit For
        End If
      Next frm
    Else
      strDocName = Me.OpenArgs
      'Refresh the prior window UI
      'Needed since this form was opened via its class and the
      'Forms Collection does not have the form name set as the key
      For Each frm In Forms
        If frm.Name = strDocName Then
          Call frm.Refresh_Click(ObjUIValidationMEToolingTbl.id)
          Exit For
        End If
      Next frm
    End If

    'Close window "self"
    DoCmd.Close acForm, Me.Name
  End If
 
I had tried your suggestion, I was coding something along these lines (but I got a type mismatch):

None of those uses a string variable as I described (which is not to say it can't be done with a form variable). This worked in a brief test:

Code:
Dim strFormName As String

strFormName = Screen.ActiveForm.Name

MsgBox Forms(strFormName).Text0
 
Thanks for your input (Michael and Paul)

I just can't get this thing to work as desired. Please see attached strip down version of my database and, if possible, tell me what I have been doing wrong. Notice that the two forms should be able to use the same code, as the only thing that changes is the form name. I left a sample working code (commented) so you can get a clearer picture of what I am trying to do.

Thanks for your feedback.
 

Attachments

For starters, compare what I posted to what you have (excluding control name for clarity):

Forms(strFormName).
Forms!strFormName.

The parentheses are key to having it interpret the variable. What you have is expected to be an actual form name.
 
Paul,

all I can say is that when you are good at something there is not discussion about it. Thank you very much for staying and guiding me to fix this. It works perfectly.
:D


*-*-*-*
If anyone is interested in the solution I have added a final version of the database with the working code. ;) Enjoy.
 

Attachments

I'm sure that using the ActiveForm will work fine but I do it by passing in the form name.

I create a sub in a standard module. This example is code I use in the close event of the "close" button on each form.
Code:
Public Sub CommonClose(frm As Form)
On Error GoTo ErrProc
    If bForceClose = True Then
        Exit Sub        'do not run close code
    End If
    If IsNull(frm.OpenArgs) Then
        DoCmd.OpenForm "Switchboard"
    Else
        DoCmd.OpenForm frm.OpenArgs
    End If
ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case 2102   'bad open args form name
            Resume ExitProc
        Case Else
            MsgBox Err.Number & "--" & Err.Description
            Resume ExitProc
    End Select
End Sub
Code:
    Call CommonClose(Me)
"Me" is a reference to the open form/report object from within that form/report's class module.
 

Users who are viewing this thread

Back
Top Bottom