Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Posts
    44

    Unanswered: How to refer to Txt boxes and Labels in VBA access

    How do I refer to text boxes and labels in VBA code? I want to be able to use these running queries etc. Thanks for your help.

    I tried this:

    [Forms]![VendorItemRequest]![txtVendorCodeQry] = "go"
    or
    [Forms]![VendorItemRequest]![lblVendorCodeQry2] = "go"

    and the error is: "You can’t assign a value to this object"
    run time error: 2448

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    From where do you try to have this code executed?

    From a VBA module, [Forms]![VendorItemRequest]![txtVendorCodeQry] = "go" is valid, provided there is an open (as in "not closed") form named "VendorItemRequest" and that there is a control (more likely a text box) named "txtVendorCodeQry" on it.

    The brackets are not necessary if there is no space or special character in the names of both the form and the control and, in case you program includes some foreing objects (i.e. non native Access objects) that you included through a reference to an OCX or a DLL, it's better to fully qualify the reference to your objects:
    Forms!VendorItemRequest!txtVendorCodeQry.Value = "go"

    For instance, if you use a Collection (VBA native) or a Dictionary (from the Scripting Library), MyCollection.Add Mycontrol, MyKey and MyDictionary.Add MyKey, Mycontrol will add a reference to the control itself (the object) and not to its value. Use MyCollection.Add Mycontrol.Value, MyKey and MyDictionary.Add MyKey, Mycontrol.Value to add the value to the collection/dictionary.

    From the class module of the form (i.e. the module that is "behind" the form), you can simply use: Me.txtVendorCodeQry = "go" or Me.txtVendorCodeQry.Value = "go"
    Have a nice day!

  3. #3
    Join Date
    Jan 2010
    Posts
    44
    I try to have it executed from the same form, which is in the open state. Something might be wrong with my database? I go to another random database and use the same style of code and it works fine. Its a valid text field and label. Is there something I need to specify on the form? I just made the db to design the forms at first. Maybe something is wrong there? But its still saying the ' Object doesn't support this property or method '

    Any ideas what else could be wrong?

  4. #4
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    If you have an expression in the Control Source of a TEXT box (such as ="go") then you cannot assign a value to that text box (such as: Forms!MyFormName!MyTextBoxName = "don't go"). That will give you an error as you indicated (ie. "You can’t assign a value to this object") and you would need to leave the Control Source of that text box blank and populate it appropriately in code. (Note: you can also utilize the DefaultValue of the text box if need be or in the OnOpen event of the form, add your code as such: Forms!MyFormName!MyTextBoxName = "Go".)

    If the Control Source of a text box has a field name in it, then when you issue a Forms!MyFormName!MyTextBoxName = "Go", then you are changing the actual data value of that field in the table.

    For Labels, you can easily manipulate the text in that label using coding as such:
    Forms!MyFormName!MyLabelName.Caption = "Go"
    Last edited by pkstormy; 03-26-10 at 15:05.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Is the control bound, and if yes is the underlying field updatable? The form could be bound to a query that is not updatable, this will explain why the control is read-only.

    For the label case, see pkstormy's explanation.
    Have a nice day!

  6. #6
    Join Date
    Jan 2010
    Posts
    44
    Thanks. That fixed what I was trying to do. What If I wanted to refer to a Label on another form that is open?

    Form name is : Employee

    Control name is a Label and named: employeename

  7. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    That would be: Forms!Employee!employeename.caption

    Remember that the form must be open.
    Have a nice day!

  8. #8
    Join Date
    Jan 2010
    Posts
    44
    Yes! thanks a lot. You have helped a ton. I am sure you will see more post from me over the next month. Thanks again.

  9. #9
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome!
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •