Yeah, there is was lot of confusion. I was attempting to understand why you would write a for each loop on every record and then use it to update buttons on the UI. In the past, this is usually attempted to update controls on a SubForm. But now knowing that the buttons are not on the SubForm it changes the picture quite a bit.
First thing to address is that your code will eventually make both Button1 and Button2 visible and it will never hide the buttons. The code would needed to be more like this:
- If CboCategory = 1 Then
-
Form_FrmProjects.Buttonl.Visible = True
-
Else
-
Form_FrmProjects.Buttonl.Visible = False
-
End If
-
-
If CboCategory = 2 Then
-
Form_FrmProjects.Button2.Visible = True
-
Else
-
Form_FrmProjects.Button2.Visible = False
-
End If
It can be simplified to:
- Form_FrmProjects.Buttonl.Visible = (CboCategory = 1)
-
Form_FrmProjects.Button2.Visible = (CboCategory = 2)
I would go about what you are doing in a different way. I think even if you get you code to work, it will only update the button's visibility based on the Last Record of the SubForm. Secondly, it will only update the Button's visibility when the user clicks the button. Typically, you want this type of thing being done automatically as the user navigates through the SubForm records.
There is a couple different ways to do this. The simplest is to move this code to the SubForm OnCurrent Event and to the CboCategory After Update Event:
- Form_FrmProjects.Buttonl.Visible = (CboCategory = 1)
-
Form_FrmProjects.Button2.Visible = (CboCategory = 2)
The OnCurrent Event will update the buttons as the user clicks on different records in the SubForm. The AfterUpdate event will update the buttons as the user edits the record.