Grey-out/Disable Textfields by checking a textbox

danskieness

Registered User.
Local time
Tomorrow, 05:39
Joined
Nov 21, 2013
Messages
11
[SOLVED]Grey-out/Disable Textfields by checking a textbox

hi im quite new to access and i wonder how can i disable a textfield or two in a form when the textbox is unchecked

also how do i add a default value for it while the textbox itself is disabled, can i get away with it by adding a default value on the textbox?
 
Last edited:
Hello danskieness, Welcome to AWF. :)

You can make use of the Form Current method.. Default Value only works for New records.. Try something along the lines of..
Code:
Private Sub Form_Current()
    If Me.[COLOR=Blue]yourCheckBoxName [/COLOR]Then
        Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = False
        Me.[COLOR=Blue]yourTextBoxName [/COLOR]= "Something here?"
    Else
        Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = True
    End If
End Sub
Change the blue bits..
 
thanks you pr2, but can you enlighten me about the "Something here?"

what should it contain?
Code:
        Me.[COLOR=Blue]yourTextBoxName [/COLOR]= "Something here?"
 
thanks you pr2, but can you enlighten me about the "Something here?"
Well in your original post you said..
also how do i add a default value for it while the textbox itself is disabled, can i get away with it by adding a default value on the textbox?
You can add whatever you want using VBA, if the CheckBox is disabled..
 
i see now, i'll give it a try and give you a heads up if it works :D
 
@paul
it's giving me an invalid null error

i've gotten a work around by setting a default value of the checkbox, anyways the problem now is the text field itself doesn't refresh whenever i click and unclick :banghead:
 
Add similar code in the AfterUpdate event of the CheckBox..
 
You are most welcome.. :)

There is tow other tricks to escape the Invalid use of Null, (a) using the Nz method OR (b) Checking if the Record is New record before actually testing the condition in Form Current..

Don't worry using Default value as you can see works too.. ;)
 
(b) Checking if the Record is New record before actually testing the condition in Form Current..

i would like to learn this one, can you teach me?
 
i would like to learn this one, can you teach me?
Sure :)

Its just adding a Check (If statement) before going into the main If..
Code:
Private Sub Form_Current()
    If Not Me.NewRecord Then
        If Me.[COLOR=Blue]yourCheckBoxName [/COLOR]Then
            Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = False
            Me.[COLOR=Blue]yourTextBoxName [/COLOR]= "Something here?"
        Else
            Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = True
        End If
    Else
         Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = True
    End If
End Sub
 
i love your logic man it's awesome thank you, i would probably use these in my future projects, btw does access code builder uses vb codes?
 
Or another neat little Trick..
Code:
Private Sub Form_Current()
    If [COLOR=Red][B]Nz([/B][/COLOR]Me.[COLOR=Blue]yourCheckBoxName[COLOR=Red][B], False)[/B][/COLOR] [/COLOR]Then
        Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = False
        Me.[COLOR=Blue]yourTextBoxName [/COLOR]= "Something here?"
    Else
        Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = True
    End If
End Sub

Glad to help ! Good Luck !
 
Or another neat little Trick..
Code:
Private Sub Form_Current()
    If [COLOR=Red][B]Nz([/B][/COLOR]Me.[COLOR=Blue]yourCheckBoxName[COLOR=Red][B], False)[/B][/COLOR] [/COLOR]Then
        Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = False
        Me.[COLOR=Blue]yourTextBoxName [/COLOR]= "Something here?"
    Else
        Me.[COLOR=Blue]yourTextBoxName[/COLOR].Enabled = True
    End If
End Sub

Glad to help ! Good Luck !

what does NZ mean? :P
 
Nz - is a function in Access VBA (http://www.techonthenet.com/access/functions/advanced/nz.php) that acts like a Simplified If that checks for one condition and only one.. Is Null.. So if the Field Value/Variable you are passing to the function is Null it will return another value..
Code:
If IsNull(someVariant) Then
    theTrueValue = anotherValue
Else
    theTrueValue = someVariant
End If
The above code is simple written using Nz as..
Code:
theTrueValue = Nz(someVariant, anotherValue)
 

Users who are viewing this thread

Back
Top Bottom