How do I reference the current form in an expression in Microsoft Access?

两盒软妹~` 提交于 2019-12-30 08:28:28

问题


I'm no Access expert, and I have an (I hope!) simple question...

I have a form with a number of records. In some textboxes I just present values from the underlying table - so they are bound to the corresponding fields.

But some textboxes should contain calculated values. Some calculations are complicated and involves many fields from the table. I write the calculation as a VBA function. I could enter something like this as "Control Source":

=MyFunction([Field1], [Field2], [Field3] ...)

But I don't want to list dozens of fields in the function call. Instead, I want to send the whole form (or the current record) as a parameter, and let the function reference the fields it needs. I can do it like this:

=MyFunction([Forms]![MyForm])

But I don't like having to name the form in the call. Isn't there a way to send the "current form" as a function argument? In VBA, you just use the "Me" keyword, for exampel "Me![Field1]". But it seems like "Me" isn't accepted in an expression.

Is there some other way to reference the current form in an expression?

(It's a cosmetic question, I know. But it's not good programming to use "[Form]![MyForm]". Later on you copy the controls to another form and forget to change the name in the expression...)

Grateful for your help! :-)

/Anders


回答1:


You can use:

=MyFunction([Form])

The code would be:

Function MyFunction(frm As Form)
MsgBox frm.Name
End Function



回答2:


'me' can only be called from within the corresponding form object (ie in the object's procedures, functions and events).

My favorite way to refer to the current form is to call the screen.activeForm object...

screen.activeForm.recordset.fields(myFieldname).value
screen.activeForm.controls(myControl).value
screen.activeForm.name
....

Of course you can send the form object to a custom function

my Result = myCustomFunction(screen.activeForm)

Or you could build a user-defined object. The needed fields could then be considered as internal properties, set in the Class_Initialize sub of the corresponding object.




回答3:


If you are using code in the form, then "me" referrers to the current form. So

Call SomeFunction(me)

However, "me" can't be used in the context of a expression that is bound to a text box.

In that case you can either pick up the current screen in the routine with screen.activeform as suggested.

I often go:

Dim f     as form
Set f = screen.ActiveForm



回答4:


Screen.ActiveControl.Parent
' or
someControlVariable.Parent

I use this if I want the current subform (or form if not currently in a subform) because using Screen.ActiveForm does not provide the current subform, but only the form containing that subform. Do be aware of your context, if the control is within a tab control then its parent is that tab control and not the form.




回答5:


In ACC2013 I couldn't use fionnuala's answer. So I changed to:

Event handler in Property-Window:

=MyFunction()

My code would be:

Function MyFunction()
  Dim frm As Form   
  Set frm = Screen.ActiveForm
  MsgBox frm.Name
End Function


来源:https://stackoverflow.com/questions/8787979/how-do-i-reference-the-current-form-in-an-expression-in-microsoft-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!