Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208

    Refer to control from inside With block


    I sometimes have a construct like
    Code:
    With Form_A.LabelB
        .BackColor = vbRed
        .ForeColor = vbGreen
        .Caption = "Rumplestiltskin"
        SomeOtherSubThatDoesSomethingImportantHere Form_A.LabelB
        .BorderWidth = 4
        .BorderColor = vbBlack
    End With
    The highlighted line is what I would like to clean up. Is there any way from INSIDE a With block to refer back to the object on the With line, without naming the entire business again? Something like ME. refers back to the parent form, without having to name it explicitly.

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you can use the path that doesnt start with a period:
    me.txtBox= "abc"

  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You can set a control variable and refer to that , but I doubt it saves much typing as you would have to set it as well?

    Code:
    
    Private Sub Command3_Click()
    
    
      Dim ctl As Control
      
      Set ctl = Me.Label11
      
      With ctl
        .BackColor = vbRed
        .ForeColor = vbGreen
        .Caption = "Rumplestiltskin"
        SomeOtherSubThatDoesSomethingImportantHere ctl
        .BorderWidth = 4
        .BorderColor = vbBlack
    End With
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by ranman256 View Post
    you can use the path that doesnt start with a period:
    me.txtBox= "abc"
    Yes, I can, but I don't WANT to. The whole point of my question is whether there is some syntax or property that will allow me to refer back to the enclosing object WITHOUT naming it again.

  5. #5
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by Minty View Post
    You can set a control variable and refer to that , but I doubt it saves much typing as you would have to set it as well?

    Code:
    
    Private Sub Command3_Click()
    
    
      Dim ctl As Control
      
      Set ctl = Me.Label11
      
      With ctl
        .BackColor = vbRed
        .ForeColor = vbGreen
        .Caption = "Rumplestiltskin"
        SomeOtherSubThatDoesSomethingImportantHere ctl
        .BorderWidth = 4
        .BorderColor = vbBlack
    End With
    I can do that, and I do, sometimes. But I'm wondering if there is some way to do what I asked - refer back implicitly to the enclosing object of the With block.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Yes you can although I have always avoided doing so. Such things are not relevant to the object to which the With applies so I don't think they belong there.
    EDIT - I'm answering based on the post title and your first post. I have no idea what this means
    refer back implicitly to the enclosing object of the With block.
    Maybe you'll get better and more focused responses by avoiding pseudo code and being specific.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by Micron View Post
    Yes you can although I have always avoided doing so. Such things are not relevant to the object to which the With applies so I don't think they belong there.
    Wonderful. And would you be willing to tell me how that is done? That was the point of my question.

    EDIT - I'm answering based on the post title and your first post. I have no idea what this means "refer back implicitly to the enclosing object of the With block."
    Sorry, I don't know how to make it any clearer, and in your first sentence, you say that what I want can be done, although you didn't explain how. But I'll try again: inside the With block, using the leading dot syntax, you can refer to all various properties and methods, implicitly of the object following the With keyword - in my example, a label on a form. I would like to be able to refer back to that object again, from inside the With block, in order to say pass it to a subroutine, WITHOUT having to explicity name it, since the naming can be multiple levels of dot syntax deep.

    Maybe you'll get better and more focused responses by avoiding pseudo code and being specific.
    There is no pseudo-code here, except for the name of the sub inside the With block. All else is completely real, and I don't know how to be more specific than this. I explained exactly what I am doing and what I would like to accomplish. What part of my post is not clear to you?

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    This is pseudo code IMO
    SomeOtherSubThatDoesSomethingImportantHere Form_A.LabelB
    And how would you refer to any thing implicitly when the with block (and most other control references) are explicit? I don't think I even know what an implied reference with respect to a control or a procedure call would look like. I didn't post a specific example because I didn't understand what you're asking for, and unfortunately I still don't.

    If you're asking if you can precede a procedure call inside of a with block by starting off with a dot, then I'd say no, unless maybe the function is a custom method of whatever the with block is for, and I'd say that object would have to be a custom object as well. What I meant was you can do this
    Code:
    With rs
        .AddNew
        'do rs stuff
    
        CallFromWithBlock
        .Update
        On Error GoTo 0
    End With
    but you can't precede the function call with a dot as it's not a member, property or method of the rs.
    Last edited by Micron; 04-28-2021 at 12:26 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    @OP - I'm sort of with Micron here, can you give a specific example that you have actually come across?

    I can see a few possibilities in Excel when referring to ranges etc, but nothing very obvious in Access?
    I can't think of a method of an control (or any other object) that does refer to itself?

    According to the great Google in the Sky the short answer is no.
    This sort of achieves it in VB https://stackoverflow.com/questions/...-with-end-with
    but again I'm not sure where you would use this.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  10. #10
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by Micron View Post
    This is pseudo code IMO
    Yes, and if you read my entire response, you would note that I wrote "There is no pseudo-code here, except for the name of the sub inside the With block. That one name I put in to make it clear what is happening. Would you feel more enlightened if I had included an actual name from some of my code? The line
    Code:
    gbl_ZapniBarvyAPozadi Form_Akces.lblPodrobnostiHledat
    is a lot less clear, in my opinion.

    And how would you refer to any thing implicitly when the with block (and most other control references) are explicit?
    The initial object reference immediately following the With keyword is explicit. The references to the properties and methods inside the With block are explicit. The continuing reference to the outer object is implicit - you don't state it again, you simply put in the notation of a leading dot, implicitly referencing the 'surrounding' object, which is carried into the With block. That's the whole point of a With block - you name the object ONCE, when initializing the block, then subsequent bits of code using the leading dot notation implicitly refer back to that same initial object. I have trouble believing that you really don't understand this - it seems such an obvious and trivial point to me.

    I don't think I even know what an implied reference with respect to a control or a procedure call would look like.
    Neither do I - that's why I'm asking. I can think of several ways to do it. Probably the most obvious would be .Me Such a reference inside a With block would be a reference back to the object that started the With block. However, I don't see anything like that in the properties, and have found nothing on the net in that direction, hence my question.

    I didn't post a specific example because I didn't understand what you're asking for, and unfortunately I still don't.
    In your first response, you wrote "Yes you can although I have always avoided doing so." What then do you mean by "Yes you can", if you don't even know what I'm asking? Sorry if I sound brusque, but you're not making a lot of sense.

    If you're asking if you can precede a procedure call inside of a with block by starting off with a dot,
    No, no, no. Here, I'll try once more. Actual pseudo-code, this time:
    Code:
    With FormA.LabelB
        .Caption = "Fred"
        SetColor .Me
    End With
    
    elsewhere...
    
    Sub SetColor(ctl as Control)
        ctl.backColor = vbRed
    End Sub
    Inside the With block, I want to call something and pass that something a reference to the object inside whose With block the code is currently executing. As is, I only know how to do it this way:
    Code:
    SetColor FormA.LabelB
    again EXPLICITLY naming the same object that initialized the With block. I don't want to have to do that. I want a reference to the control inside whose With block I am currently running. The .Me construct seems an obvious one, just like the Me keyword refers to the form from which it is executed, without having to explicitly name the form. If you change the name of a form, and you have named references to the form in the form's code, you have to change all those as well. If you use ME, you don't need to change anything.

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Yes, and if you read my entire response,
    I don't like your tone and insinuations. Good luck, I'm out - for good.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by Minty View Post
    @OP - I'm sort of with Micron here, can you give a specific example that you have actually come across?
    No, because I have not 'come across' any examples. I have written such code, exactly the sort of thing I initially posted.
    I can see a few possibilities in Excel when referring to ranges etc, but nothing very obvious in Access?
    I do much more work in Access than Excel, but this is not object-specific or application-specific. The With construct is pure VBA, and situations where you might want to do this can arise anywhere.
    I can't think of a method of an control (or any other object) that does refer to itself?
    Me neither, which is why I was asking. It would be trivial to include such a property if I was writing my own class and needed it there, but I generally want such things when I am dealing with native stuff, like form controls. I guess each such construct would have to have such a property specifically written into it, and probably nobody thought it was important enough to do so.

    According to the great Google in the Sky the short answer is no.
    This sort of achieves it in VB https://stackoverflow.com/questions/...-with-end-with
    but again I'm not sure where you would use this.
    Yes, that discussion is exactly what I meant. I could give you numerous examples where I would like to use such a property, but I don't think it would be of any use. Bottom line seems to be that I'm just out of luck.

  13. #13
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by Micron View Post
    I don't like your tone and insinuations. Good luck, I'm out - for good.
    Well, when you clearly ignore what I've written, or tell me I can do what I asked without saying how, then later admit you don't understand what I asked, you have to expect some pushback. But no great loss - you didn't have anything useful to offer anyway. Better luck next time.

  14. #14
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,412
    This was as close as I could get. It will get a red fred.

    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub cmdAction_Click()
    With LabelB
        .Caption = "Fred"
        Call SetColor(Me.LabelB)
    End With
    End Sub
    
    
    Function SetColor(ctl As Control)
        ctl.BackColor = vbRed
    End Function

  15. #15
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @pdanes,
    Wow! Really? Someone tries to help you and you respond like that?
    Your descriptions and explanations weren't clear to me either...... so I didn't respond.
    But it doesn't matter now - I've added you to my ignore list. Good luck with the other forums...

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. New kid on the block
    By ldodge429 in forum Access
    Replies: 5
    Last Post: 07-17-2014, 08:45 PM
  2. Is Control inside Subform
    By CementCarver in forum Programming
    Replies: 3
    Last Post: 06-28-2013, 01:31 PM
  3. Replies: 1
    Last Post: 09-05-2012, 07:04 AM
  4. Replies: 5
    Last Post: 08-03-2012, 04:20 PM
  5. End While block problem
    By cbh35711 in forum Programming
    Replies: 4
    Last Post: 05-07-2012, 03:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums