Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    Join Date
    Aug 2012
    Posts
    49

    Unanswered: Option Group Text

    Hi guys...I'm new here and looking for a little help.

    I've got an Option Group on my form with 5 possible options displayed as radio buttons. Beside each radio button is a word which is associated with that choice. I'd like that text to turn red when that particular option is selected. Is this difficult ?
    I've done a search on Google and haven't found anything

    I'd appreciate any help,
    Thanks.

    Jen

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Let's imagine that the name of the Option Group control is MyOptionGroup, that the names of the Radio Buttons are Option1 (OptionValue=1), Option2 (OptionValue=2), ..., Option5 (OptionValue=5) and that the labels associated with each button are Label1, Label2, ..., Label5.
    You can use this procedure in the Form module:
    Code:
    Option Compare Database
    Option Explicit
    
    Private m_FormerValue As Integer
    
    Private Sub MyOptionGroup_AfterUpdate()
    
        If m_FormerValue <> 0 Then Me.Controls("Label" & m_FormerValue).ForeColor = vbBlack
        m_FormerValue = Me.MyOptionGroup
        Me.Controls("Label" & m_FormerValue).ForeColor = vbRed
        
    End Sub
    If the Option Group control has a default value (say 1), add this:
    Code:
    Private Sub Form_Open(Cancel As Integer)
    
        m_FormerValue = Me.MyOptionGroup.Value
        Me.Controls("Label" & m_FormerValue).ForeColor = vbRed
        
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Aug 2012
    Posts
    49
    Thanks for helping Sinndho. I've added the code to the AfterUpdate event on the forms property sheet. Is this right ?

    Nothing happens when i click on the radio buttons at first but after i've clicked on say the 3rd one i get the following message.

    Run-time error '2465':
    Microsoft Office Access can't find the field 'Label80' referred to in your expression

    Sorry if i've messed up the code but i've never tinkered with VBA before. Here's my code as it stands

    Code:
    Option Compare Database
    
    Option Explicit
    
    Private m_FormerValue As Integer
    
    Private Sub OptProb_AfterUpdate()
    
        If m_FormerValue <> 0 Then Me.Controls("Label" & m_FormerValue).ForeColor = vbBlack
        m_FormerValue = Me.OptProb
        Me.Controls("Label" & m_FormerValue).ForeColor = vbRed
        
    End Sub

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    The important thing is that the naming of the label (more precisely its numeric ending) must match the values of the corresponding option buttons.
    Have a nice day!

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    why choose the after update event?
    I'd expect this to be done (if it must) in the forms
    on current event (to set the value when the record changes)
    on change event (to reflect the new value)
    after update doens't provide any useful info 9in my books, but perhaps I've been away from Access events too long

    personalyy changing the label to red doesn't really add anything and if anything subtracts from the overall user interface paradigm used in Access applications. it comes accross as more something thats been done because you can rather than you need
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    This is tested and works:

    Code:
    Private Sub Frame0_Click()
     
     Select Case Me.Frame0
      
      Case 1
       
        Label4.ForeColor = vbRed
      
      Case 2
      
        Label6.ForeColor = vbRed
      
      Case 3
       
         Label8.ForeColor = vbRed
       
     End Select
       
    End Sub


    You'll also need the same code in the Form_Current event, as Healdem said, in order for the formatting to be Record-appropriate, in moving from Record-to-Record, and as Sinndho alluded to, the names of the Labels in the code must match the actual Label names of the corresponding Option Buttons.

    The actual name of your Option Group also needs to be placed instead of Frame0 in the code.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    Join Date
    Aug 2012
    Posts
    49
    My Labels are numbered Labels 1 - 7 but the error message is saying something about not being able to find Label25 referred to in my expression ?. I don't have a Label25.

  8. #8
    Join Date
    Aug 2012
    Posts
    49
    Sorry made that last post before i saw missinglinq's

    This is my code now, which is in the On Current and After Update events of the main form's properties

    Code:
    Private Sub OptProb_Click()
     
     Select Case Me.OptProb
      
      Case 1
       
        Labell.ForeColor = vbRed
      
      Case 2
      
        Label2.ForeColor = vbRed
      
      Case 3
       
         Label3.ForeColor = vbRed
         
      Case 4
       
         Label4.ForeColor = vbRed
         
      Case 5
       
         Label5.ForeColor = vbRed
         
      Case 6
       
         Label6.ForeColor = vbRed
         
      
       Case 7
       
         Label7.ForeColor = vbRed
         
     End Select
       
    End Sub

    When i click the 1st option button, the 5th option button's text turns red.

  9. #9
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Before the line

    Select Case Me.OptProb

    place this line

    Msgbox Me.OptProb

    Run the Form and click thru the Option Group radio buttons, one at a time, and see what Value is being produced by each, especially the first button.

    Also, double-check the names of the Labels. Make sure that you select the Label, itself, before checking the name, not the Radio Button.

    Consecutive names like Labell, Label2, Label3, etc. means that you renamed these Labels, as the Access Gnomes always add two as suffixes. If the Label for the first Option was Labell then the next ones would be Label3, Label5, etc. When you're renaming a number of controls at one time it's easy to make a mistake; we've all done it!

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    Join Date
    Aug 2012
    Posts
    49
    Ok i put the extra line of code in and i get little message boxes telling me the value of each option button as i click it. They were all correct.

    I changed the names of all the labels and they are now Label1,3,5,7,9,11,and 13.

    The option buttons are numbered Option1 - Option7

    Option Group name is OptProb

    One odd thing is that when i look at the Forms property sheet, the OnCurrent and AfterUpdate events don't have that little message that says [Event Procedure] when you've added a bit of code in there.


    Code:
    Private Sub OptProb_Click()
    
       
     Select Case Me.OptProb
      
      Case 1
       
        Label1.ForeColor = vbRed
      
      Case 2
      
        Label3.ForeColor = vbRed
      
      Case 3
       
         Label5.ForeColor = vbRed
         
      Case 4
       
         Label7.ForeColor = vbRed
         
      Case 5
       
         Label9.ForeColor = vbRed
         
      Case 6
       
         Label11.ForeColor = vbRed
         
      
       Case 7
       
         Label13.ForeColor = vbRed
         
     End Select
       
    End Sub

  11. #11
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    That's the problem, then! Sometimes if you simply paste code into the Form's Code Module the Gnomes have trouble 'connecting' to the Events. Go to Properties - Events, click on the Option Group's AfterUpdate Property; this should take you to that event in the Code Module, assuming that the names match up. Back out of the Code Module and repeat this procedure for the OnCurrent event. Check to make sure that they are 'connected,' and you should be set.

    It's probably a good idea, when Copying & Pasting code from an online source, to actually go to and click on the appropriate Event from the 'Properties Pane,' go to the Code Module, and paste the code (minus the Sub Header and End Sub from the posted code) into the Sub.

    After 'connecting,' make sure, once again, that the Label names in the code and the actual names match properly.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  12. #12
    Join Date
    Aug 2012
    Posts
    49
    I've added the code to the OnCurrent event of the main form and the AfterUpdate event of the option group...is this correct ?.

    So i save it and try clicking on the radio buttons but the text doesn't change colour. When i go back into the properties, the little [Event Procedure] message is either gone or the code has changed from what i put in 2 minutes previously.

    By the way i renamed the option group 'ExProbability'

    The code below is what i entered onto the AfterUpdate event of the option group


    Code:
    Private Sub ExProbability_AfterUpdate()
    
    Select Case Me.ExProbability
    
    
    
      Case 1
       
        Label1.ForeColor = vbRed
      
      Case 2
      
        Label3.ForeColor = vbRed
      
      Case 3
       
         Label5.ForeColor = vbRed
         
      Case 4
       
         Label7.ForeColor = vbRed
         
      Case 5
       
         Label9.ForeColor = vbRed
         
      Case 6
       
         Label11.ForeColor = vbRed
         
      
       Case 7
       
         Label13.ForeColor = vbRed
         
     End Select
    
    
    End Sub


    The following code is whats there when i go back in a minute later


    Code:
    Private Sub Form_Current()
    
    End Sub
    
    
    
    Private Sub ExProbability_AfterUpdate()
    
    
    Select Case Me.ExProbability
    
    
    
      Case 1
       
        Label1.ForeColor = vbRed
      
      Case 2
      
        Label3.ForeColor = vbRed
      
      Case 3
       
         Label5.ForeColor = vbRed
         
      Case 4
       
         Label7.ForeColor = vbRed
         
      Case 5
       
         Label9.ForeColor = vbRed
         
      Case 6
       
         Label11.ForeColor = vbRed
         
      
       Case 7
       
         Label13.ForeColor = vbRed
         
     End Select
    
    
    End Sub

  13. #13
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Yikes! It would appear that the Access Gods are angry, and that you've got corruption, either of the Form or the entire Database!

    Usually the first thing to try is to create a new, blank Access file, then import everything (all objects) into it, then see if you can set the Events as they should be and have them 'hold.'

    Sometimes this rather simple thing is all that's needed to recover from corruption. If that doesn't work, here are two links to two Access experts' articles on recovering from corruption:

    Allen Brown on corruption.

    Microsoft Access tips: Recovering from corruption

    Tony Toews on corruption.

    Corrupt Microsoft Access MDBs FAQ

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  14. #14
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Im not suprised the Access gnomes are angry. The afterupdate event is not an appropriate event for this sort of thing. It fires after the update has proceeded but i presume you want to set this when thd use makes a change.

    Incidentally you should set the colour of all the labels to the default prior to setting the other to red. Its the price you have to pay for diverging from the ui standards
    I'd rather be riding on the Tiger 800 or the Norton

  15. #15
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    You're right about resetting all non-clicked items to the default color, in case the user makes a selection then changes their mind, but the Control's AfterUpdate event is exactly where this type of code belongs! What difference does it make if the update has already taken place?

    Linq ;0)>
    Last edited by Missinglinq; 08-31-12 at 21:19.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •