Results 1 to 5 of 5
  1. #1
    Tenmakk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    35

    Checking/Unchecking adds/deletes a specific word. Trying to find more efficient code than mine.

    Hello,



    I have 3 checkboxes on my form that will put "Student", "Teacher", and "Family" into a textbox. If one of the 3 is unchecked, that text in the textbox will be removed. This works great, but unfortunately I am going to need 6 more checkboxes now for 6 different things. For 9 checkboxes, I think this code could get really messy and repetitive. Is there a quicker way or more efficient way of doing this? I'm willing to take the time to write all that code, but I don't want to do it if there's a more efficient way. This is my code for the 3 checkboxes I have:

    Code:
    Private Sub chkFamily_AfterUpdate()
    
    If chkFamily Then
        txtPreFamily = "Family"
    Else
        txtPreFamily = ""
    End If
    
    txtPosition = txtPreTeacher & " " & txtPreStudent & " " & txtPreFamily
    
    End Sub
    
    Private Sub chkStudent_AfterUpdate()
    
    If chkStudent Then
        txtPreStudent = "Student"
    Else
        txtPreStudent = ""
    End If
    
    txtPosition = txtPreTeacher & " " & txtPreStudent & " " & txtPreFamily
    
    End Sub
    
    Private Sub chkTeacher_AfterUpdate()
    
    If chkTeacher = True Then
        txtPreTeacher = "Teacher"
    Else
        txtPreTeacher = ""
    End If
    
    txtPosition = txtPreTeacher & " " & txtPreStudent & " " & txtPreFamily
    
    End Sub
    Thanks for your help.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    What does this mean in plain English? You have described HOW you have done something, but the something and rationale is unclear.

  3. #3
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Not sure if its much better, but what about something like:

    Code:
    Private Sub chkFamily_AfterUpdate()
      RunChecks "F"
    END SUB
    
    Private Sub chkTeacher_AfterUpdate()
      RunChecks "T"
    END SUB
    
    Private Sub chkStudent_AfterUpdate()
      RunChecks "S"
    END SUB
    
    private sub RunChecks(pvFlag)
      select case pvFlag
          case "F"
    If chkFamily Then txtPreFamily = "Family" Else txtPreFamily = "" End If
    case "T"
    If chkTeacher Then txtPreTeacher = "Family" Else txtPreTeacher = "" End If
    case "S"
    If chkStudent Then txtPreStudent = "Student Else txtPreStudent = "" End If
    end select txtPosition = txtPreTeacher & " " & txtPreStudent & " " & txtPreFamily end sub

  4. #4
    Tenmakk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    35
    orange, I want the user to specify whether the information he's entering on the form is relevant to Student, Teacher, or Family (or any combination) so what they check will appear in the textbox.

    Ranman256, thanks. I will try the code when I get home.

  5. #5
    Tenmakk is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    35
    Thank you Ranman256, this is easier.

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

Similar Threads

  1. Replies: 8
    Last Post: 09-15-2015, 11:34 AM
  2. Replies: 5
    Last Post: 06-23-2014, 08:54 AM
  3. Code deletes too much records
    By JeroenMioch in forum Access
    Replies: 2
    Last Post: 01-03-2013, 08:06 AM
  4. Need help making my code more efficient
    By themidnitereign in forum Programming
    Replies: 3
    Last Post: 08-10-2012, 11:06 AM
  5. More efficient way of repeating code
    By karmacable in forum Programming
    Replies: 2
    Last Post: 09-12-2011, 12: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