Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216

    Need help using variables


    I have a form which has many text fields (may actually be labels). I need to remove the contents in them. Below is the code I have tried.

    Public Function modCal_removeJob()
    Dim strCell As String
    strCell = "wd14115" ' I do have a field (label named wd14115


    Forms![frmCalendar].Form![strCell] = Empty
    End Function

    Thank for your help.

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Data is stored in tables and only displayed / edited in forms.
    Either use a delete query to remove the data (if you are sure you want it gone!) or filter the form’s record source to filter out the unwanted data
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    This is not data stored in a table. It is called a label on Access's form design. It has no data source.

  4. #4
    Minty is offline VIP
    Windows 11 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Just select the label and delete it in design view?
    Or select multiple labels and delete them all at once.

    Writing code to do this seems incredibly time consuming for a one off exercise?
    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 ↓↓

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    If it is a label, and you need to be able to tell the difference between those and other types of controls, then you need the Caption property?

    ChatGPT
    Code:
    Private Sub ClearLabels()
        Dim ctl As Control
        
        For Each ctl In Me.Controls
            If ctl.ControlType = acLabel Then
                ctl.Caption = ""
            End If
        Next ctl
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    Welshgasman
    , I tried your sub (It would not run as a sub, I had to make it a function). I get an error Invalid use of the Me keyword. See below

    Public Function ClearLabels()
    Dim ctl As Control

    For Each ctl In Me.Controls
    If ctl.ControlType = acLabel Then
    ctl.Caption = ""
    End If
    Next ctl
    End Function

    To Minty; I have a lot of these. I admit I wrote it all wrong. I have to live with it for now.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    Put in your form.

    If you are going to run it on multiple forms, then do put it in a module and pass in the form object to the sub/function.

    I only ever use functions if I want to return something, else I use subs.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows 11 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    lawdy,
    Can you step back and tell us in plain English what exactly you want to accomplish, and tell us a little about your environment? Is it the label you want removed? Or, only the caption that you may want to change/edit? Are you re-purposing a form? Or extending the application to other users?

    Label(captions) often help users to understand the intended purpose/meaning/context of the associated control. My analogy is the label on a soup can --tells you what's inside. So removing label captions may be something to reconsider or, at least, review to ensure that is the "best way" to meet your goals.

    Not trying to dissuade you, just suggesting you look at options before pulling the trigger.

  9. #9
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    Ok Orange, I will try. I want to remove the caption inside the label, but not the label. I know I set this up wrong and I would not do it that way again. But I need to be able to delete or remove the captions.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    Quote Originally Posted by lawdy View Post
    Ok Orange, I will try. I want to remove the caption inside the label, but not the label. I know I set this up wrong and I would not do it that way again. But I need to be able to delete or remove the captions.
    So now you have code to do exactly that.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    No Welshgasman, I don't. That code does not work
    I get an error Invalid use of the Me keyword.

  12. #12
    orange's Avatar
    orange is online now Moderator
    Windows 11 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    lawdy,

    All forms or just some forms? All labels or some labels? The answers will help responders with coding suggestions.

    To use the Me keyword, the form has to be OPEN --- that may be your issue.

  13. #13
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    They are all on the same form. They are quite a few. The form is open. The particular item to be changed is right clicked and a selection is made. Then I get the error.

  14. #14
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    I am beginning to think that the problem may be because it uses a function. I use code like the following all time

    #Forms![frmCalendar].Controls(strCell).BorderColor = vbBlack#So as my title states, I need help with variables (perhaps). That is what I had in mind. Can this be done using the entire form structure instead of using the me keywork?

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Me only works if code is behind form or report. Your code should work. Why would this need to be a Function?

    What do you mean by "The particular item to be changed is right clicked and a selection is made."? How is this relevant to the code?

    If you want to provide db for analysis, follow instructions at bottom of my post.

    BTW, use of # character in your post accomplishes nothing. Use # icon on edit toolbar to generate CODE tags.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 1
    Last Post: 07-27-2022, 03:07 PM
  2. Replies: 1
    Last Post: 10-16-2019, 03:45 PM
  3. Using Public variables between forms/queries/reports
    By dcrake in forum Sample Databases
    Replies: 2
    Last Post: 12-25-2015, 05:44 PM
  4. Replies: 1
    Last Post: 03-17-2015, 01:06 PM
  5. Replies: 3
    Last Post: 02-19-2015, 04:41 PM

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