Making an area un-editable using a checkbox - vba

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

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.

Disable TextInput Object

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

Turn off "The record source specified on this form does not exist" warning

how can I turn off/disable "The record source specified on this form does not exist" warning message? I created GUI that uses subforms, but the tables that are resource for these are generated in the process. So it means that GUI does not have any resource after opening the database and shows the error "The record source specified on this form does not exist". How can I turn it off/disable it? I tried to add DoCmd.SetWarnings False
but this has no effect on the error message.
You could leave the RecordSource empty or assign it some (empty) dummy record.
When you set the RecordSource to its actual value, the subform will automatically requery.
You need to look at Application.DisplayAlerts
Sub Example()
'do stuff
Application.DisplayAlerts = False
'Code that fires the warning message
Application.DisplayAlerts = True
'do stuff
End Sub
Instead of disabling alerts you can just assign some unrelated table to this form/subform.
Another solution - disable only this exact error if you have an error number (I guess it is 7874) like that:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 7874 Then
Response = acDataErrContinue
Else
Response = acDataErrDisplay
End If
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.

how to test if a particular control has focus?

i have access 2007 form and i want to test if a particular control (toggle button) has the focus ,
something like :
if gotfocus(mytoggle) then
dosomething
endif
or maybe like :
if me.mytoggle.setfocus = true then
dosomething
endif
I have searched and cannot find this , can someone tell me what is correct top to do this ?
This for the current form:
If (mytoggle Is Me.ActiveControl) Then
This for the current Access.Application:
If (mytoggle Is Screen.ActiveControl) Then
Be careful, if no control has focus, *.ActiveControl may not exist.
Try this code - I've tried to account for .ActiveControl not existing.
Private Function isCurrentControl(thisControl As Control) As Boolean
On Error GoTo err_handler
If Not Me.ActiveControl Is Nothing Then
If (Me.ActiveControl Is thisControl) Then
isCurrentControl = True
Else
isCurrentControl = False
End If
Else
GoTo err_handler
End If
close_function:
On Error GoTo 0
Exit Function
err_handler:
isCurrentControl = False
Resume close_function
End Function
You just need to call the function and set the control as a parameter
''EXAMPLE: isCurrentControl(mytoggle)
Unfortunately, there are situations where the .ActiveControl is temporary non-existing ! When records are scrolled in a form, the procedure Form_Current() gets run. Already at the beginning, there is no focus anymore – the focus is reset to the previous field only after Form_Current() has terminated.