Check box True adds details

nightmaregenerator

Registered User.
Local time
Tomorrow, 02:45
Joined
Dec 11, 2009
Messages
33
I have a form that I want when a check box is True it allows you to fill in extra details, but if it is False it either hides these details or does not allow you to enter these details.

Anyone have any ideas?
 
Last edited:
Do you have an existing control for the additional details that is currently visible?


If so, place the following code in your forms on current event and in the checkboxes after update event:-

Code:
If Me.YourCheckBoxNameHere = -1 Then
Me.YourDetailsControlNameHere.Visible = True
Else
Me.YourDetailsControlNameHere.Visible = False
End If

If you do not wish to hide the Details control, rather disable it then change the 'Visible' in the above code to 'Enabled'.
 
Just to expand a little on what dbDamo has said:

If you have more that one control into which the user would need to be allowed to enter data or be restricted from entering data based on the status of the check box, then you will need to address each of the controls individually.

If you want to just disable the controls (my personal choice) you would need code like:
Code:
Me.NameOfOneControl.Enabled = me.NameOfCheckbox
Me.NameOfAnotherControl.Enabled = me.NameOfCheckbox

You would need a line like the ones above for each control that you wish to disable when the check box is unchecked.

Notice that using these statememts you do not need the "If" statement ass the enabled status of the controls is controled by the value that is in the check box.

You will need these lines of code to be run in the On Current event of your form as dbDamo has said but you will also need these same lines of code to run from the After Update event of your check box. Therefore I would suggest that you create a user defined function with the needed lines of code in the function. Like:

Code:
Function EnableDisableCtrls ()
     Me.NameOfOneControl.Enabled = me.NameOfCheckbox
     Me.NameOfAnotherControl.Enabled = me.NameOfCheckbox
     'Additional lines of code here as needed for each control to be disabled
End Function

When you have this function written, you can then call this function from the On Current event of your form and the After Update event of you check box. With the following line:

EnableDisableCtrls

This way you only have the required lines of code in one place, making it much eaiser to modifiy the code if and when it is necessary to make changes or add additional controls.
 
I didn't know you didn't need to use an IF statement to set the enabled property - thanks.

Also, I did state in my post that any code should go in the forms on current event AND the checkboxes after update event.

EDIT - Also a good idea about creating a function, saves you having to dig around in all places you have the code when making changes!
 
Sorry I missed where you included the After Update event for the code to be called. Just over looked that one.
 
I just glad to share. Have got to learn to be more observant though. LOL
 
I've been caught out on here a few times!
 
Code:
Function EnableDisableCtrls ()
     Me.NameOfOneControl.Enabled = me.NameOfCheckbox
     Me.NameOfAnotherControl.Enabled = me.NameOfCheckbox
     'Additional lines of code here as needed for each control to be disabled
End Function

When you have this function written, you can then call this function from the On Current event of your form and the After Update event of you check box. With the following line:

EnableDisableCtrls

This way you only have the required lines of code in one place, making it much eaiser to modifiy the code if and when it is necessary to make changes or add additional controls.

I'm only new to macros so I wrote that code here:

Code:
 Private Sub Form_Current()
Function EnableDisableCtrls()
     Me.Hours.Enabled = Me.Local
     Me.HourlyRate.Enabled = Me.Local
    Me.Nights.Enabled = Me.Allowance
      Me.Hours.Disabled = Me.Local
     Me.HourlyRate.Disabled = Me.Local
    Me.Nights.Disabled = Me.Allowance
End Function

So then I put the Function name EnableDisableCtrls into the After Update function but when I went to run the form, it kept saying it could not find the macro. Have I typed something wrong?:confused:
 
If you are using a macro then call the function from within your macro. You cannot do both. You either call the function in the After Update event or you call a macro.

If you are not doing anything else in the macro then change the calling of the macro to Event Proceedure and then call the function from the event.
 
As you are progressing down the validation route you may need to consider what happens if the fields you want to populate (if tick box is true) contain values, then the user unticks the box. This would conflict with your rules. You would need to first check if any of the said fields contain a value, if so then disable the check box if the check box is checked.

The alternative would be bring up a message box stating that the unchecking of the checkbox is invalid as certain fields have values in them already. Or do you want to remove all contents of said fields or not.

David
 
As you are progressing down the validation route you may need to consider what happens if the fields you want to populate (if tick box is true) contain values, then the user unticks the box. This would conflict with your rules. You would need to first check if any of the said fields contain a value, if so then disable the check box if the check box is checked.

The alternative would be bring up a message box stating that the unchecking of the checkbox is invalid as certain fields have values in them already. Or do you want to remove all contents of said fields or not.

David
That was a factor I never took into consideration...

It raises a good point though; I would like it to come up with a message box warning that if they continue the fields would remove all the text.

If I wanted something like this, how would I go about it?
 
If you want to remove the values for fields when the checkbox is unchecked then in the on change event of the checkbox, something along the lines of (aircode, might need some playing with):-

Code:
Dim Msg, Style, Title, Response
Msg = "Your message here"
Style = vbYesNo
Title = "Title for message box here"
Response = MsgBox(Msg, Style, Title)
 
If Response = vbYes Then
      Me.YourControlNameHere = Null
      [COLOR=red]list any other controls to be set to Null[/COLOR]
Else
      Me.Undo
End If

So basically whenever a user changes the value of the checkbox a message will be displayed. If the user selects Yes, the change of value takes place and the listed controls are cleared of any data. If the user selects No, the change of the value of the checkbox is undone and all data remains.

You may want to add in a check before the message is displayed as you only want the message to be displayed when the value of the checkbox is changed from true (-1) to false (0) as this is the only time there will be data to be cleared.
 
This is terrible coding practice:

Code:
Dim Msg, Style, Title, Response

It's essentially declaring all variables as variants. Data types should always be declared as the most restrictive type possible. Why does it matter? Well, recently Symantec has been having its anti-virus updates fail, starting with Jan. 1st, 2010, because they were using a string variable for the year, and "10" is less than "9" so the new updates weren't getting through. Had the variables been properly defined as a numeric type, that never would have happened.
 
Thank you David. Please feel free to provide some alternative code.
 
Please feel free to provide some alternative code.

I would think the reader should be able to figure it out, but in my opinion the correct variable declarations would be:

Code:
Dim Msg As String
Dim Style As VbMsgBoxStyle ' alternative is Long
Dim Title As String
Dim Response As VbMsgBoxResult ' alternative is Integer
 
After reading your response dfenton with the alternate code I understood the pros to doing it that way, I have made those adjustments and have tested it but found that no matter whether I hit Yes or No it clears the box, so I am trying to add a if response but I don't actually know what to put in opposite to Null.

Code:
Dim Msg As String
Dim Style As VbMsgBoxStyle 'alternative is long
Dim Title As String
Dim Response As VbMsgBoxResult ' Alternative is Integer
Msg = " PLEASE BE AWARE: Un-checking this box will clear the Nights text box. Please be aware this checkbox should only be a null value if the driver is doing a local job."
Style = vbYesNo
Title = "Allowance ERROR"
Response = MsgBox(Msg, Style, Title)

[COLOR=Red]If Response = vbYes Then
    Me.Allowance = Null
If Responce = vbNo Then
Me.Allowance =[/COLOR]
Else
    Me.Undo
End If
 
You do not need the 2nd If clause for the No response as this is already dealt with by the Else clause.

Code:
If Response = vbYes Then
Me.Allowance = Null
[COLOR=red]If Responce = vbNo Then[/COLOR]
[COLOR=red]Me.Allowance =[/COLOR]
Else
Me.Undo

There are only 2 outcomes to a vbYesNo - Yes or No. You have dealt with the Yes response in the first 2 lines of the above code. The No response should be dealt with after the Else - which in the code I gave you performs the undo action. Simply remove the 2 lines I have highlighted in your code and it should work just fine.
 
Yeah I was playing with the code and i got it working pretty well except I can't work out how to only get it to pop up if the box is unchecked.
 
Wrap this around your code:-

Code:
If Me.YourCheckBoxNameHere = 0 Then
 
'the rest of your code here
 
End If
 

Users who are viewing this thread

Back
Top Bottom