Confusing Issue with If Statement

Mr. Hero

Registered User.
Local time
Today, 06:35
Joined
Mar 16, 2010
Messages
84
I am trying to utilize an If, Else statement in my on Current event in the form. I am attempting to accomplish a serious of checks so when a record is selected, the stored record will display the selected check boxes properly and the none selected checkboxes are disabled. Any assistance is greatly appreciated!!! Thanks in advance! Below is a serious of if Statements that I have written for my form. :confused:

Private Sub Form_AfterUpdate()
LvDeniedCalledOff.Enabled = True
LvRequest.Enabled = True
LvCalledOff.Enabled = True
LvApproved.Enabled = False
LvDenied.Enabled = False
LvCalledApproved.Enabled = False
LvCalledDenied.Enabled = False
End Sub

Private Sub Form_Current()
End Sub

Private Sub Form_Load()
LvApproved.Enabled = False
LvDenied.Enabled = False
LvCalledApproved.Enabled = False
LvCalledDenied.Enabled = False
End Sub

Private Sub Form_Open(Cancel As Integer)
LvApproved.Enabled = False
LvDenied.Enabled = False
LvCalledApproved.Enabled = False
LvCalledDenied.Enabled = False
End Sub

Private Sub LvApproved_AfterUpdate()
If LvApproved = True Then
LvDenied.Enabled = False
Else
LvDenied.Enabled = True
End If
End Sub

Private Sub LvCalledApproved_AfterUpdate()
If LvCalledApproved = True Then
LvCalledDenied.Enabled = False
Else
LvCalledDenied.Enabled = True
End If
End Sub

Private Sub LvCalledDenied_AfterUpdate()
If LvCalledDenied = True Then
LvCalledApproved.Enabled = False
Else
LvCalledApproved.Enabled = True
End If
End Sub

Private Sub LvCalledOff_AfterUpdate()
If LvCalledOff = True Then
LvCalledApproved.Enabled = True
LvCalledDenied.Enabled = True
LvDeniedCalledOff.Enabled = False
LvRequest.Enabled = False
Else
LvCalledApproved.Enabled = False
LvCalledDenied.Enabled = False
LvDeniedCalledOff.Enabled = True
LvRequest.Enabled = True
End If
End Sub

Private Sub LvDenied_AfterUpdate()
If LvDenied = True Then
LvApproved.Enabled = False
Else
LvApproved.Enabled = True
End If
End Sub

Private Sub LvDeniedCalledOff_AfterUpdate()
If LvDeniedCalledOff = True Then
LvCalledOff.Enabled = False
LvRequest.Enabled = False
Else
LvCalledOff.Enabled = True
LvRequest.Enabled = True
End If

End Sub
Private Sub LvRequest_AfterUpdate()
If LvRequest = True Then
LvApproved.Enabled = True
LvDenied.Enabled = True
LvDeniedCalledOff.Enabled = False
LvCalledOff.Enabled = False
Else
LvApproved.Enabled = False
LvDenied.Enabled = False
LvDeniedCalledOff.Enabled = True
LvCalledOff.Enabled = True
End If
End Sub
 
What seems to be the issue with the Fields and Controls?

Your if statements seem OK.

Would you be able to upload a sample to the forum and we will take a look.
 
Okay I have attached the database, I have written some code in the OnCurrent event in the Form. When I scroll between records not all the checked boxes enable and disable properly.
 

Attachments

What is the purpose of this test?

You have different things going on in current and activate which is confusing

when you activate the form it is to go to a new record, you should also set the default values to false for each of the checkbox and enable them

That would be the start point, then you can deal with after updates, and also navigating to current records, you also didn't have the navigation option selected so there was no way to navigate to the next record.

I have commented all the code out except the Form Activate and add the code to set values to false and enable.


Private Sub Form_Activate()
DoCmd.GoToRecord , , acNewRec

Me.LvApproved.Value = False
Me.LvCalledApproved.Value = False
Me.LvCalledDenied.Value = False
Me.LvCalledOff.Value = False
Me.LvDenied.Value = False
Me.LvRequest.Value = False
Me.LvDeniedCalledOff.Value = False

LvApproved.Enabled = True
LvDenied.Enabled = True
LvCalledApproved.Enabled = True
LvCalledDenied.Enabled = True
Me.LvRequest.Enabled = True
Me.LvDeniedCalledOff.Enabled = True

End Sub

I haven't attached the database as I am not clear of the purpose of the form.
 
I would like to start off by thanking you for the time you are taking to help me!!

Okay this is the purpose of the form.
First when you open the form to input the records Checkbox1, checkbox2, and box3 will be enabled and the others will be disabled. Lets say you selected Checkbox1, then checkbox2 and box3 will become disabled, and box1a and box2a now are enabled. Then if you check on box1a, box2a will become disabled. Note: checkbox2, box2a, box2b, and box3 are still disabled. Once you click on the add button, a new record becomes available and checkbox1, checkbox2 and box3 are enabled, and box1a, box1b, box2a, and box2b are disabled. I hope this makes sense.

Thanks a bunch for you assistance!
 
Trevor,
I am still having some difficulties with my form. Could you assist me with this issue? My discription (post #5) is what I haven't been able to accomplish 100%. I think I am close. I believe the issue is my lack of knowledge in using the proper if statements in the correct properties...
 
I'm going to suggest one Procedure that enforces the logic of what you want. Then call it wherever that logic basis may be changed. It will make it easier for all of us to understand your logic tree.
 
Just as one example, you don't need an If/Then/Else case in at least a couple of these...

Code:
If LvApproved = True Then
LvDenied.Enabled = False
Else
LvDenied.Enabled = True
End If

Could also be written as

Code:
    LvDenied.Enabled = NOT LvApproved

Shorter, faster, and still directly to the point.
 
I tried your suggestion and it works great. Thanks!
 
RG, I am not sure if I understand. Are you saying that I need to create function or module?
 
Since this involves only one form, the Procedure can be in the Class (code) module of *that* form. Once the logic is all in one location it will be easier to understand, support and document. Then wherever you feel the logic needs to be checked again you just put in Call YourProcedure. Doc is of course right that it can be written in easier ways but I thought you should take this Procedure step first for everyone's benefit.
 
I am kind of following. I will try to type out my understanding of your sugguestion. For example, Since I am using many controls of the same type, I can create a class that turns the controls on or off based on user selection...
 
You can start with a sequence of If...Else...EndIf statements or a sequence of Me.Control.Enabled = Not (Me.Control = "Approved") or whatever. The main thig is get all of the logic like this in one location so we can follow what is going on. I'm a simple kind of guy and write code for flexability and maintainability and not necessarily for speed of execution.
 
This will take me some time to figure out how to write the logic for all the similar controls into a class. I understand what you mean, but I am not very good at writting classes or modules and calling them (the calling part is less challenging). I give it a try; do you have any idea in how many functions I will need to create? So something kind of like this..?

Dim Cntl As Control
For Each Cntrl In Me.Section(acDetail).Controls
Select Case Cntl.ControlType
Case acComboBox
Cntl.Value = Null
End Select
Next
 
One! The logic should all go in one sub/function. No Class needed.
 
Okay, So lets say I now have a module called modOnOff. Inside this module I will have created the following

Public Function boxTurnOn()
Dim Cntl As Control

For Each Cntrl In Me.Section(acDetail).Controls
Select Case Cntl.ControlType
Case acComboBox
Cntl.Value = Null
End Select
Next
End Function

Public Funtion boxTurnOff()
Dim Cntlz As Control
For Each Cntrlz In Me.Section(acDetail).Controls
Select Case Cntlz.ControlType
Case acComboBox
Cntlz.Value = True
End Select
Next
End Function
 
As I said before, this need not be in a standard module. It can be in one procedure in the Form's code module. You need to crawl before you can walk so start with a sequence of If...Then statements as you have already done in those assorted events. We will then see what rthe logic is of your form and be able to assist. Then you can start creating the For...Next procedures to shorten the code.
 
Last edited:
RG,
I do agree with your analogy for me to start from the basics.

I have attached a database please see post #3. Inside this database contains what I have written for my checkboxes. Would you mind taking a look to see if I am on the write path? Thanks
 
I've added a procedure at the bottom of the code module for the form and called it from the Current even of the form to show you what I mean. Why are you manipulating the Enabled property of the CheckBoxes instead of the .Value of each CheckBox?
 

Attachments

RG,
When I started building this form I thought that I could accomplish what I wanted by enabling or disabling the controls.

You have brought up a interesting point. I think that I will rewrite all the statements to reflect the changes, based on the value selection.

Thanks for the Database example.
 

Users who are viewing this thread

Back
Top Bottom