Is there an easier way to access an unbound field on a form? (1 Viewer)

hkc94501

Member
Local time
Tomorrow, 04:31
Joined
Aug 6, 2021
Messages
38
forms!myform!field works but isn't there some way to do this relative to me. that would be simpler?

I have yet to really understand what me. entails and what it does not.

Hank
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:31
Joined
Oct 29, 2018
Messages
21,474
Hi. Are you saying Me,TextboxName doesn't work?
 

Mike Krailo

Well-known member
Local time
Today, 15:31
Joined
Mar 28, 2020
Messages
1,044
If the code is in the form itself, meaning it is form module code, then you could use me.field to reference the control. If the code is in a standalone module or in a query then the form must be open and use the unambiguous forms!myform!field or forms!myform.form.someformoperation
 

hkc94501

Member
Local time
Tomorrow, 04:31
Joined
Aug 6, 2021
Messages
38
If the code is in the form itself, meaning it is form module code, then you could use me.field to reference the control. If the code is in a standalone module or in a query then the form must be open and use the unambiguous forms!myform!field or forms!myform.form.someformoperation
Thanks, I'll try that.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:31
Joined
Jul 9, 2003
Messages
16,282
Please provide more detail about what you are attempting, as there may be other solutions.

What you are attempting has probably been done before, and will have a proven, well tested method of doing it. It's difficult to understand what you are attempting from the information you have provided so far.
 

hkc94501

Member
Local time
Tomorrow, 04:31
Joined
Aug 6, 2021
Messages
38
Please provide more detail about what you are attempting, as there may be other solutions.

What you are attempting has probably been done before, and will have a proven, well tested method of doing it. It's difficult to understand what you are attempting from the information you have provided so far.
I am trying to use a group of optionbuttons to set individual bits of a mask. The buttons are not grouped in an optiongroup because I want them to operate independently rather than as radio buttons. The buttons are unbound because I want to control their appearance based on a single bit rather than the whole word. If they are all bound to the same field then setting any bit will cause them all to indicate False. Each button has an after_update event procedure that sets its bit based on the value of the button after update. What I was asking for was a simple way to refer to the unbound field of the current record of the form.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:31
Joined
Oct 29, 2018
Messages
21,474
I am trying to use a group of optionbuttons to set individual bits of a mask. The buttons are not grouped in an optiongroup because I want them to operate independently rather than as radio buttons. The buttons are unbound because I want to control their appearance based on a single bit rather than the whole word. If they are all bound to the same field then setting any bit will cause them all to indicate False. Each button has an after_update event procedure that sets its bit based on the value of the button after update. What I was asking for was a simple way to refer to the unbound field of the current record of the form.
And like I asked earlier, which I don't understand your response, are you saying using Me.OptionButtonName didn't work?

It doesn't matter if the control is bound or unbound, Me.ControlName should work.
 

hkc94501

Member
Local time
Tomorrow, 04:31
Joined
Aug 6, 2021
Messages
38
No that works for getting the state of the buttonAnd like I asked earlier, which I don't understand your response, are you saying using Me.OptionButtonName didn't work?
It doesn't matter if the control is bound or unbound, Me.ControlName should work.
That works for getting the state of the button. My question was how to access an unbound field to assign a value. My real problem is that I don't really understand what the context me. means.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:31
Joined
Oct 29, 2018
Messages
21,474
No that works for getting the state of the buttonAnd like I asked earlier, which I don't understand your response, are you saying using Me.OptionButtonName didn't work?

That works for getting the state of the button. My question was how to access an unbound field to assign a value. My real problem is that I don't really understand what the context me. means.
Hi. To assign a value to an unbound control, you simply use something like Me.UnboundControlName=SomeValueYouWantToAssignToIt

The context of Me is the "current" object. For example, if you're trying to write some code behind a Form, then Me refers to that form. If you were writing code behind a report, then Me refers to that report. Remember, Me has to be an object.

Now, you can't use Me everywhere, so maybe that's why you're having some problems using it.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:31
Joined
Feb 28, 2001
Messages
27,188
I have yet to really understand what me. entails and what it does not.

OK, I'll address that question. My colleagues are approaching it from the direct problem I don't need to overlap their work.

