Help with Eval Function

evanscamman

Registered User.
Local time
Yesterday, 22:25
Joined
Feb 25, 2007
Messages
274
I need to set a control to a given field on a form.
But, the form could either be a form, or a subform.

Is it possible to set a control using the eval function as follows:

Code:
Dim ctl as Control
Dim strForm as string
dim strControl as string
 
strForm = "frmItem!sfrmItemSupplier.Form"
strControl = "UnitPrice"
 
set ctl = eval("Forms!" & strForm & "!" & strControl)

If this can't work, is there another command similar to eval that lets you execute a line of code from a string?
I know I could use Forms(strForm).Controls(strControl) - but this doesn't work for a subform.

Thank you,
Evan
 
You can ref a control (or form) using the following:

forms!myFormName("MyControlName")

Hence you can do something like

Dim strMyControlName as String
strMyControlName = "MyRealControlName"
forms!myFormName(strMyControlName)

So I would think you could do something like:

Dim ctl as Control
Dim strForm as string
dim strControl as string

strForm = "frmItem!sfrmItemSupplier.Form"
strControl = "UnitPrice"

set ctl = forms(strForm)(strControl)

I hope I have the syntax correct and hopefully this will give you nudge in the right direction - :)
 
Thanks for the reply.

Your suggestion works good when strForm is "frmItem".

But when it includes the subform name "frmItem!sfrmItemSupplier.Form", then I get the "form not found" error.
That's why I was trying to use eval or something like it.

Evan
 
Yeah, I jumped the gun a little didn't I. I didn't notice you were working with a sub form.

Q. Will you always be dealing with a control on a subform?
 
I suppose you could write a function that would look for the occurance of the '.' in the string and parse it out from there...
 
Just curious - What are you doing with the control after you have it defined?
 
Reading it again, and realized that I didn't understand your question first time.

I'm not sure what you are trying to do with this, but wouldn't it be easier to just call this from the control you need or the form/subform's event.

For example:

Code:
Public Function foo(bar As Control)

....

End Function

Which can be called from the subform's event.. let's say BeforeUpdate:

Code:
Private Sub Before_Update(Cancel As Integer)

Call foo(Me.MyTextBoxIWantToPassToTheFunction)

End Sub
 
I'm trying to set up a FieldValidation routine that I can call from any form.
When the user clicks, for example, "Proccess Order", the Validation function will run. It opens a query that contains a list of fields and required criteria that must be met before it allows the proccess to continue. If this is not met, it highlights the offending field(s) and alerts the user. I don't like to use Validation via Access, because I hate the annoying popup messages it gives - and the messages are not user-friendly. Plus, just by adding a new entry to the table, I can require additional validation without more coding.

This was working fine, excetp when I tried to add a field that is in a subform - then there are problems. I'm going to do as was suggested, and use the "!" to split the strForm apart.


So, am i correct to conclude that there is not an "eval" type function that runs a line of code?

Evan
 
I'm trying to set up a FieldValidation routine that I can call from any form.
When the user clicks, for example, "Proccess Order", the Validation function will run. It opens a query that contains a list of fields and required criteria that must be met before it allows the proccess to continue. If this is not met, it highlights the offending field(s) and alerts the user. I don't like to use Validation via Access, because I hate the annoying popup messages it gives - and the messages are not user-friendly. Plus, just by adding a new entry to the table, I can require additional validation without more coding.

So if the routine only is supposed to work on the current form and all the subforms that may be nested to X level deep, then you need a recursive function:

Code:
Private Sub Validate(frm As Form) 'Normally, you would pass the top-level form as the argument)

Dim ctl As Control

For each ctl in frm  'Or should that be For each ctl in frm.Controls?
    If ctl.ControlType = acSubform Then 'NOTE: This is off the top of my head, verify the syntax for ControlType!!
        Call Validate(ctl.Form) 'Recur the function, passing the subform as an argument
    Else
       'Do your validation thing here
    End If
Next ctl

BTW, In case you don't know, you can go through controls faster if you tagged them to tell you which controls needs to be validated. Look up the Tag property.

So, am i correct to conclude that there is not an "eval" type function that runs a line of code?

I believe it does, yes, but it's buggy and almost unnecessary, and worse of all, it fucks up the error handling.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom