Disable TextInput Object - vba

I have a checkbox and if the checkbox.Value = False I want to disable my TextInput-Object. On the internet there where some suggestions, but the methods which were used are not working for me, because the methods are not found.
I tried it with the .Valid-method:
Dim tf As TextInput
Dim checkbox As CheckBox
Sub checkbox_click()
Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
Set tf = ActiveDocument.FormFields("textfield").TextInput
If checkbox.Value = False
tf.Valid = False
End If
End Sub
But that doesn't work for some reason. I found tf.Enabled = False on the internet, but this method is unknown in my case.

You need something more like this:
Dim ff As FormField
Dim checkbox As CheckBox
.
.
Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
Set ff = ActiveDocument.FormFields("textfield")
If checkbox.Value = False
ff.Enabled = False
End If
With legacy FormField objects, some of the properties you need are associated with the FormField itself, and others are associated with child objects of the FormField such as the FormField.Checkbox
So the problem here is that tf is a FormField.TextInput object, but .Enabled is a property of the FormField object.
Not relevant to your question, but just as an observation, FormFields do not have Word events associated with them in the normal sense of VBA Events. The settings of each field tell Word to run a named Sub "on entry" and/or "on exit" - that's it. No actual click events. No problem with using names that makes those things look like events but I just thought I'd mention it.

Whilst the internet can be helpful you should also use the Object Browser in the VBE along with Help. 5 seconds spent doing a search for FormField would have given you the answer.
Dim tf As TextInput
Dim checkbox As CheckBox
Sub checkbox_click()
Set checkbox = ActiveDocument.FormFields("checkbox").CheckBox
If checkbox.Value = False
ActiveDocument.FormFields("textfield").Enabled = False
End If
End Sub

Related

Hide a Form tab depending on the value of a field

pretty simple question here in scope.
Question:
Wondering If I would be able to hide the tabs of a form based off the values of a table's fields.
I have been reading the 2019 Access Bible and so far it is still unclear to me how I would write the VBA module to constantly be running. Im asking the question a little early in my attempts, but hoping I can ask this well enough to get a head start.
I dont quite understand the execution model of VBA for access yet. I have prior expierence coding but this will be my 1st time in access. Just looking for a little help on how to write the function scope. You see below that I think it should be "Main" something, as I want it to run whenever in form view. Like I know how to write Private sub functions to respond to a button click. But not for a function that just runs in the background of the form.
I guess the real question is when to execute? Right? Any suggestions?
I was thinking something along the line of this below.
Main function()
If Me.FieldProcess_Water = "1" Then
Me.TabProcess_Water.Visible = True
Else
Me.TabProcess_Water.Visible = False
End If
End Sub
This requires some setup to reduce redundant code but should do what you want.
First you'll need your checkbox names and page names to be similar. In my example I have the page names as Tab_Name and the checkboxes as just Name. eg. Tab_Process Water and Process Water.
Then Create a sub called Form_Current() in the form's code-behind.
Private Sub Form_Current()
Dim chk As Control
For Each chk In Me.Controls
If chk.ControlType = acCheckBox Then
If chk = False Then
'Change TabCtl0 to whatever your's is called
Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = False
Else
Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = True
End If
End If
Next
End Sub
This will iterate through the current record's checkboxes and toggle it's respective tab's visibility.
To have the CheckBox update the tabs as they are clicked:
Private Sub Form_Load()
Dim chk As Control
Dim prop As Variant
For Each chk In Me.Controls
If chk.ControlType = acCheckBox Then
chk.OnClick = "=Check_Click()"
End If
Next
End Sub
This will assign a command to each checkbox.
Dim caller As String
caller = Me.ActiveControl.Name
If Me.ActiveControl.Value = False Then
Me.TabCtl0.Pages("Tab_" & caller).Visible = False
Else
Me.TabCtl0.Pages("Tab_" & caller).Visible = True
End If
This will hide the relevant tabs.

Making an area un-editable using a checkbox

I have a word-document and want to make a specific area uneditable (greyed out) if the value of a checkbox is true. My problem is that there are some errors, which I am unable to fix it by myself, hopefully you can help me out with it.
Sub checkBoxStatus()
'declare variables
Dim cb3Y As CheckBox
Dim cb3N As CheckBox
Set cb3Y = ActiveDocument.FormFields(1).CheckBox
cb3Y.Value = False 'just needed this for debugging, to see if I got the right checkbox
End Sub
I got an error message all the time when running this code fragment. "Runtime Error '5941' The Requested Member of Collection Does Not Exist". Unfortunately I don't know where I can edit the id of the right checkbox I need.
There is no CheckBox collection. Use something like:
Sub checkBoxStatus()
With ActiveDocument
If .FormFields(1).CheckBox.Value = True Then
' code for true here
Else
' code for false here
End If
End With
End Sub

Enabling/disabling content control text boxes in word 2016

