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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing an unopened Form With a String reference

Status
Not open for further replies.

txgeekgirl1

Programmer
Sep 10, 2009
85
US
I have a string that contains the name of a form, something like “Update Requests 1”. It has to start as a string because the digit at the end will change based on user interaction. The digit could be up to 99 so a giant case statement is unmanageable.

I need to populate a variable of data type “form” with a reference to this form so that I can feed it to a function that requires a parameter of type “form”. The function call is something like

Code:

Code:
Dummy = Fixform(myformname)    
 
    ' Where the function definition is
      Function Fixform(pform as form) as boolean

The best that I can do so far is get the form as an “AccessObject” data type with this:


Code:

Code:
Dim obj as AccessObject
                Set obj = CurrentProject.AllForms(mystringvar)

At that point I can display the “name” property of the object, so I know that the string is populated properly. How do I use the string to create a variable of “form” data type?

This works (because the form is open:


Code:
Code:
Dim myform as form
Myform = me.form
Dummy = Fixform(myform)

I need this to work:


Code:

Code:
Dim myString as string
Dim mynum as integer
 
Mynum = 1
Mystring = “Update Requests “ + TRIM(STR(mynum))
Dummy = Fixform(mystring)


Tried to set the string to the form reference and got an error that it couldn't reference a form that didn't exist - even though the form does exist using this:


Code:
Code:
Set myform = Forms(mystring).form
Thanks for the help.
 
I think you can only accomplish what you want if the form is open.

I hate to ask but what is the context for this and why would you have numbered forms? Seems a bit un-normalized from my perspective.

Duane
Hook'D on Access
MS Access MVP
 
I need to pass the form name to my function as a form in a string context.

Although it is not normal - because we experiment a lot with pushing Access to it's limits - we are building master forms on the fly and the values could be 1-99 so MyNum can be any of these. Function FixForm expects a form as it's passed variable.

Code:
Mystring = “Update Requests “ + TRIM(STR(mynum))
Dummy = Fixform(mystring)

Here’s another example of something that almost works:

Code:
Dim theform as Form
Dim frm As AccessObject
Set frm = CurrentProject.AllForms(myForm)
MsgBox frm.Name
Set theform = frm
dummy = FixForm(myform, Me.Flowchart)

I shows the message box with the correct form name in it, but bombs on the 5th line. I think this is because I’m trying to do an implicit type change by assigning from an AccessObject type of variable to a Form type of variable.
 
No it does not almost work. Not even close. An access object and a form are two entirely different classes and you can not cast them. An access form class exists, but and access form does not exist until instantiated. So the answer is this cannot be done with a closed form. Again why? Seems like a sloppy way to "push access to its limits". How about multiple form instances of the same form?
 
So let's assume I open the form before making the call to the function to populate the Function with the form name as a string but needing to be a form, how can I then get the form as the variable into that function.
 
Normally you would pass by name to a function. If the function has to modify design time properties then the function opens the form hidden and in design view. Then saves the form. Then opens the view.

If it was me and I need lots of similar form. I create an instance of the template class. Add the instance to my custom template class collection. Modify that instance as needed. However still limited to run time modifications, so not able to provide unique controls for each instance.
 
if the form is open then use the forms collection
Dummy = Fixform(forms(myformname))

Unlike MSFORMS or VB the forms collection in access only contains instantiated "open" forms
 
a couple of helper functions
Code:
Public Function FormExists(strForm As String) As Boolean
  Dim frm As AccessObject
  For Each frm In CurrentProject.AllForms
    If strForm = frm.Name Then
      FormExists = True
      Exit Function
    End If
  Next frm
End Function
Public Function getForm(strForm As String) As Access.Form
  'Verify that the name is valid first using FormExists
  If Not CurrentProject.AllForms(strForm).IsLoaded Then
    'If not yet open then open it
    DoCmd.OpenForm strForm ' May want it hidden and/or design
  End If
  Set getForm = Forms(strForm)
End Function

Public Sub TestIt()
  Dim strForm As String
  Dim frm As Access.Form
  strForm = "Form1"
  If FormExists(strForm) Then
    Set frm = getForm(strForm)
    MsgBox frm.Name
  End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top