Enabling Certain Controls (1 Viewer)

Weekleyba

Registered User.
Local time
Today, 01:15
Joined
Oct 10, 2013
Messages
586
On my Contract form I have a button (ButtonOpenProject1) that opens the project form based on the projectID in the current form.
I made a change to the project form to help control the input of data. That change included placing some of the objects to Enable = False on the form.
Now I'm trying to fix my ButtonOpenProject1 since when I click it, those objects are not enabled, even though all the require fields are completed.
ValidationOfControl is a module that checks is if specific fields are populated. If they are not populated is returns True. If all of the required fields are populated, it returns False.

Here's my code for the ButtonOpenProject1

Code:
Private Sub ButtonOpenProject1_Click()
On Error GoTo ButtonOpenProject1_Click_Err
    DoCmd.OpenForm "F_Project", acNormal, "", "[ProjectID]=" & ProjectID, , acNormal

ButtonOpenProject1_Click_Exit:
    Exit Sub
ButtonOpenProject1_Click_Err:
    MsgBox "You have to select a Project first, to Open it."
    Resume ButtonOpenProject1_Click_Exit
    
'ValidationOfControl = True when certain fields are not populated
'When F_Project is opened with this button, these fields should have already been populated.
If ValidationOfControl(Form) = False Then
  With Forms!F_Project.Form
   .cboProjectFY.Enabled = True
   .cboProjectType.Enabled = True
   .cboFundingType.Enabled = True
   .CheckPSD.Enabled = True
   .CheckNEPA.Enabled = True
   .CheckActive.Enabled = True
   .CheckActiveAwarded.Enabled = True
   .CheckActiveContractor.Enabled = True
   .CheckCloseOutProject.Enabled = True
   .txtEstimate.Enabled = True
   .txtProjectPriority.Enabled = True
   .txtDateCreated.Enabled = True
   .SF_Contract.Enabled = True
   .TabCtProjects.Enabled = True
  End With
End If
End Sub

What am I missing here?
When the button is pressed, it opens the form but all the objects shown in the code above are not enabled, even though they should be because ValidationOfControl should be returning false.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 07:15
Joined
Jul 9, 2003
Messages
16,274
Re:-
Code:
ButtonOpenProject1_Click_Exit:
    Exit Sub

I suspect your "Exit Sub" is exiting the sub before anything happens?
 

isladogs

MVP / VIP
Local time
Today, 07:15
Joined
Jan 14, 2017
Messages
18,209
Well for a start all that code needs to be before the error handling.
At the moment it won't run even if an error occurs.

Next I think you need the If ValidationOfControl line after the With line
Similarly reverse the End With and End If

ValidationOfControl(Form) looks wrong as well
 
Last edited:

vba_php

Forum Troll
Local time
Today, 01:15
Joined
Oct 6, 2019
Messages
2,880
here's my "loose cannon" view on this problem of yours, Weekley:
Code:
Private Sub  ButtonOpenProject1_Click()
On Error GoTo ButtonOpenProject1_Click_Err
    DoCmd.OpenForm "F_Project", acNormal, "", "[ProjectID]=" &  ProjectID, , acNormal
are you sure that the string (technically looks like a variable) ProjectID is looking at a control on the current form named ProjectID? Your code is what would be written by a programmer who wants to use a variable called ProjectID as an argument instead of a control with that name. the standard way to write the code to do what you want to do would be:
Code:
DoCmd.OpenForm  "F_Project", acNormal, "", "[ProjectID]=" & [B]Me.ProjectID[/B], , acNormal
although I do believe you can reference a control by omitting the "me." qualifier. your ValidationOfControl function is not executing because you're existing the script before you get there:
Code:
ButtonOpenProject1_Click_Exit:
    Exit Sub
this initialization statement of the function call:
Code:
 If ValidationOfControl(Form) = False Then
is taking a general argument of Form. are you sure that's right? I've never seen that done. if you want to reference an actual form object, I believe you would have to declare it first, then include the declared variable as the argument in the function. like:
Code:
dim frm as form
frm = forms("form name here")
(that code might not be 100% accurate. test it). this line:
Code:
With Forms!F_Project.Form
also might not be accurate. I've issued pointers to form objects many times in my stuff, but I've never used the statement you're using here. but, there's more than one way to do that. if this is indeed part of the problem, you might try changing it to the statement syntax I referred to above:
Code:
With Forms("F_Project")
or maybe even:
Code:
With Forms("F_Project").Form
as an addition to all this garbage I just posted for your review, Tony and Colin both are exactly right in terms of the things you need to fix first. You might also consult them to verify that what I've posted is accurate because I'm not an Access expert. I hope this helps you. :)
 
Last edited:

vba_php

Forum Troll
Local time
Today, 01:15
Joined
Oct 6, 2019
Messages
2,880
I don't know whether to laugh or cry...
what do you mean Tony? I'm just telling the question asker in a round-a-bout sort of way that my solutions are never nice-neat-and-clean like yours are. And to boot, I'm not an expert like you are. Thus, a lot of my stuff can be misconstrued as garbage. Maybe a play on words? Referring to the tech concept "garbage in, garbage out"? But hopefully you guys won't have to see those long-winded posts of mine much longer. I've picked up some enlightenment recently in the form of letter writing skills that's proven to be very effective for getting the attention of employers and actually initiating conversations with hiring managers. So I'm hoping I'll be back on site sometime in the near future and won't be able to post much anymore. Not sure where I got these new skills. Divine intervention? I don't think he'd be too satisfied with my behavior....:p
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:15
Joined
May 21, 2018
Messages
8,525
1. I do not know what you are error checking. I am assuming on the form there is a field or control named project ID. Avoid whenever possible using error handling for an if check.
2. If validationofcontrol = True then I am assuming all controls remain disabled.
3. I am interpretting differently then Collin and I assume validationofcontrol is an external procedure. It looks in the right place to me.

Code:
Private Sub ButtonOpenProject1_Click()
  
  On Error GoTo ButtonOpenProject1_Click_Err
  dim frm as access.form
  
  if trim(me.ProjectID & " ") = "" then
    Msgbox "You have to select a Project first, to Open it. Please fill in project ID on form."
  else
    DoCmd.OpenForm "F_Project", acNormal, "", "[ProjectID]=" & ProjectID, , acNormal
    set frm = forms!F_Project
    'ValidationOfControl = True when certain fields are not populated
    'When F_Project is opened with this button, these fields should have already been populated.
    If ValidationOfControl(Frm) = False Then
      With Frm
        .cboProjectFY.Enabled = True
        .cboProjectType.Enabled = True
        .cboFundingType.Enabled = True
        .CheckPSD.Enabled = True
        .CheckNEPA.Enabled = True
        .CheckActive.Enabled = True
        .CheckActiveAwarded.Enabled = True
        .CheckActiveContractor.Enabled = True
        .CheckCloseOutProject.Enabled = True
        .txtEstimate.Enabled = True
        .txtProjectPriority.Enabled = True
        .txtDateCreated.Enabled = True
        .SF_Contract.Enabled = True
        .TabCtProjects.Enabled = True
     End With
   End If
  end if
  Exit Sub
ButtonOpenProject1_Click_Err:
   MsgBox Err.Number & " " & err.description & "  In ButtonOpenProject1"
End Sub

A control or field on a form is property of the class and can be referred to without Me
so Me.ProjectID or ProjectID or Me!ProjectID are all correct

when referring to a form you can add the form property but is is not needed. It just points back to the obect
so Forms("formName") or Forms!FormName are ok
You can add the .form but it is a waste and not needed.
 

Weekleyba

