I have a multipage userform including entry details page and am using "NEXT" button to move onto next pages. ( There are 5 question pages + 1 entry detail page)
Here is the code that I am using to navigate.
If QuestionairePages.value < QuestionairePages.Pages.Count - 1 Then
QuestionairePages.value = QuestionairePages.value + 1
End If
Is there a way to disable the moving to the next pages unless all options buttons selected on specific pages?
I don't have enough information to write exactly what you need, but you can try with the .visible command and with the boolean on the option button like this :
If YourOptionButton1 = true then
YourButtonNext.Visible = False
End if
Related
I need a code that loops through options in a < select > object on a web page, selects each option and clicks "Show" button to show some data related to the selected option. I started with this code:
Set periodSelector = ie.document.getElementById("period")
For Each Period In periodSelector.Options
Period.Selected = True
Application.Wait (Now + TimeValue(waittime))
Next Period
It works well - the browser selects each option just fine. But when I add button.click to show the data related to the selected option, an error "Permission denied" occurs on the second selector loop (seems like it cannot use .select command anymore).
Set periodSelector = ie.document.getElementById("period")
For Each Period In periodSelector.Options
Period.Selected = True
Application.Wait (Now + TimeValue(waittime))
ie.document.getElementsByTagName("input")(17).Click
Application.Wait (Now + TimeValue(waittime))
Next Period
I guess it is due to same origin policy. Probably, when I click on the "Show" button, the page gets refreshed (although it is not really reloaded - the button uses scripts to retrieve some information and show it in a table below the button).
How can I avoid this same origin policy issue and loop through the dropdown options?
In cases like this I try a slightly different approach which is to work off the page and not a variable. You can get your number of options from an initial variable, but after that keep working off the current document which may have refreshed. Amongst other things, you want to avoid underlying stale element exceptions.
Dim periodSelector As Object, i As Long, optionsLength As Long
Set periodSelector = ie.document.querySelectorAll("#period option")
optionsLength = periodSelector.Length -1
For i = 0 to optionsLength
ie.document.querySelectorAll("#period option").item(i).Selected = True
Application.Wait Now + TimeValue(waittime)
ie.document.getElementsByTagName("input")(17).Click
Application.Wait Now + TimeValue(waittime) '<== this I would replace with proper page load wait While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Next
Hey, I have an UserForm into which an user inputs varius profile
fields.
Once it's filled in, there is verification - if anything goes astray,
the CommandButton, named save_button is disabled
What I want to achieve is: If user clicks on the button, whilst it is in the disabled state, to display a MsgBox saying he needs to correct the incorrectly filled in field
For Demonstration purposes, I'm not gonna paste here the validation procedures, so let's just pretend that the save_button.Enabled = False is set from the getgo. Produces the same result.
save_button.Enabled = False ' already ran before, pretend this executes it
Private Sub save_button_Click()
If save_button.Enabled = False Then
MsgBox "Clicked disabled button"
End If
End Sub
Issue is, once a CommandButton is set to .Enabled = False then it can no longer be officially clicked (hence it can't even trigger the Click() procedure)
My next thought was to use the MouseUp as a substitute. Issue is,
this triggers on any miniscule movement over the button and I don't want to bombard the user with MsgBoxes
Can you think of any alternatives, as to how to detect if user clicked the disabled button?
When a control is disabled, the click event bubbles up the tree. In your case, I guess the user form will get the click instead. If you put your save button inside a frame, that will get the click if the button is disabled. It is fairly easy to make the frame invisible, by setting
Caption to ""
BorderStyle to fmBorderStyleNone
SpecialEffect to fmSpecialEffectFlat
And then size the frame so that is the same size as the button.
The code is easy:
Private Sub YourNewFrame_Click()
MsgBox "Save button disabled!"
End Sub
Tip: If you draw the frame, cut your button and paste it inside your new frame, it will be placed properly. Properly, as in in the right part of the hierarchy. Visually, you will have to do manually.
I want to offer another approach based on my comment to your question.
In the below gif, you will see that the 'Create Form' does not become enabled until all the necessary information is provided. You don't see it in the gif so much, but each entry that requires validation also has it behind the scenes in code.
The essential code behind that action is this:
Sub checkFields()
Select Case True
Case Len(Me.formTitleLine1) = 0, Len(Me.formPrefix) = 0, Len(Me.formNumber) = 0, Len(Me.formProduct) = 0, Len(Me.formEditionMonth) = 0, Len(Me.formEditionYear) = 0
Me.createForm.Enabled = False
Case Else
Me.createForm.Enabled = True
End Select
End Sub
And it's called on the afterUpdate event of each relevant box:
Private Sub formEditionYear_AfterUpdate()
checkFields
End Sub
With one small change to the checkFields sub, you can only show the save button once everything has been filled in correctly. That change would be:
Me.createForm.Visible
instead of
Me.createForm.Enabled
I have a user form with multipage. I would like to have the tabs of a multipage visible on top, so the user knows on which page he is and how many there are left. But I would like to disable the possibility to jump between the tabs by pressing on the tab name. Instead only next and back buttons would be available to navigate between the pages. Is it possible just by using the properties, or do I have to write the code for the mulipage change?
create form with one multipage control
then add this code ... click form background to see result
Private Sub UserForm_Click()
If UserForm1.MultiPage1.Pages(0).Enabled Then
UserForm1.MultiPage1.Pages(1).Enabled = True ' must always have at least one enabled
UserForm1.MultiPage1.Pages(0).Enabled = False ' otherwise it looks bad
Else
UserForm1.MultiPage1.Pages(0).Enabled = True ' reverse statements, and you will see
UserForm1.MultiPage1.Pages(1).Enabled = False
End If
End Sub
I have a userform that needs to display different options based on the user that is opening the form. Some of these options should only be enabled once a selection has been made in a combobox, but I can't seem to find a way to get them to update after a combobox selection is chosen.
What I'm using is:
Private Sub cbCharts_AfterUpdate()
If Me.cbCharts Is "" Then
Me.bQuickEntry.Enabled = False
Me.bView.Enabled = False
Exit Sub
ElseIf UserDep = "Quality Control" Then
Me.bQuickEntry.Enabled = True
Me.bView.Enabled = True
Me.bAdjust.Enabled = True
Else
Me.bView.Enabled = True
Me.bQuickEntry.Enabled = True
End If
End Sub
Where cbCharts is the combobox in question, and bQuickentry, bView and bAdjust are the buttons.
Using this code, the buttons don't enable until I click somewhere else on the form, instead of immediately after making the selection. Then, if I clear the combobox Excel hangs and has to be force closed.
I've tried instead using Private Sub cbCharts_OnExit with the same code, but it doesn't do anything at all.
I know I can just leave all the buttons enabled and visible add a verification step to the code for each button to ensure that there is a valid combobox selection before proceeding, but I would prefer to enable and set their visibility to prevent user confusion, as some of these buttons will not be usable by the majority of spreadsheet users.
What am I doing wrong?
Try putting your code into the cbCharts_Click() event function instead. This event is fired as soon as the user selects an item in the list.
I have just added in Calendar 12.0 from the tools > Additional Controls. Calendar works fine and I have it spitting the date out to the right cells. What I would like, however, is to really make the calendar visible from a command button as my form contains a bunch of fields and I don't want to bog up the form with this calendar. I have tried Calendar1.show but the .show isn't an option.
So ultimately I need a command button to show the calendar, allow the user to select (I have that) and then close the calendar. Any help? I thank you in advance!!
bdubb
In this snippet, CommandButton1 is from the ActiveX controls, not the form controls. It requires that you click the button to show the calendar (which pops up next to the button you clicked), and click the button again to hide the calendar.
Private Sub CommandButton1_Click()
If Not Calendar1.Visible Then
Calendar1.LinkedCell = "A1"
Calendar1.Top = Sheet1.CommandButton1.Top
Calendar1.Left = Sheet1.CommandButton1.Left + Sheet1.CommandButton1.Width + 1
Calendar1.Visible = True
Else
Calendar1.Visible = False
End If
End Sub
Obviously, different buttons would require different linked cells, but it does mean that you could have a single calendar control that it displyed by multiple buttons (if that is what you want).
Unfortunately, it would appear that you cannot hide the control while any of its events are firing (e.g AfterUpdate). It just doesn't want to disappear!!
Hide/Close a calendar control still not works (new year 2015 = almost four years later) but I think I found a workaround to hide the control after firing events.
I have a Calendar1_AfterUpdate(), which launches before Calendar1_Click(). Code is placed directly in a worksheet and NOT in a module.
Private Sub Calendar1_AfterUpdate()
Range("a1") = Me.Calendar1.Value
' Next two lines does not work within AfterUpdate
' When running step by step it seems to work but the control is
' visible when End Sub has run
Me.Calendar1.Visible = True
Me.Calendar1.Visible = False
End Sub
To that I simply added
Private Sub Calendar1_Click()
Me.Calendar1.Visible = True
Me.Calendar1.Visible = False
End Sub
Please note that the control for some reason must be made visible before hiding.
Why it does not work directly in Calendar1_AfterUpdate() is a mystery to me.
Next problem is to hide the control when I remove the mouse. Mouse-events seems to be impossible in a calendar control ...