Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in setting a constant reference to form names 1

Status
Not open for further replies.

inso18

Technical User
Dec 30, 2006
147
IL
Hi Everyone.

In my access app VBA code, I very often use the same form names for reference in fuctions or methods, here's example from my code, in public Module:

Code:
Function fColorAmount()
    If flgFrmMainIsLoaded Then
        If Not IsNull([b]Forms!frmMain!sfrmPanelDate.Form!sfrmPanelColor.Form.ctlColorAmount[/b]) Then
            fColorAmount = [b]Forms!frmMain!sfrmPanelDate.Form!sfrmPanelColor.Form.ctlColorAmount[/b]
        Else
            fColorAmount = "*"
        End If
    Else
        fColorAmount = "*"
    End If
End Function


Function fHighLights()
    If flgFrmMainIsLoaded Then
        If Not IsNull([b]Forms!frmMain!sfrmPanelDate.Form!sfrmPanelColor.Form.ctlHighLights[/b]) Then
            fHighLights = [b]Forms!frmMain!sfrmPanelDate.Form!sfrmPanelColor.Form.ctlHighLights[/b]
        Else
            fHighLights = "*"
        End If
    Else
        fHighLights = "*"
    End If
End Function

I use the form reference "Forms!frmMain!sfrmPanelDate.Form" (or any other reference) many times in the app code, and if I decide to change the forms/subform structure, I need to change the reference each instance it's used.

Isn't there somehow, maybe through class module, where the reference name would be created, to only have to change the form name reference once to change the reference for all places it used?

Thanks,
inso18
 
Perhaps you could declare the form name once in a global declaration: public frmForm1 as <whatever the right type is>
Then you can have an initialization procedure to set that value (once).

_________________
Bob Rashkin
 
If "Forms!frmMain!sfrmPanelDate.Form" is a Variable you should pass it to your Functions as a Parameter.
 
Bong, it doesn't work.
HughLerwill, my functions need to have this variable availible. How does a function can read it then?
 
it doesn't work" is a meaningless term. WHAT doesn't work?

How does the function read them? By doing exactly as Bong and Hugh suggested. Declare the object, and pass it as a parameter to the Function.

Further...as you posted this exact same question previously - May 24th - AND you had solid suggestions then...I suggest you go back and read your original post. The answer was given then.

faq219-2884

Gerry
My paintings and sculpture
 
This question could probably have been posted in one of the Access fora on the site.

[tt]Function fColorAmount(frm as Access.Form)
If flgFrmMainIsLoaded Then
If Not IsNull(frm.ctlColorAmount) Then
fColorAmount = frm.ctlColorAmount
Else
fColorAmount = "*"
End If
Else
fColorAmount = "*"
End If
End Function[/tt]

Call it with

[tt]Call fColorAmount(Forms!frmMain!sfrmPanelDate!sfrmPanelColor.Form)[/tt]

or

[tt]Call fColorAmount(Me!sfrmPanelColor.Form)[/tt]

if it's from the current form.

Rewriting all subs/functions to receive the either form name, or a form object, makes it a bit more flexible, though you could probably also pass either control name, or the control to check/alter also to make it even more dynamic.

Any reason you're not using a sub? You're not returning any value.

Roy-Vidar
 
RoyVidar, thanks for the clear explanation!
now I understand I need to use Access.Form type to pass it through.

I'm using a function and not a sub so I could use it in queries.
 
Hmm - does that work from a query? I'd think you'd need to pass the name of both the main form, and the subform.

[tt]Function fColorAmount(MainFormName as String, SubFormName As String)
If flgFrmMainIsLoaded Then
If Not IsNull(Forms(MainFormName)(SubFormName)!ctlColorAmount) Then
fColorAmount = Forms(MainFormName)(SubFormName)!ctlColorAmount
Else
fColorAmount = "*"
End If
Else
fColorAmount = "*"
End If
End Function[/tt]

TheField: fColorAmount("frmMain", "sfrmPanelDate")

Roy-Vidar
 
Roy-Vidar,

The function you wrote works as a function that gets a parameter and uses it:

Code:
Function fEventDate(frm As Access.Form)
    fEventDate = frm.EventDate
End Function

So I in a query and form I can use it in the following way:
Code:
fEventDate(Forms!frmMain!sfrmPanelDate.Form)

And get the correct value

Additionally, I was interested in setting a form location as a constant, so I would need to change it only one time, if I change the form name.

so now I can do it in the following way:

Code:
Function fLocSfrmDyeingDate() As Access.Form
    Set fLocSfrmDyeingDate= Forms!frmMain!sfrmPanelDate.Form
End Function

Function fEventDate(frm As Access.Form)
    fEventDate = frm.DyeingDate
End Function

And ?fEventDate(fLocSfrmDyeingDate) returns the correct value

Thanks
 
Fumei,

You are right.
As an non-programmer I didn't understand both Bong's and HughLerwill's suggestions as a solution, so In case of Bong, I've tried his global declaration and one-time initialization suggestion, but it wasn't enough for me (as a non-programmer) to create something that works

I've opened a new thread because the old one didn't produced a solid solution, but some vague suggestions, specialy for an non-programmer like myself. (unlike this thread, where in the end there's a clear and working solution)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top