In a subroutine or function, whether it is in a GENERAL module that stands apart from other structures, or in a CLASS module that is directly related to some structure, you write code that usually must reference something external to the routine.

Anything you declared with a DIM statement inside the routine is obviously available to be used in code. That is closest in scope to the code. ("In scope" just means visible/usable in expressions in your code.) Anything in the declaration area at the top of the module is available to any routine in that module and is next-closest in scope. Anything declared public in the declaration area of a general module is available and is farther out in scope. These things can be directly referenced by their name alone, without a qualifier. Once you exhaust those possibilities, VBA has a few more ways for you to reference some type of data that is also in scope, just less obvious.

The most common method after the above list of direct references is to declare or identify an object that has properties and methods associated with it. I.e. the object brings baggage to the table. Then you can use the name of the object to QUALIFY (which in this context simply means SPECIFY) where to find those methods and properties. If you see a phrase such as "qualified reference" it means the reference includes not only the name of the referenced thing but its location. When the object has a class module associated with it, this is where Me comes in.

The keyword Me refers to the object to which the class module is attached. On a form called Fred, it takes the place of Forms("Fred") syntax. It is a SHORTHAND way of saying "in the form to which the class module is attached." So if you have a textbox named FredBox on that form, then the code in the class module can reference Me.FredBox rather than using the longer and more formal syntax of Forms("Fred").FredBox to get to that box..

Now, the other part... when is Me NOT available? Well, to understand how it works, you need to know that Me is a COMPILATION short-cut. If you think about it, what it really means is that it is telling the VBA compiler that "the thing that follows Me is associated with the object nearest to the code module" - which is either a form or a report.

If you happen to be running code in that class module and you call a routine in a general module (external to the class module), the closest thing to the general module isn't a form or report because general modules are stand-alone objects. Therefore, in subroutines outside of the associated class module, Me has nothing to reference. In essence, the compiler gets lost trying to find Me.XXX because it can't find Me because there is nothing close enough.

Hope this helps you to understand when and when not to use Me in a reference.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:31
Joined
Oct 29, 2018
Messages
21,474
Hi. In case this helps... If you are familiar with other programming languages, Me in VBA is like using This in some other languages.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:31
Joined
May 21, 2018
Messages
8,529
My question was how to access an unbound field to assign a value. My real problem is that I don't really understand what the context me. means.
If I understand correctly, you want to set the value of a field in the recordsource, but that field is not bound to any control.
I think your question confused people because we routinely talk about controls that are unbound (control without a controlsource), but not fields being unbound (a field in the recordsource not associated to any control).

How to reference the "unbound field" depends if you added the recordsource when you built the form. If when you built the form the recordsource was assigned then the Field is immediately assigned as a Property to the form. So you can simply use
Me.YourFieldName = Somevalue
In fact since it is a property you do not even need Me and can simply use its name
YourFieldName = Somevalue

However, if you assign the recordsource dynamically the field is not added as a property. This is not very common. In that case you will need to use the bang operator
Me!YourFieldName = Somevalue

There is a long reason for this but the ! operator does an assignment at runtime.

If you really want a detail clear discussion on Me. and Me! see here
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:31
Joined
May 21, 2018
Messages
8,529
This demo may help. In this demo the field Dept is not assigned to any control. You can only add it in code. Since Dept is part of my recordsource of the form it is a property of the form. So I can simply do
Dept = SomeValue
or
Me.Dept = SomeValue

If I would add the recordsource dynamically at runtime I would have to use
Me!Dept = SomeValue
This is a rare case, when you have to use ! in writing code. It is the same reason that you have to use ! in queries and other expressions.
Demo.jpg


If you scroll through the main form you can change the Dept and you can view the changes in the subform.

Here is the code
Code:
Private Sub cmdAssign_Click()
  'Dept is not assigned to a control but it is in recordsource. Only can change it in this code
  'Can use Me.Dept to make it easier to understand and leverage intellisense
  Dept = InputBox("Type in a value for the Department number", "Department Number")
  'The below is only needed for the demo to show the changes immediately in the subform
  Me.Dirty = False
  Me.subFrmDemo.Requery
End Sub
 

