Compile error with If statement on radiobutton.value - vba

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.

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

VBA code for unhiding a bookmarked text is not working

I've created an ActiveX dropdown list and each option is linked to a bookmark for the text. Under the ActiveX controls there are the bookmarks (R1 andR2), hidden.
When I hit the btnselect button, all the other bookmarks, except the selected one, get deleted and the selected one becomes visible.
In the bookmark R2
I have a MacroButton for showing/hiding another text (CollapseMentiuniReclamant). When clicking the button it runs either Expand1 sub or Collapse1 sub, but the bookmark CollapseMentiuniReclamant doesn't show up.
I've simplified the document and codes as much as possible. Link to the document - https://wetransfer.com/downloads/1caea3c5d3b05e226e8b8f6a29760ad220190522071742/15db59.
The vba code is:
Private Sub btnselect_Click()
If ComboBox1.Value = "1" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("R2").Range.Delete
End If
If ComboBox1.Value = "2" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R1").Range.Delete
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End If
End Sub
Sub Expand1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Collapse1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
Sub Collapse1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Expand1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End Sub
Update: I've simplified the last part of code and the problem still persists:
Sub Expand1()
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
I've even removed the button entirely and ran the macro from View Macros Tab and it's not working.
Why doesn't CollapseMentiuniReclamant show up?
It's not showing up because what you're trying to hide/unhide isn't inside the bookmarked range. In any event, you should be inserting/deleting the content, not simply toggling it's hidden property. Making something hidden is no guarantee it won't be seen or printed (even if not seen), as those settings depend on how the end user has Word configured.

Number only text box not working on one of two text boxes in a userform

I have two text boxes on a userform that I would like to be numeric only. The first one works fine based on this( Link), however the second one, which I have implemented in exactly the same way as the first is not working, and I don't know why. Any idea why?
The first textbox is call TextBoxMainVal
The second is called perHour
Code:
'If the Main Value box does not recieve a number send a message to make them change it
Private Sub TextBoxMainVal_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBoxMainVal.Value = "" Then
ElseIf Not IsNumeric(TextBoxMainVal.Value) Then
MsgBox "Enter numbers only"
Cancel = True
TextBoxMainVal.Value = vbNullString
End If
End Sub
'I DONT KNOW WHY THIS ONE ISNT WORKING!
Private Sub perHour_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If perHour.Value = "" Then
ElseIf Not IsNumeric(perHour.Value) Then
MsgBox "Enter numbers only"
Cancel = True
perHour.Value = vbNullString
End If
End Sub
I thought there could be a naming error conflict, so I changed the textbox name, but that did not resolve it.
I cant understand why it is not working. What am I overlooking?
For those who are interested why this may happen there is an answer here. The reason, it appears, the textbox exit handler is not occurring is because it is outside of a frame. For the exit handler to work you need to stay inside the frame.

Userform disable all fields

I have a userform that can be filled in but only if a previous userform was filled in as there are calculations in the userform based on the previous input and if those are empty, the calculations crash.
Now I did write a few if statements that check for these empty values and then had a brain flash.. how about if one of the fields is missing, the userform is just disabled. So thought, so done and it works :)
if DP1 = "" then
reportback.enable = false
else
end if
That is the form and it shows up beautifully and nothing can be changed, but Ohhh.. you canĀ“t even close the form, nothing works .. lol.
So my question. Is there a way to disable all fields from any input but still have the cancel button active ?
Private Sub Cancel_Click()
Unload reportback
End Sub
Use below code to disable all controls on your form to avoid the issue.
UserForm1 refers to name of Userform kindly replace accordingly.
Dim ctrl As Control
For Each ctrl In UserForm1.Controls
ctrl.Enabled = False
Next
Set ctrl = Nothing