Results 1 to 6 of 6
  1. #1
    grizzli00 is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    1

    Using tag feature

    Hi


    I have a contimuous form containing many text box (text1, text2, tex3......text12, Choice)

    On [tex1] I have assign a script on the ON ENTER event

    The script says [Choice] = 1

    I want to assign the same script to ALL text box (except [Choice] )

    Is there an easy way to assign the same scrip in one shot (maybe using Tag feature) instead of repeating 12 times the same script

    thanks

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    The easiest way is probably to just set the default property value to be 1.
    You can also select all necessary controls in design view then put a value in the tag property line of the property sheet. You would then have to loop over the form controls collection and set the value if the Tag property equals that keyword you entered. Another would be to write a function that accepts the control name as an argument and instead of setting tag values, you'd set the event property to myFunction() (change myFunction to the name of your function). However, to pass the name you must then individually edit every event property to include the control name (txt1 or whatever). There is a way to skip that step and refer to the Active control but that's not as fool proof. Sometimes the active control is not the one you expect. Either of those is much more complicated than just setting the default value. I'm wondering what is the point of setting a bunch of controls to some value but I suppose you have your reasons.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I'm assuming you're referring to a macro? Can you just create 1 macro then call that macro from the on enter event of each textbox?

    You can copy and paste macros, just select all the steps in the macro and CTRL+C, go to your new macro and CTRL+P

    We usually prefer vba to macros around here.

    Click image for larger version. 

Name:	Untitled.png 
Views:	20 
Size:	22.4 KB 
ID:	44489

  4. #4
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    I usu copy the 1 event block to notepad

    Code:
    Private Sub txtBox1_Enter()
    
    choice = 1
    End Sub


    then paste a dozen copies in the notepad, then alter the textbox name to the new name:

    Private Sub txtBox2_Enter()
    choice = 1
    End Sub


    Private Sub txtBox3_Enter()
    choice = 1
    End Sub



    then paste all the code back into the bottom of the form vb.
    all the events should install.

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    similiar to ranman I use the immediate window

    for i = 1 to 12
    Debug.print "Private Sub txtBox" & i & "_Enter()" & vbnewline & "
    choice = 1" & vbnewline & "End Sub" & vbnewline & vbnewline
    next i
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by grizzli00 View Post
    The script says [Choice] = 1

    I want to assign the same script to ALL text box (except [Choice] )
    Hi!

    If you want to reset the value of Choice to 1 by Enter of the rest textboxes, follow those steps:
    1. In code module of your form, create a public function as folows:
    Code:
    Function ResetChoise()
        Me!Choice = 1
    End Function
    2. In design view, select all desired texboxes of your form and type the expression =ResetChoise() in the property OnEnter of selected controls as seems in the picture below:

    Click image for larger version. 

Name:	EventsTab.JPG 
Views:	10 
Size:	23.4 KB 
ID:	44505

    But, if you want to pass the corresponding property of the current control (.Value, Tag, .Name or whatever) to the Choice, you have to follow an other path.
    1. Paste the code below in a new Class module named clsTextbox:
    Code:
    Option Compare Database
    Option Explicit
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    'Code for a Class module.
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Public WithEvents Target As TextBox
    '
    
    Private Sub MyTextBox_Enter()
        Target.Parent!Choice = Target.Tag
    End Sub
    2. Paste the code below in the code module of your form:
    Code:
    Option Compare Database
    Option Explicit
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    'Code for a Form module.
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Private colMyTextboxes As Collection
    '
    
    Private Sub Form_Load()
        Dim ct As clsTextbox
        Dim c As Control
        Dim i As Integer
        
        'Create a local collection.
        Set colMyTextboxes = New Collection
        For Each c In Me.Controls
            If c.ControlType = acTextBox Then
                'Skip the Choice control.
                If c.Name <> "Choice" Then
                    i = i + 1
                    'Set the Tag of textbox.
                    c.Tag = i
                    'Enable the Enter event of textbox.
                    c.OnEnter = "[Event procedure]"
                    'Create an instance of clsTextbox.
                    Set ct = New clsTextbox
                    'Point this instance to this textbox.
                    Set ct.Target = c
                    'Keep this instance in collection.
                    colMyTextboxes.Add ct, c.Name
                End If
            End If
        Next c
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        'Release the collection with all instances.
        Set colMyTextboxes = Nothing
    End Sub
    In any case, open your form and check the results and let us know if works as you expected.

    I hope you are still with us,
    John

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with the Max feature
    By Shannonvip in forum Access
    Replies: 3
    Last Post: 02-10-2014, 02:15 PM
  2. Search Bar Feature
    By Underw7519 in forum Access
    Replies: 1
    Last Post: 08-27-2013, 01:39 PM
  3. Login Feature
    By Yanni in forum Access
    Replies: 1
    Last Post: 01-22-2013, 01:29 AM
  4. Directory Creation Feature Help
    By MWB in forum Access
    Replies: 6
    Last Post: 10-27-2010, 01:50 PM
  5. Comparison Feature Help
    By Kapelluschsa in forum Access
    Replies: 2
    Last Post: 10-25-2010, 06:43 AM

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