Disable texboxes (1 Viewer)

JPR

Registered User.
Local time
Today, 06:19
Joined
Jan 23, 2009
Messages
192
Hello friends, still asking for your precious help.

I have a form with a record source based on a query. It has a textbox which can indicate two different values: A or B.
If it has A, I would like disable other textboxes. If B, then the textboxes should not be disabled.

I have tried several ways, with the If Then, ElseIf and Case but none work. Please see code below

Example 1

If me.textbox1 = "A" then
Me.textbox2.disabled = true
me.textbox3.disables = true

If me. textbox1 = "B" then

Me.textbox2.disabled = false
me.textbox3.disables = false

Example 2

Select Case Me.textbox1

Case "A"
Me.textbox2.disabled = True
Me.textbos3.disabled = True

Case "B"
Me.textbox2.disabled = False
Me.textbos3.disabled = False

End Select

Thank you
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 13:19
Joined
Jul 9, 2003
Messages
16,245
A good approach for doing this is to use a for next loop. Instead of accessing individual controls directly you go through the whole collection of the controls and carry out your update.

This has the advantage that it doesn't matter how many controls you've got you just have the same piece of code, whether you've got two text boxes or 50 text boxes.

See more information on my website, see video number one:-

 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:19
Joined
May 7, 2009
Messages
19,175
as UG, explained you need to Loop through all textbox.
add code to the "textbox" (let say, Textbox1) AfterUpdate:
Code:
Private Sub Textbox1_AfterUpdate()
Call Form_Current
End Sub

Private Sub Form_Current()
Dim ctl As Control
For Each ctl In Me.Controls
    If TypeOf ctl Is Textbox Then
        If ctl.Name <> Me!textbox1.Name Then
            ctl.Enabled = Nz(Me!Textbox1, "B") = "B"
        End If
    End If
Next
End Sub
 
Last edited:

missinglinq

AWF VIP
Local time
Today, 09:19
Joined
Jun 20, 2003
Messages
6,423
Since nobody else mentioned it...the problem with your code lies here:

Me.textbox2.disabled = True

Notice that Intellisence didn't capitalize the 'd' in disabled! That's because there is no 'disabled' Property!

Arnelgp's codes shows the correct Property here

ctl.Enabled = Nz(Me!Textbox1, "B") = "B"

Enabled = Yes/True or Enabled = No/False

Linq ;0)>
 

JPR

Registered User.
Local time
Today, 06:19
Joined
Jan 23, 2009
Messages
192
Thanks to both for your help. However I am still having some problem. Whatever selection I type in texbox1, I textboxes are disabled.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:19
Joined
Sep 21, 2011
Messages
14,052
You need to show your code (within code tags as well), so people can see exactly what you have now?
 

JPR

Registered User.
Local time
Today, 06:19
Joined
Jan 23, 2009
Messages
192
Sorry. I will try to be more accurate.
In my form, I have placed a certain number of textboxes. The form has a query as record Source.

When I open the form, a first textbox (texbox1) can be used to set two values: A or B.
If users type A, then all textbox2 and textbox3 should not be disabled, allowing users to enter data.
If users type B, then textbox2 and textbox3, should be disabled.

In both cases, textbox1 should obviously remain not disabled as users may change the data.
I have used the code quoted below, that kindly arnelgp posted but could not make it work. Thank you


< Private Sub Textbox1_AfterUpdate()
Call Form_Current
End Sub

Private Sub Form_Current()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is Textbox Then
If ctl.Name <> Me!textbox1.Name Then
ctl.Enabled = Nz(Me!Textbox1, "B") = "B"
End If
End If
Next
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:19
Joined
May 7, 2009
Messages
19,175
it is reversed logic as you have in post#1,
therefore change this:

ctl.Enabled = Nz(Me!Textbox1, "B") = "B"

To:

ctl.Enabled = Nz(Me!Textbox1, "A") = "A"
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:19
Joined
Sep 21, 2011
Messages
14,052
Arnel put all that code into code tags, but you cannot be bothered? :(
 

JPR

Registered User.
Local time
Today, 06:19
Joined
Jan 23, 2009
Messages
192
got it. Working perfectly now. Thanks a have a great weekend.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 13:19
Joined
Jul 9, 2003
Messages
16,245
Arnel put all that code into code tags, but you cannot be bothered? :(

I have given up asking people to put their code in code tags as it started getting under my skin. I started being rude to people that didn't bother to enclose their code in code tags, and that's not good!

All I do now is, if I know someone is a persistent offender, I don't bother answering their questions.
 

JPR

Registered User.
Local time
Today, 06:19
Joined
Jan 23, 2009
Messages
192
Arnel put all that code into code tags, but you cannot be bothered? :(
Sorry Gasman. It's not a matter of being or not being bothered or wanting to be rude. It's not my style. It's years I have been using this forum and can only express my gratitude. As I like to say, I am only learnings and just don't know how to! You are referring to an icon.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:19
Joined
Sep 21, 2011
Messages
14,052
It does help others to help you though?
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:19
Joined
Sep 21, 2011
Messages
14,052
I have given up asking people to put their code in code tags as it started getting under my skin. I started being rude to people that didn't bother to enclose their code in code tags, and that's not good!

All I do now is, if I know someone is a persistent offender, I don't bother answering their questions.
Well it annoys me to, but if one is not advised of it, how can they know? Now if they choose to ignore it, that is a whole new ballgame😀
 

Users who are viewing this thread

Top Bottom