I am trying to create a form that requires text boxes to be disabled until a checkbox is ticked. Previously I have been able to use activeX and Legacy tools in the document but after a software upgrade to a 3rd party application the manufacture no longer supports these. I am therefore having to re-learn how to use content control boxes and their VBA controls.
This is my attempt based at varying google responses. "Lowers" is the checkbox. "Long3""Long4" "Lat2""Vert2" are the textboxes I am trying to control.
I had managed to disable the textboxes but this now seems to have been lost.
Any suggestions would be greatly appreciated.
Private Lowers_onChange()
Dim oCC_Lowers As ContentControl
Set oCC_Lowers = ActiveDocument.SelectContentControlsByTag("Lowers").Item(1)
Dim oCC_Long3 As ContentControl
Set oCC_Long3 = ActiveDocument.SelectContentControlsByTag("Long3").Item(2)
Dim oCC_Long4 As ContentControl
Set oCC_Long4 = ActiveDocument.SelectContentControlsByTag("Long4").Item(3)
Dim oCC_Lat2 As ContentControl
Set oCC_Lat2 = ActiveDocument.SelectContentControlsByTag("Lat2").Item(4)
Dim oCC_Vert2 As ContentControl
Set oCC_Vert2 = ActiveDocument.SelectContentControlsByTag("Vert2").Item(5)
If oCC_Lowers.LockcontentControl = True Then
oCC_Long3.LockContents = False
oCC_Long4.LockContents = False
oCC_Lat2.LockContents = False
oCC_Vert2.LockContents = False
Else
oCC_Long3.LockContents = True
oCC_Long4.LockContents = True
oCC_Lat2.LockContents = True
oCC_Vert2.LockContents = True
End If
End Sub
Firstly a link to a good resource on working with Content Controls: Greg Maxey's Word Content Controls page
As your code is an event handler responding to a built-in event, it needs to be in the ThisDocument module. As there isn't a Change event you need to use the OnExit event. You can find this by navigating to the ThisDocument module in the VBE and selecting Document in the left hand drop-down. In the right hand drop-down select ContentControlOnExit. An empty event handler is then created with the correct syntax.
You will note that this event handler will fire for every content control in the document, so you need to test the value of ContentControl.Tag to see if it is the one that you need to work with.
Once you have verified that you have the correct content control you can then use the value of its Checked property to set the value of the LockContents property of each of the other content controls. Your completed code should then look like this:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If ContentControl.Tag = "Lowers" Then
Dim oCC_Long3 As ContentControl
Set oCC_Long3 = ActiveDocument.SelectContentControlsByTag("Long3").Item(1)
oCC_Long3.LockContents = Not ContentControl.Checked
Dim oCC_Long4 As ContentControl
Set oCC_Long4 = ActiveDocument.SelectContentControlsByTag("Long4").Item(1)
oCC_Long4.LockContents = Not ContentControl.Checked
Dim oCC_Lat2 As ContentControl
Set oCC_Lat2 = ActiveDocument.SelectContentControlsByTag("Lat2").Item(1)
oCC_Lat2.LockContents = Not ContentControl.Checked
Dim oCC_Vert2 As ContentControl
Set oCC_Vert2 = ActiveDocument.SelectContentControlsByTag("Vert2").Item(1)
oCC_Vert2.LockContents = Not ContentControl.Checked
End If
End Sub

How do I efficiently pair Toggle Buttons and Textboxes in Access Form?

