Results 1 to 8 of 8
  1. #1
    mcaliebe is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    18

    Splitting a Text Field and uusing the data to change field properties

    I have a table with 20 records. I have a form that has 6 fields on it and depending on what record is selected determines the number of fields that are use.

    For instance,

    Type NbrConnections CallOuts

    Elbow 2 1,3
    Tee 3 1,2,3
    Tapped Tee 4 1,2,3,6

    I can probably nest 20 If statements to evaluate the "Type" field on the form and than tell it which of the 6 fields to make visible, however I was looking for a simpler solution.



    I thought perhaps there was a way to use the text in the CallOuts fields to create the necessary VBA statements to change the field properties ie

    Elbow 1,3
    Split 1,3 into 1 and 3
    Combine to create fields names "textbox"&"1" and "textbox"&"2"
    Now set the properties for textbox1.visible and textbox2.visible

    Is this possible or is there a better way of handling this?

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    How about setting the tags of your form fields as either 1,2,3,4,5,6



    Then split the the1,3 into 1 and 3

    Then Something like
    For each ctl in me.controls

    For I = 1 to 6
    If ctl.tag=myarray(I-1) then ctl.visible=true
    Else Ctl.visible = false
    End if
    Next
    Next

  3. #3
    mcaliebe is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    18
    Andy

    What do you mean split the 1,3 into 1 and 3?

    1,3 are in a text field in the table and on the form. Is Myarray the text field data?

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe something like this. I used part of the code Andy49 proposed.

    EDIT: BTW, "Type" is a reserved word and shouldn't be used as an object name. Note that I used "ConType".
    "Type" isn't really descriptive either. "Type" of what??? Think of the person that might have to follow you.......
    Attached Files Attached Files
    Last edited by ssanfu; 06-30-2017 at 10:18 PM.

  5. #5
    mcaliebe is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    18
    Thanks Steve. Yes, I can make this work. I just wish I understood more why it worked. I'll have to do some research.

  6. #6
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Does this help?? (I added comments)
    Code:
    Private Sub Form_Current()
        Dim ctl As Control
        Dim I As Integer
        Dim myarray() As String
        Dim tmp As Integer
    
        'if nothing in the control, exit the sub
        If IsNull(Me.tbCallout) Then
            Exit Sub
        End If
    
        ' loop through all of the controls on the form
        ' to hide the 6 controls
        For Each ctl In Me.Controls
            '109 is a text box. 
           ' Could also use the AcControlType Enumeration "acTextBox" instead of the value of 109
            'The control tag property has a number in it.
            If ctl.ControlType = 109 And ctl.Tag >= 1 And ctl.Tag < 7 Then
                ctl.Visible = False
            End If
        Next
    
        ' use the split function to populate the array
        myarray = Split(Me.tbCallout, ",")
        ' loop through the controls
        For Each ctl In Me.Controls 'only want to check text boxes (109 is a text box.)
            If ctl.ControlType = 109 Then
                tmp = Val(ctl.Tag) 'convert the tag string to a value (number)
                If tmp > 0 Then 'if tmp is a 0, there was not an entry in the tag property of the control
                    For I = 0 To UBound(myarray)
                    'check if an entry in the array matches whatins in tmp
                        If tmp = Val(myarray(I)) Then
                            ctl.Visible = True 'if they match, display the control
                        End If
                    Next
                End If
            End If
        Next
    End Sub

  7. #7
    mcaliebe is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    18
    That helps a lot! Thank you.

    Now, if I have additional text boxes on the form, does it matter?

  8. #8
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Shouldn't do as other text boxes won't have a tag so the line starting

    if tmp>0

    will return false so the txt box will effectively be ignored.

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

Similar Threads

  1. Replies: 3
    Last Post: 02-28-2015, 06:52 PM
  2. Replies: 7
    Last Post: 04-23-2013, 11:57 AM
  3. Replies: 6
    Last Post: 11-14-2012, 01:06 PM
  4. Change Text field to Memo Field in a Query
    By Yarbz in forum Queries
    Replies: 5
    Last Post: 06-28-2012, 05:24 PM
  5. Splitting a number field across 4 text boxes
    By R_Badger in forum Reports
    Replies: 1
    Last Post: 02-06-2012, 06:12 AM

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