Results 1 to 6 of 6
  1. #1
    swalsh84 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    36

    Function to Enable/Disable Field

    I have a form that is used for data entry. I give the user the ability to edit the record and save the record. Based on the whether the field meets certain criteria (mainly filled in or not filled in), the field is available for edit. For example, if the customer name is filled in, the field is disabled and vice versa. Currently, I have code that does the following:
    Code:
     
    Me.fieldName.Enabled = True
    Me.fieldName.Locked = False
    Me.fieldName.BackStyle = 1
    Me.fieldName.SpecialEffect = 2
    I am attempting to write a VBA function that will do this for each field that needs to be enabled when the user clicks on the "Edit Record" button. The trouble I'm running into is how to pass the field name to the function and utilize it within the function. Here's what I have so far:

    Code:
     
    Public Function enableField(field As String)
     
        field.Enabled = True
     
     '...etc.
     
        'Me.fieldName.Enabled = True
        'Me.fieldName.Locked = False
        'Me.fieldName.BackStyle = 1
        'Me.fieldName.SpecialEffect = 2
     
    End Function
    I know it's not much, but it doesn't seem to be a difficult thing to accomplish. I want to save a ton of lines of code because I have about 30 fields per record. Any thoughts?



    TIA

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Presuming the function is in the form's module, try:

    Me(field).Enabled = True
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe this will help. I stole the code from Allen Browne's site at

    http://allenbrowne.com/ser-56.html

    and modified it. I put a button on a form and named it "LockBoundControls". The code goes in the click event:

    Code:
    Sub LockBoundControls_Click()
       On Error GoTo Err_Handler
       'Purpose:   Lock/Unlock/Enable/Disable controls
       Dim frm As Form
       Dim ctl As Control      'Each control on the form
       Dim lngI As Long        'Loop controller.
       Set frm = Me
       For Each ctl In frm.Controls
          Select Case ctl.ControlType
          'remove any control types you don't want to Lock/Unlock/Enable/Disable 
          Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox, acOptionButton, acToggleButton
             'Lock/unlock these controls if bound to fields.
             If Len(ctl.ControlSource) > 0 And Not ctl.ControlSource Like "=*" Then
                ctl.Enabled = True
                ctl.Locked = False
                ctl.BackStyle = 1
                ctl.SpecialEffect = 2
             End If
          Case acSubform
             'Do nothing
          Case acLabel, acLine, acRectangle, acCommandButton, acTabCtl, acPage, acPageBreak, acImage, acObjectFrame
             'Do nothing
          Case Else
             'Do nothing
          End Select
       Next
    Exit_Handler:
       Set ctl = Nothing
       Exit Sub
    Err_Handler:
       MsgBox "Error " & Err.Number & " - " & Err.Description
       Resume Exit_Handler
    End Sub
    .

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can also use Allen's code with the Tag property of the controls. Rather than examining the type, you put something in the Tag property of the controls you want affected.

    Love the "Veni, Vidi, Velcro" thing!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    swalsh84 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    36

    pbaldy FTW

    Thanks to both of you. I do have some fields on the form that I did not want to disable so ssanfu's solution (while awesome) doesn't work in my specific case. Final function turned out as such:

    Code:
     
    Public Function enableField(field As String)
     
            Me(field).Enabled = True
            Me(field).Locked = False
            Me(field).BackStyle = 1
            Me(field).SpecialEffect = 2
    End Function
    Public Function disableField(field As String)
            Me(field).Enabled = False
            Me(field).Locked = True
            Me(field).BackStyle = 0
            Me(field).SpecialEffect = 0
     
    End Function
    A call to the function would like this:
    Code:
        'Enable / Disable Source
        If IsNull(Me.cboSource.Value) Then
            enableField (Me.cboSource.NAME)
        Else
            disableField (Me.cboSource.NAME)
        End If
    A quick follow up question: If I wanted to place these functions outside the form and use them elsewhere in other forms, how would I go about calling the function? I'm assuming the same way?

    Thanks!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I suspect you can still pass the field name and use ActiveForm to determine which form is open and active.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 09-29-2010, 09:31 AM
  2. Replies: 1
    Last Post: 06-23-2010, 09:45 AM
  3. could not enable autocorrect
    By easbrandel in forum Access
    Replies: 0
    Last Post: 04-15-2010, 07:10 AM
  4. Replies: 15
    Last Post: 02-16-2010, 10:58 AM
  5. Replies: 5
    Last Post: 06-17-2009, 02:18 PM

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