Registered User.
Local time
Today, 01:15
Joined
Oct 10, 2013
Messages
586
Thanks for all help!
I'm still learning here so that's probably why the code looked strange to you all.
After reviewing your suggestions and further research, I went in the following direction which seems to produce the result I wanted.
I eliminated the error checking. (note: the error checking was generated by Access when I had Access convert the Macro to VBA.)
Below is the code.
Let me know if this is a good idea.
Although it may work, I want to know if this is a good sound solution.

Code:
Private Sub ButtonOpenProject1_Click()
If Me.cboProjectNumber2.ListIndex > -1 Then
   DoCmd.OpenForm "F_Project", acNormal, "", "[ProjectID]=" & ProjectID, , acNormal
    
   'ValidationOfControl = True when certain fields are not populated
   'When F_Project is opened with this button, these fields should have already been populated.
   If ValidationOfControl(Me) = False Then
     With Forms!F_Project.Form
        .cboProjectFY.Enabled = True
        .cboProjectType.Enabled = True
        .cboFundingType.Enabled = True
        .CheckPSD.Enabled = True
        .CheckNEPA.Enabled = True
        .CheckActive.Enabled = True
        .CheckActiveAwarded.Enabled = True
        .CheckActiveContractor.Enabled = True
        .CheckCloseOutProject.Enabled = True
        .txtEstimate.Enabled = True
        .txtProjectPriority.Enabled = True
        .txtDateCreated.Enabled = True
        .SF_Contract.Enabled = True
        .TabCtProjects.Enabled = True
     End With
   End If
  Else: MsgBox "You have to select a PROJECT first, to Open it."
 End If
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:15
Joined
May 21, 2018
Messages
8,525
My question is why this code is on a button click when you open F_Project? I would think this code belongs on the on_current event of F_Project. Seems that where you would validate what fields to enable disable.
 

isladogs

MVP / VIP
Local time
Today, 07:15
Joined
Jan 14, 2017
Messages
18,209
I would restore the error checking but in the correct place as MajP showed you.
Without seeing the full code, I'd question whether ValdationOfControl(Me) is correct. Aren't you checking another form?

In fact assuming it works, I would use MajP's solution as I think its neater.
Perhaps consider using Nz rather than Trim but that's just my personal preference.
 

moke123

AWF VIP
Local time
Today, 02:15
Joined
Jan 11, 2013
Messages
3,912
Another option to consider is using the tag property of the controls to mark it and then loop through the controls collection of the form and if the control has your marker unlock it. I dont want to confuse the issue(well, maybe a little :p) but it will shorten your code considerably.
 

Weekleyba

Registered User.
Local time
Today, 01:15
Joined
Oct 10, 2013
Messages
586
Here's some more info.

The ValidationOfControl public function checks the tag property of combos and text box controls and returns the true or false.
Code:
Public Function ValidationOfControl(frm As Access.Form) As Boolean
'This function requires the user to provide data to Combo and Text boxes that have a * in the Tag property.
'This function is put in the Before Update event of the F_Project.
If frm.Dirty = True Then
    Dim msg As String, Style As Integer, Title As String
    Dim nl As String, ctl As Control
    Dim boolResult As Boolean
    
    nl = vbNewLine & vbNewLine
    For Each ctl In frm.Controls
        If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
            If ctl.Tag = "*" And Len(ctl.Value & "") = 0 Then
              msg = "Data Required for '" & ctl.Name & "' field! Before Update" & nl & _
                    "You can't save this record until this data is provided!" & nl & _
                    "Enter the data and try again . . . "
              Style = vbQuestion + vbOKOnly
              Title = "Required Data..."
              MsgBox msg, Style, Title
              ctl.SetFocus
              boolResult = True
              Exit For
            End If
        End If
        Next ctl
End If
ValidationOfControl = boolResult
End Function

