UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Help With Coding From Two Public Functions To Possibly Just Using One For Greater Efficiency    
 
   
gint32
post Jun 5 2017, 10:57 AM
Post#1



Posts: 211
Joined: 8-May 09
From: Australia


Hi,

Can anyone help me with with the following (working) functions. Thanks to the help of http://www.accessforums.net/showthread.php...0236#post360236

I have a Test_form with many different controls on it, it also contains two buttons one for disabling all the controls that are populated..(have a value/Notnull) and the other button for Re-enabling them.

What I am after is how to tidy the code up a little, as presently due to my incompetance, I have to use two functions and I think but am not sure how, its might be possible for these button to use the same function(once amended) instead of two separate ones as is the case at the moment.

Here what I have so far. which seems ever so cumbersome...Any help with this will be much appreciated...

CODE
Option Compare Database
Option Explicit

Private Sub Command2_Click()
Dim ctl As Control 'lock only the controls that have a value (= notnull)

On Error Resume Next
Stop

For Each ctl In Me.Controls

  Select Case ctl.ControlType
    
    Case acTextBox, acComboBox, acLabel, acCheckBox, acOptionButton
      ctl.Enabled = IsNullEmpty(ctl)
  
  End Select
Next
End Sub

and
CODE
Private Sub Command3_Click()  'unlock all controls that have a value (= notnull)
Dim ctl1 As Control

On Error Resume Next
'Stop
For Each ctl1 In Me.Controls

  Select Case ctl1.ControlType
  
    Case acTextBox, acComboBox, acLabel, acCheckBox, acOptionButton
      ctl1.Enabled = IsNullEmpty1(ctl1)
        
  End Select
Next
End Sub


then

CODE
Public Function IsNullEmpty(ctl As Control)

    IsNullEmpty = False
    
    If IsNull(ctl) Or ctl = "" Or ctl = 0 Then IsNullEmpty = True
        
End Function


and

CODE
Public Function IsNullEmpty1(ctl1 As Control) 'unlocks the textbox

    IsNullEmpty1 = True 'false
    If IsNull(ctl1) Or ctl1 = "" Or ctl1 = 0 Then IsNullEmpty1 = True 'false
    
End Function

This post has been edited by gint32: Jun 5 2017, 11:06 AM
Attached File(s)
Attached File  Database10.zip ( 30.67K )Number of downloads: 1
 
Go to the top of the page
 
mklein
post Jun 5 2017, 11:57 AM
Post#2



Posts: 195
Joined: 7-August 12
From: BC, Canada


You could do...
CODE
Private Sub Command2_Click()
    SetControlState
End Sub

Private Sub Command3_Click()
    SetControlState
End Sub

Private Sub SetControlState()
    Dim ctl As Control

    For Each ctl In Me.Controls
        Select Case ctl.ControlType
            Case acTextBox, acComboBox, acLabel, acCheckBox, acOptionButton
                ctl.Enabled = IsNullEmpty(ctl)
        End Select
    Next
End Sub

Public Function IsNullEmpty(ctl As Control)
    IsNullEmpty = IsNull(ctl) Or ctl = "" Or ctl = 0
End Function

--------------------
| Mark Klein | Access 2010 | Windows 10 | Visual Studio 2013
Go to the top of the page
 
cheekybuddha
post Jun 5 2017, 12:05 PM
Post#3


UtterAccess VIP
Posts: 9,051
Joined: 6-December 03
From: Telegraph Hill


I'm not sure how Mark's code re-enables previously disabled controls.

I would create a function/sub that takes a boolean parameter to determine whether you want the controls enabled or disabled.

I would just wrap the null/empty test within the code:
CODE
Private Sub ChangeCtlState(blEnable As Boolean)

  Dim ctl As Control

  For Each ctl In Me
    Select Case ctl.ControlType
      Case acTextBox, acComboBox, acLabel, acCheckBox, acOptionButton
        If Len(ctl.Value & vbNullString) > 0 Then ctl.Enabled = blEnable
    End Select
  Next ctl

End Sub


Then just call it:
CODE
  ChangeCtlState True

' or

  ChangeCtlState False
as required.

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
ADezii
post Jun 5 2017, 12:15 PM
Post#4



Posts: 1,643
Joined: 4-February 07
From: USA, Florida, Delray Beach


Along very similar lines, I've attached a Demo. The only problem that I see if that all relevant Controls have Values in them then there would be no way to Unlock one or more of these Controls with this Logic. This may, may not, be what you are looking for. See Attached:
Attached File(s)
Attached File  Database10_Revised.zip ( 24.33K )Number of downloads: 3
 
Go to the top of the page
 
mklein
post Jun 5 2017, 02:26 PM
Post#5



Posts: 195
Joined: 7-August 12
From: BC, Canada


I think this works if you have two buttons, lock and unlock, on the form...
CODE
Private Sub cmdLock_Click()
  LockUnlockControls True
End Sub

Private Sub cmdUnlock_Click()
    LockUnlockControls
End Sub

Public Function IsNullEmpty(ctl As Control) As Boolean
    IsNullEmpty = IsNull(ctl) Or ctl = "" Or ctl = 0
End Function

Private Sub LockUnlockControls(Optional state As Boolean)
    Dim ctl As Control
    
    For Each ctl In Me.Controls
        Select Case ctl.ControlType
            Case acTextBox, acComboBox, acCheckBox, acOptionButton
                ctl.Enabled = Me.IsNullEmpty(ctl) Or Not state
        End Select
    Next
End Sub

Mark
This post has been edited by mklein: Jun 5 2017, 02:30 PM
Attached File(s)
Attached File  Database10_0.02.zip ( 23.42K )Number of downloads: 2
 

--------------------
| Mark Klein | Access 2010 | Windows 10 | Visual Studio 2013
Go to the top of the page
 
gint32
post Jun 6 2017, 09:19 AM
Post#6



Posts: 211
Joined: 8-May 09
From: Australia


Looks good and many thanks, I will give it a try soon as, although won't get a chance for few days from now!
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    28th June 2017 - 09:03 PM