Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    3 Star Lounger
    Join Date
    Sep 2002
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Enable controls (xp)

    In a previous postings, Hans posted code to disable code on a form, i have two questions, why does this code, give an error message shown on the line in blue. the line refers to a subform on the form.

    tblsamples2 = the main form
    tbltestr2 = the subform,

    The error generated is 'run time error 2455, you entered an expression that has an invalid reference to the property Form / Report'

    For Each ctl In Forms!tblsamples2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = True
    End Select
    Next ctl

    <font color=blue>For Each ctl In Forms!tblsamples2!tbltestr2.Controls</font color=blue>
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = True
    End Select
    Next ctl


    the 2nd part of the question for a further 5 points, and bonus round....

    When you make a control. Enabled = False, it colors both the background and the text grey.. is there an option somewhere to make that text black, instead of grey, so you can actually see it properly? I've looked all over and can't see anything in access.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Enable controls (xp)

    1) Try

    For Each ctl In Forms!tblsamples2!tbltestr2.Form.Controls

    2) For text boxes, combo boxes, list boxes, option buttons and check boxes, you can set ctl.Locked = True in addition to ctl.Enabled = False. This will undo the grayed-out effect. this doesn't work for command buttons, though.

  3. #3
    5 Star Lounger
    Join Date
    Apr 2003
    Location
    Hampshire, United Kingdom
    Posts
    602
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    Ok, firstly a supplementary question to the first bit: are both forms open and visible when the error occurs?

    I can definitely answer the second question. Instead of setting .enabled to false, try setting .locked to true. This will lock the control without changing its formatting. (If you still want the border to be grey, you'll have to write code to do that at the same time as locking the control. Don't forget to reset it when the control is unlocked).
    Waggers
    If at first you do succeed, you&#39;ve probably missed something.

  4. #4
    3 Star Lounger
    Join Date
    Sep 2002
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    no luck Hans same error message.

    Everything i know about access tells me that my current code is correct and should work. Thats how you would normally refer to a subform on a form.

    Whats eerie, is that the same code further down, appears to work. (This is where i decide to disable the subform and it works!)
    the bit in Blue works, but the bit in Red does not !

    The complete code section is :-

    Private Sub Form_Current()
    Dim ctl As Control

    If Right(Me!SAMPLE, 11) = "Micro Check" Then
    Me.FrozenRecord.Visible = True
    Me.Label1122.Visible = True
    Else
    Me.FrozenRecord.Visible = False
    Me.Label1122.Visible = False
    End If
    Forms!tblsamples2.Detail.BackColor = 8454143

    For Each ctl In Forms!tblsamples2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = True
    End Select
    Next ctl

    <font color=red>'For Each ctl In Forms!tblsamples2!tbltestr2.Form.Controls <font color=magenta><--- new line also doesn't work</font color=magenta>
    ' Select Case ctl.ControlType
    ' Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' ' Control has an Enabled property
    ' ctl.Enabled = True
    ' End Select
    'Next ctl</font color=red>

    If DateDiff("d", Me!samDate, Date) > 7 Then
    Forms!tblsamples2.Detail.BackColor = 12632256
    '---------------------------------- Protect all the Forms ----------------------------------------------
    For Each ctl In Forms!tblsamples2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = False
    End Select
    Next ctl

    <font color=blue>For Each ctl In Forms!tblsamples2!tbltestr2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton
    ' Control has an Enabled property
    ctl.Enabled = False
    End Select
    Next ctl</font color=blue>

    Forms!tblsamples2.Command35.Enabled = True
    Forms!tblsamples2.Command32.Enabled = True
    Forms!tblsamples2.Command23.Enabled = True
    Forms!tblsamples2.Command33.Enabled = True
    Forms!tblsamples2.Command34.Enabled = True

    'If Not IsNull([Forms!tblsamples2!tbltestr2.tsamid]) Then

    ' Forms.tblsamples2.tbltestr2.cmdpre.Enabled = True
    ' Me.tbltestr2.cmdnext.Enabled = True
    'End If


    End If

    End Sub

  5. #5
    5 Star Lounger
    Join Date
    Apr 2003
    Location
    Hampshire, United Kingdom
    Posts
    602
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    Steve,

    If you want to enable or disable ALL the controls on your subform, you can instead enable or disable the subform itself:
    Forms!tblsamples2!tbltestr2.enabled=true
    Forms!tblsamples2!tbltestr2.enabled=false

    This way uses a lot less code and a lot less time, too!
    Waggers
    If at first you do succeed, you&#39;ve probably missed something.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Enable controls (xp)

    Try single-stepping through the code to find out for which control it bombs.

  7. #7
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Enable controls (xp)

    Attached is a sample in which your code does work.

    I used the same names as you and just pasted in your code and it worked!

    One thought I had, (but I could not get it to give the error you describe).

    tbltestr2 is the name of the subform. But is that the name of the subform as it appears in the db window, or the name of the subform control on the main form or both?

    The code needs to refer to the name of the subform control, which may be different to the name of the form itself.
    Regards
    John



  8. #8
    3 Star Lounger
    Join Date
    Sep 2002
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    Thanks John... my subform name, and control are the same. (I will look at your db posted in 2 min !)

    Whats totally confusing me here, and i've spent hours and hours messing with this !!! is look at the code below, the line in red, wont work and gives an error, the line in blue Works! (but they are the same !!!!)



    '---------------------------------------------------------------------------------------------------------------------------------
    Private Sub Form_Current()
    Dim ctl As Control

    If Right(Me!SAMPLE, 11) = "Micro Check" Then
    Me.FrozenRecord.Visible = True
    Me.Label1122.Visible = True
    Else
    Me.FrozenRecord.Visible = False
    Me.Label1122.Visible = False
    End If
    Forms!tblsamples2.Detail.BackColor = 8454143

    For Each ctl In Forms!tblsamples2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = True
    End Select
    Next ctl

    <font color=red>Forms!tblsamples2!tbltestr2.Form!testfor .Enabled = False</font color=red>

    '-------------------------------------------------------------------------------------------------------
    '------------------------------------------ Check length of date ---------------------------------------
    '-------------------------------------------------------------------------------------------------------
    If DateDiff("d", Me!samDate, Date) > 7 Then
    Forms!tblsamples2.Detail.BackColor = 12632256
    '---------------------------------- Protect all the Forms ----------------------------------------------
    For Each ctl In Forms!tblsamples2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = False
    End Select
    Next ctl

    Forms!tblsamples2.Command35.Enabled = True
    Forms!tblsamples2.Command32.Enabled = True
    Forms!tblsamples2.Command23.Enabled = True
    Forms!tblsamples2.Command33.Enabled = True
    Forms!tblsamples2.Command34.Enabled = True

    <font color=blue>Forms!tblsamples2!tbltestr2.Form!testfo r.Enabled = False</font color=blue>
    Forms!tblsamples2!tbltestr2.Form!tmethod.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!tincutime.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!mbatchno.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!mexpdate.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!treadby.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!treaddate.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!tr1.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!tcom.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!Command224.Enable d = False
    Forms!tblsamples2!tbltestr2.Form!Check1804.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!treport.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!Frame1818.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!Command1810.Enabl ed = False
    Forms!tblsamples2!tbltestr2.Form!cmdfirst.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!cmdlast.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!Command1813.Enabl ed = False
    Forms!tblsamples2!tbltestr2.Form!Check1804.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!treport.Enabled = False
    Forms!tblsamples2!tbltestr2.Form!tinspec.Enabled = False


    'Forms!tblsamples2!tbltestr2.Form!cmdpre.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!cmdnext.Enabled = True


    End If




    End Sub

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Enable controls (xp)

    Does the error occur only when you open the form, or also when you move from record to record?

  10. #10
    3 Star Lounger
    Join Date
    Sep 2002
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    The error message when i load the form, doesn't give me the option to continue Hans.

    I've tried the db john posted, and i can see that access is supposed to work, and the code should work... but.. it doesn't.

    To try to understand this further, i gave up trying to do the loop on the subform, and disable the controls, and just tried to reference a single control. which i can do, but only at the blue place in the form above, not in the red code area. /boggle..

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Enable controls (xp)

    One more thing to try: change all instances of Forms!tblsamples2 in the code to Me

    If that doesn't help, could you post a stripped down copy of the database? See <post#=401925>post 401925</post#> for instructions.

  12. #12
    3 Star Lounger
    Join Date
    Sep 2002
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    So the disabling of the controls in the subform, in a loop, works. but every time i visit a new record, i need to first, enable all of the controls on the form, and the subform... the main form works. but I can't seem to enable all of the controls on the subform.







    Private Sub Form_Current()
    Dim ctl As Control

    If Right(Me!SAMPLE, 11) = "Micro Check" Then
    Me.FrozenRecord.Visible = True
    Me.Label1122.Visible = True
    Else
    Me.FrozenRecord.Visible = False
    Me.Label1122.Visible = False
    End If
    Forms!tblsamples2.Detail.BackColor = 8454143

    For Each ctl In Forms!tblsamples2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = True
    End Select
    Next ctl


    'Forms!tblsamples2!tbltestr2.Form!testfor.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!tmethod.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!tincutime.Enable d = True
    'Forms!tblsamples2!tbltestr2.Form!mbatchno.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!mexpdate.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!treadby.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!treaddate.Enable d = True
    'Forms!tblsamples2!tbltestr2.Form!tr1.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!tcom.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!Command224.Enabl ed = True
    'Forms!tblsamples2!tbltestr2.Form!Check1804.Enable d = True
    'Forms!tblsamples2!tbltestr2.Form!treport.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!Frame1818.Enable d = True
    'Forms!tblsamples2!tbltestr2.Form!Command1810.Enab led = True
    'Forms!tblsamples2!tbltestr2.Form!cmdfirst.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!cmdlast.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!cmdpre.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!cmdnext.Enabled = True
    'Forms!tblsamples2!tbltestr2.Form!Command1813.Enab led = True

    '-------------------------------------------------------------------------------------------------------
    '------------------------------------------ Check length of date ---------------------------------------
    '-------------------------------------------------------------------------------------------------------
    If DateDiff("d", Me!samDate, Date) > 7 Then
    Forms!tblsamples2.Detail.BackColor = 12632256
    '---------------------------------- Protect all the Forms ----------------------------------------------
    For Each ctl In Forms!tblsamples2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = False
    End Select
    Next ctl

    Forms!tblsamples2.Command35.Enabled = True
    Forms!tblsamples2.Command32.Enabled = True
    Forms!tblsamples2.Command23.Enabled = True
    Forms!tblsamples2.Command33.Enabled = True
    Forms!tblsamples2.Command34.Enabled = True

    For Each ctl In Forms!tblsamples2.tbltestr2.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCommandButton, acOptionButton, acCheckBox
    ' Control has an Enabled property
    ctl.Enabled = False
    End Select
    Next ctl

    Forms!tblsamples2!tbltestr2.Form!cmdpre.Enabled = True
    Forms!tblsamples2!tbltestr2.Form!cmdnext.Enabled = True
    End If
    End Sub

  13. #13
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Enable controls (xp)

    Here is a further db with more of your code. All still working.
    Regards
    John



  14. #14
    3 Star Lounger
    Join Date
    Sep 2002
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    I tried stripping the db down Hans, but its so huge, and everything is tied in so much with everything else, its proving impossible, to get just that form on its own working.

    Will carry on for now investigating.

  15. #15
    3 Star Lounger
    Join Date
    Sep 2002
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enable controls (xp)

    Update: I managed to fix this by this sequence of events:-

    a) disable all of the controls on the main for, when its opened.
    [img]/forums/images/smilies/cool.gif[/img] enable a button on the main form
    c) set the focus to that main form button
    d) run the loop and disable all of the subform controls.

    I then do not get the described error above.
    There is the problem that you cannot disable a control if it has the focus. but thats work-round-able.


    Many Thanks for all the suggestions guys, very strange behaviour i must say. <img src=/S/bash.gif border=0 alt=bash width=35 height=39>

Page 1 of 2 12 LastLast

Posting Permissions

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