I decided to try putting the ValidationOfControl code in the OnCurrent event of the Project form as MajP suggested. Then I can eliminate it from other places, shortening up the overall code. This is code in the OnCurrent event:
Code:
Private Sub Form_Current()
   'This is on the Project form.
   'ValidationOfControl = True when certain fields are not populated
   'When F_Project is opened with this button, these fields should have already been populated.
   If ValidationOfControl(Me) = False Then
     With Forms!F_Project.Form
        .cboProjectFY.Enabled = True
        .cboProjectType.Enabled = True
        .cboFundingType.Enabled = True
        .CheckPSD.Enabled = True
        .CheckNEPA.Enabled = True
        .CheckActive.Enabled = True
        .CheckActiveAwarded.Enabled = True
        .CheckActiveContractor.Enabled = True
        .CheckCloseOutProject.Enabled = True
        .txtEstimate.Enabled = True
        .txtProjectPriority.Enabled = True
        .txtDateCreated.Enabled = True
        .SF_Contract.Enabled = True
        .TabCtProjects.Enabled = True
     End With
   End If
End Sub

The problem I've run into is when I click the New Project button, it opens the form and the objects that are disabled, are all enabled right away without adding any data.
I'm not sure why this is happening??

Perhaps it's time to post a stripped down version of the my database.
 

Weekleyba

Registered User.
Local time
Today, 01:15
Joined
Oct 10, 2013
Messages
586
I should add, the code for the AddNewProject button, which is on the main form is:
Code:
Private Sub cmdAddNewProject_Click()
'On click, Opens F_Project form to a new record
  Dim stDocName As String
  
  stDocName = "F_Project"
  
  DoCmd.OpenForm stDocName, , , , acFormAdd
End Sub
 

Weekleyba

Registered User.
Local time
Today, 01:15
Joined
Oct 10, 2013
Messages
586
I guess I don't follow Isladogs.
They are already disabled by default (as I understand it)
I have the Enable set to No for each combo box or text box on the Project form that
I do not want the user to enter data in, until the certain combo boxes and text
boxes are first filled in. That is was the ValidationOfControls does for me.

Is that what you are saying?
 

Weekleyba

Registered User.
Local time
Today, 01:15
Joined
Oct 10, 2013
Messages
586
Attached is my database.
Note the 'Add New Project' button the main form, F_SearchMuti.
Why do the objects that I have Enable set to No, suddenly now are enabled?

Thanks for the help.
 

Attachments

  • 0 DFM 3.zip
    480.9 KB · Views: 139

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:15
Joined
May 21, 2018
Messages
8,525
I have not disected your code but your validation event starts with

If frm.Dirty = True Then

That is not going to be true when you call it. In that caase it always returns false, and therefore you enable the controls.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:15
Joined
May 21, 2018
Messages
8,525
Code:
Private Sub Form_Current()
   'This is on the Project form.
   'ValidationOfControl = True when certain fields are not populated
   'When F_Project is opened with this button, these fields should have already been populated.
   Dim valid As Boolean
   valid = ValidationOfControl(Me)
     With Me
        .cboProjectFY.Enabled = Not valid
        .cboProjectType.Enabled = Not valid
        .cboFundingType.Enabled = Not valid
        .CheckPSD.Enabled = Not valid
        .CheckNEPA.Enabled = Not valid
        .CheckActive.Enabled = Not valid
        .CheckActiveAwarded.Enabled = Not valid
        .CheckActiveContractor.Enabled = Not valid
        .CheckCloseOutProject.Enabled = Not valid
        .txtEstimate.Enabled = Not valid
        .txtProjectPriority.Enabled = Not valid
        .txtDateCreated.Enabled = Not valid
        .SF_Contract.Enabled = Not valid
        .TabCtProjects.Enabled = Not valid
     End With
 End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:15
Joined
May 21, 2018
Messages
8,525
I would pull any message out of the validationofcontrols. That should return true or false and that is it. Put the message in the events that call the function. This will give you more flexibility. You do not need the message on a new record or when you update a required field.
 

Users who are viewing this thread

Top Bottom