Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read only settings (97)

    Hi, I am wanting to be able to make a 'Update' form by only haveing those fields that are as yet unpopulated accessible.

    on the design properties i know i can set the 'enable' property to No, but i am wanting to do this at the form_load event, as i have the form opening on a specific record from a previous form and query...

    i have this in mind

    Private Sub Form_Load()
    Dim cn As Control
    Dim f As Form
    Set f = Me
    For Each cn In f
    ' make those fields that are not empty read only
    Next cn
    End Sub

    not sure of how to do this though? can u help?

    thanks
    Thanks,

    pmatz

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Read only settings (97)

    You should be able to use something like
    <font color=blue><font face="Georgia">If IsNull(cn.Value) Then
    cn.Enabled = False
    cn.Locked = False
    Else
    cn.Enabled = True
    cn.Locked = True
    End If</font face=georgia></font color=blue>
    Wendell

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Read only settings (97)

    Before you start enabling and disabling controls, you need to test the control type to make sure it applies. You undoubtedly have labels, command buttons, and other control that are not data controls and that you do not want to disable. You can't lock a label anyhow, so trying to will trigger an error. You can do something like this:

    <pre>Private Sub Form_Load()
    Dim cn As Control

    For Each cn In Me.Controls
    ' make those fields that are not empty read only
    Select Case cn.ControlType
    Case acTextBox, acComboBox
    cn.Enabled = IsNull(cn) Or Len(cn & "") = 0
    cn.Locked = Not IsNull(cn) Or Len(cn & "") > 0

    Case Else
    End Select
    Next cn
    End Sub</pre>

    Charlotte

  4. #4
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read only settings (97)

    Thanks guys, thats great. ( i like the use of the & "" to stop a null error )

    how come then after i type the dot after cn, 'enable' and 'locked' do not come up as methods?

    cheers
    Thanks,

    pmatz

  5. #5
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Read only settings (97)

    Since you have defined the form as a variable, Access can't make the association - in addition, they aren't really methods, they are properties that you set.
    Wendell

  6. #6
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read only settings (97)

    I see - thanks! [img]/forums/images/smilies/smile.gif[/img]

    If i want to dim F as just the detail on the form is this the best way?

    Dim F as Object
    Set F = Me.Detail

    ?
    Thanks,

    pmatz

  7. #7
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Read only settings (97)

    I think that should work, but you might want to use a slightly longer variable name just to be sure of no conflicts and to mean something down the road.
    Wendell

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Read only settings (97)

    If you just want to deal with the controls in the detail section, you could add a test for Section = 0, which is the detail section. I don't believe you'll be able to loop through the controls is a section, although each control has section as a property, so you can test for it.
    Charlotte

  9. #9
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read only settings (97)

    Thanks peeps.

    Another (related) Q:

    On this form now i have 2 subforms. They are currently in datasheet mode.

    Can I get the code to include those controls too that have data in them?

    something like :

    Dim cn As Control
    Dim f As form
    Set f = Me
    For Each cn In f.Controls
    If cn.ControlType = acTextBox ( or datasheetfield?) Then
    cn.Enabled = IsNull(cn) Or Len(cn & "") = 0
    cn.Locked = Not IsNull(cn) Or Len(cn & "") > 0
    End If
    Next cn

    or would i have to do
    dim subf as subform
    etc

    for each of them and then loop through their controls?

    <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Thanks,

    pmatz

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

    Re: Read only settings (97)

    A subform is likely to have multiple records. If you disable or lock a control in a datasheet form or continuous form, you disable or lock it for all records. It is simply NOT possible to have a control enabled in one record and the "same" control disabled in another record. So what do you want to accomplish with the subforms?

  11. #11
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read only settings (97)

    Ah, yes that makes sense. Though it causes me a problem.

    I want to 'lock out' any records that have already been input, and leave open those that can be updated. Once a vlue has been input and then the form closed I dont want general users to be able to edit tohose records. But as each record has fields that will need to be updated at different times then i want to leave these blank ones open.

    If that makes sense ... erm...

    [img]/forums/images/smilies/smile.gif[/img]
    Thanks,

    pmatz

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

    Re: Read only settings (97)

    You can put the code to enable/disable/lock/unlock in the On Current event of each of the subforms. It may look strange, but at least the correct controls will be enabled etc. as the user moves from record to record. If you make the subforms single forms instead of datasheets or continuous forms, it will look more natural, but of course the user can only see one subform record at a time then.

  13. #13
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read only settings (97)

    Cool, but it gives an error if i try to go back into a control. perhaps i should do it on the open event?
    Thanks,

    pmatz

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

    Re: Read only settings (97)

    No, the On Open event doesn't fire for subforms. What do you mean by "go back into a control" and what is the error you get?

    (Providing specific information in a question increases the chance of getting a specific answer. Sentences like "it doesn't work" or "it gives an error" don't give others much to work on.)

  15. #15
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Read only settings (97)

    The open event fires for subforms, but not when the developer might expect.
    Charlotte

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
  •