Attachments

  • UnassignedControl.accdb
    484 KB · Views: 66

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:31
Joined
May 21, 2018
Messages
8,529
If anyone is interested here is the rare case where you have to use the late binding "!".
I load the recordsource in the on load event. Since the form was built without a recordsource no Fields are properties of the form.
Me.Dept will not compile on the form since it is not a property of the form and must use me! . You can test this yourself by trying to change the ! to "." and then save the form. It will give you a compile error.

Code:
Private Sub Form_Load()
  MsgBox "This form was built without a Recordsource. It is assigning it on load.  No fields are property of the form.  Must use late binding me!"
  Me.RecordSource = "Table1"
End Sub

Private Sub cmdAssign2_Click()
  'This will fail since no recordsource at design time
  On Error GoTo Errlbl
  'Me.Dept will not even compile
  Me!Dept = InputBox("Type in a value for the Department number", "Department Number")
  'The below is only needed for the demo to show the changes immediately in the subform
  Me.Dirty = False
  Me.subFrmDemo.Requery
  Exit Sub
Errlbl:
  MsgBox Err.Number & Err.Description
End Sub
 

Attachments

  • UnassignedControlRuntime.accdb
    480 KB · Views: 73
Last edited:

hkc94501

Member
Local time
Tomorrow, 04:31
Joined
Aug 6, 2021
Messages
38
This is curious. When is me. computed and is it ever re-computed?
The question arises because I find that if I add a field to my table after the VB module has been created the VB compiler doesn't seem to notice that there is a new field.
In the attached file the one table has two fields cia and mask. Mask was added after the VB module was created and when I try to access me.mask in my VB it fails and generates a compiler error "Method or data member not found"

Is there some way to force the compiler to re-evaluate me.?
 

Attachments

  • BIttwidler.zip
    34.6 KB · Views: 74

hkc94501

Member
Local time
Tomorrow, 04:31
Joined
Aug 6, 2021
Messages
38
This is curious. When is me. computed and is it ever re-computed?
The question arises because I find that if I add a field to my table after the VB module has been created the VB compiler doesn't seem to notice that there is a new field.
In the attached file the one table has two fields cia and mask. Mask was added after the VB module was created and when I try to access me.mask in my VB it fails and generates a compiler error "Method or data member not found"

Is there some way to force the compiler to re-evaluate me.?
Late binding of me! does work in this case. It does resolve the new field. However, it would be much nicer if there was a way to force the compiler to re-evaluate the bindings of me.
Decompiling didn't help.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:31
Joined
May 21, 2018
Messages
8,529
However, it would be much nicer if there was a way to force the compiler to re-evaluate the bindings of me. .... Is there some way to force the compiler to re-evaluate me.?
Your interpretation and terminology are incorrect. It is not failing to reevaluate the bindings. There is no property yet in the class, you need to do add the Field as a Property to the class. As previously stated, this is done when you add the recordsource to the form. If you create the form with a recordsource and then add a field to your table, you would need to go back to the recordsource of the form and "reselect" it. This will cause any fields that have not been added as a property to get added. Compiling has nothing to do here. ONE MORE TIME. Fields are added as properties to the form when you add a recordsource. This occurs then and only then.



You can demo. In yours.
type ME. and see if intellisense brings up Mask and it will not.
Clear out the recordsource and reassingn it to Bits which will ensure all fields are added as a property
type Me. and see if it shows in intellisense. It will.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:31
Joined
Oct 29, 2018
Messages
21,474
However, it would be much nicer if there was a way to force the compiler to re-evaluate the bindings of me.
I guess, in a way, @MajP gave you the answer. You could try reassigning the record source of your form when it opens. Maybe that will work.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:31
Joined
May 21, 2018
Messages
8,529
You could try reassigning the record source of your form when it opens. Maybe that will work.
@theDBguy and @hkc94501
NO you can not do that. Please see thread 15.

One more time
Fields are added as PROPERTIES to the Form Class ONLY when you add a recordsource at DESIGN time.
I show that if you reassign the recordsource at RUN TIME the FIELDS are not added as PROPERTIES to the Forms Class. (this behavior should be expected) You must use late binding with the ! notation.

In DESIGN TIME if you add a field to a table since you last assigned the recordsource, you must reselect the recordsource. It is that action that creates the properties.

See thread 13 for the long explanation of this explained by SONIC8
 

Users who are viewing this thread

Top Bottom