I know the title is a bit confusing so let me make myself as clear as possible.
In an Access form (2010), I have a set of text fields meant to contain dates. Those fields are all optional, the user can fill in any number of dates.
To make it more user-friendly, I want to associate each field to a toggle button. Then I want 2 things to happen :
On CURRENT event :
If a text field has a value, then it is visible and the toggle button
associated with it is pressed.
If a text field is empty, then it is not visible and the toggle button isn't pressed.
On CLICK of a toggle button :
If the text field associated with it has a value, then this field gets cleared (and invisible) and the toggle button gets de-pressed ;
If the text field associated with it is empty, then the focus is set on it and the toggle button gets pressed (and stay that way if a value is entered in the text field, otherwise everything gets back the way it was, invisible and unpressed).
So far I've achieved the first step by setting two collections of controls based on some examples I found online.
So, when the form is loaded, I call this :
Private mcolGroupTxToggle As New Collection
Private mcolGroupBuToggle As New Collection
Private Sub InitializeCollections()
Dim ctl As Control
If mcolGroupTxToggle.Count = 0 Then
For Each ctl In Me.Controls
If ctl.Tag = "txtoggle" Then
mcolGroupTxToggle.Add ctl, ctl.Name
End If
Next ctl
Set ctl = Nothing
End If
If mcolGroupBuToggle.Count = 0 Then
For Each ctl In Me.Controls
If ctl.Tag = "butoggle" Then
mcolGroupBuToggle.Add ctl, ctl.Name
End If
Next ctl
Set ctl = Nothing
End If
End Sub
And on Form_Current event, I call that :
Private Sub OnLoadToggles(mcol As Collection, pcol As Collection)
Dim ctl As Control
Dim btn As Control
Dim strBtn As String
For Each ctl In mcol
'Every button has the same name than the textbox + "b"
strBtn = ctl.Name & "b"
For Each btn In pcol
If btn.Name = strBtn Then
If IsNull(ctl) Then
ctl.Visible = False
btn.Value = False
Else
ctl.Visible = True
btn.Value = True
End If
End If
Next btn
Next ctl
Set ctl = Nothing
End Sub
Everything works well so far, but I'm not sure that's the best way to do it and I figured I would need to repeat some lines in step 2.
Making the distinction between text boxes and buttons in the procedure seems weird, I feel like it should be done prior so that I don't have to do it in every procedure. I also feel like it would be better to loop through each pair of controls (text + button) instead of each control in both collections.
Basically, I'm wondering if it would be (1) better and (2) possible to have something as simple as this :
Private Sub OnLoadToggles(Collection of pairs)
for each [pair of txt and btn]
if isnull(txt) Then
txt.visible = false
btn.value = false
else
...
end if
...
My guess is that I would need to make a public sub where I set a collection of pairs of buttons and text fields based on their tags (there are other controls in my form that need to be left alone) and names, but I'm not sure how, I'm a beginner in VBA.
Any suggestions please ?
-- Edit step 2 --
Thanks to Andre's answer the second part was easier than I thought. I've updated my sample database. So on click events I call this :
Private Sub ClickToggles()
Dim ctl As Control
Dim btn As Control
Dim strBtn As String
Dim strCtl As String
strBtn = Me.ActiveControl.Name
strCtl = Left(strBtn, Len(strBtn) - 1)
Set ctl = Me(strCtl)
Set btn = Me(strBtn)
If IsNull(ctl) Then
btn.Value = True
ctl.Visible = True
ctl.Enabled = True
ctl.SetFocus
Else
ctl.Value = ""
btn.Value = False
ctl.Visible = False
End If
End Sub
It's not perfect but it works. Probably not a good idea to clear the data at this point because misclicks may happen. It would be better to loop through the textboxes right before saving the form and clear values of invisible and/or disabled controls from the collection. I might to that later.
I had to add the .enabled property next to the .visible one because on lostfocus events I was getting an error saying the control was still active so it couldn't make it not visible.
Right now I'm more concerned about the amount of click and lost focus events. I'd rather have some public functions and event handlers dealing with it but it's getting too complicated for me. I'll get back to it when I know more about... everything.
Suggestions are still welcome anyway =) !
Since your control pairs are "paired" by their name anyway, you don't need any fancy constructs, or even the second collection. Just address the matching control directly by its name.
Instead of using If/Else for setting boolean properties, it is usually easier to assign a variable to the property.
Private Sub OnLoadToggles(mcol As Collection)
Dim ctl As Control
Dim btn As Control
Dim strBtn As String
Dim bShowIt As Boolean
For Each ctl In mcol
'Every button has the same name than the textbox + "b"
strBtn = ctl.Name & "b"
' If you have the control name, you can get the control directly:
Set btn = Me(strBtn)
' Using a variable you don't need If/Else
bShowIt = Not IsNull(ctl.Value)
ctl.Visible = bShowIt
btn.Value = bShowIt
Next ctl
' Note: this is not necessary for local variables - they go automatically out of scope
Set ctl = Nothing
End Sub

Compile error with If statement on radiobutton.value

I'm trying to perform an action 'if a radioboxed have been checked", but I'm getting an error:
Compile error: Method or data member not found.
I've created a userform with four radiobuttons (using Controls toolbox), and a commandbutton. The userform loads in an excelsheet (with the click of a separate button), and it is possible to check the radiobutton. if the radiobutton is checked and I click the command button I want some action to happen, but it wont compile my code.
Private Sub cmdCSV_Click()
Dim JurBen As Integer
With Thisworkbook
If .lblRKinst.Value = True Then
JurBen = 1
MsgBox "hurray"
ElseIf .lblRKkon.Value = True Then
JurBen = 2
ElseIf lblForinst = True Then
JurBen = 3
ElseIf lblForkon = True Then
JurBen = 4
Else: Exit Sub
MsgBox ("Choose an option")
End If
It seems to dislike the "value" statement, which works fine with checkboxes? I've tried with "enabled" and without anything. I seem to be the only person on the internet with this problem...
As I've used loads of time on this tiny issue, and seem to be stuck, any help would be greatly appreciated!
If the Radioboxes are on UserForm then if you want to check their value then 1. the UserForm must be loaded at that time and 2. you need to refer to the UserForm.
Example:
if UserForm1.OptionButton1.Value = true then
The radiobox (OptionButton1 in my example) is member of UserForm and not of ThisWorkbook.
As written by Matteo NNZ I was simply referencing the label and not the radiobutton next to it.
No problem what so ever, as the code above works fine.