How to select/open a page available under a Multipage (Userform) using VBA code - vba

I am trying to prompt a message to a user to select at least one checkbox available under each Page's (Multipage option) on a Userform and navigate him to that page to select the checkbox
Code
'this will check whether any checkbox selected, if not it will prompt to select any one
If UF1_Revenue_Location_Role_Srch.Revenue_Loc_CheckBox1.Value = True And Not _
(UF2_Revenue_DailyRate.MultiPage1.Page1.Revenue_Ind_CheckBox1.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page1.Revenue_Ind_CheckBox2.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page1.Revenue_Ind_CheckBox3.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page1.Revenue_Ind_CheckBox4.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page1.Revenue_Ind_CheckBox5.Value = True) Then
MsgBox "To Proceed: Kindly select atleast one Designation under Location India"
UF2_Revenue_DailyRate.MultiPage1.Page1.Show
Call UserForm_Initialize
Else
If UF1_Revenue_Location_Role_Srch.Revenue_Loc_CheckBox2.Value = True And Not _
(UF2_Revenue_DailyRate.MultiPage1.Page2.Revenue_Ger_CheckBox1.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page2.Revenue_Ger_CheckBox2.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page2.Revenue_Ger_CheckBox3.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page2.Revenue_Ger_CheckBox4.Value = True _
Or UF2_Revenue_DailyRate.MultiPage1.Page2.Revenue_Ger_CheckBox5.Value = True) Then
MsgBox "To Proceed: Kindly select atleast one Designation under Location Germany"
UF2_Revenue_DailyRate.MultiPage1.Page2.Select
Call UserForm_Initialize
On above code, I try to open the page using select option and Show option
UF2_Revenue_DailyRate.MultiPage1.Page1.Show
UF2_Revenue_DailyRate.MultiPage1.Page2.Select
but none of them really works.
Is there any other way to open this page to the user so that they can easily select the checkbox from it instead of selecting the page one by one?
Thanks in advance....

You can set the Attribute Value of your Multipage:
UF2_Revenue_DailyRate.MultiPage1.Value = 0 'Page1
UF2_Revenue_DailyRate.MultiPage1.Value = 1 'Page2
Remember that it starts with 0

Related

How to send focus to a text box of a VBA form during its initialization/activate event?

I have a VBA form in Corel. Behaving exactly like a similar one in Excel...
Initially, when the form initialize event used to contain only some lines of code, the simple ending line me.txtCsv.Setfocus used to send the focus on it. I mean, it appeared being in edit mode with the cursor blinking inside.
After a period of time, after the application became complex, I am not able to send the focus to the text box in discussion.
I know that Activate event goes last and I also have in it the line me.txtCsv.Setfocus. But without expected result. Inside the Initialization event code I inserted that line Debug.Print Me.ActiveControl.Name & " - 1", changing 1 in 2, 3 up to 6 in many places, including the last line and all the time the name of the text box in discussion (txtCsv) appears in Immediate Window.
So, the control in discussion is the activate one, but the cursor is not inside it when the form is loaded.
TabStop is set to True. I set the TabIndex to 0.
The control is enabled and not blocked. I have created a new simple form with three text boxes and it works well.
I mean the text box which I want to send the focus, has the focus when the form is loaded, keeping a similar code in its Initialize or Activate events.
I compared all properties of the two forms and all text box controls and they are the same...
When I send the focus from another control on the form, the text box in discussion receives it.
It does not receive the focus (anymore) only when the form is shown, the focus being sent by Initialize or Activate evens.
Events code:
Private Sub UserForm_Activate()
Me.txtCsv.SetFocus
End Sub
Private Sub UserForm_Initialize()
Dim P As Printer, i As Long, NrImp As Long, prDefault As String, strJustEngr As String
Dim Printers() As String, n As Long, s As String, boolFound As Boolean
Dim strEng As String, MEngr As Variant, m As Variant, el As Variant, defSize As String
Dim strDropbox As String
boolOpt = True: boolFound = False
Me.cbPrinters.Clear
If Me.chkNewStyle.Value = True Then boolNewStyle = True
prDefault = Application.Printers.Default.Name
strEng = GetSetting(ECA_K, ECA_set, ECA_Engr, "No settings...")
If strEng <> "No settings..." Then
boolSelectedEngravers = True ' only adding engraver is possible...
MEngr = Split(strEng, "|")
'Incarcare in combo:
Me.cbPrinters.Clear
For Each el In MEngr
m = Split(el, ":")
Me.cbPrinters.AddItem m(0)
If m(0) = prDefault Then
boolFound = True
defSize = m(1)
End If
Next
Me.cbPrinters.Value = Me.cbPrinters.List(0)
With Me.btChoosePrinters
.Caption = "Add an Engraver"
.ControlTipText = "Add another Engraver(must be installed)"
End With
Me.btEliminatePrinters.Enabled = True
Me.lblPrinters.Caption = "Engravers: "
Me.cbPrinters.ControlTipText = "Select Engraver to be used!"
Else
Printers = GetPrinterFullNames()
For n = LBound(Printers) To UBound(Printers)
Me.cbPrinters.AddItem Printers(n)
If Printers(n) = prDefault Then boolFound = True
Next n
boolSelectedEngravers = False
End If
Debug.Print Me.ActiveControl.Name & " - 1"
If boolFound Then
Me.cbPrinters.Value = prDefault
Else
Me.lblStatus.Caption = "The default printer (""" & prDefault & """) is not a laser Engraver..."
End If
If GetSetting(ECA_K, ECA_set, "LowRAM", "No settings...") <> "No settings..." Then
boolLowRAM = CBool(GetSetting(ECA_K, ECA_set, "LowRAM", "No settings..."))
End If
If boolLowRAM = True Then
Me.chkLowRAM.Value = True
Else
Me.chkLowRAM.Value = False
End If
Debug.Print Me.ActiveControl.Name & " - 2"
'Direct engrave setting:
Dim strDirectEngrave As String
strDirectEngrave = GetSetting(ECA_K, ECA_set, ECA_Direct_Engrave, "Nothing")
If strDirectEngrave <> "Nothing" Then
Me.chkDirectEngrave.Value = CBool(strDirectEngrave)
If CBool(strDirectEngrave) = True Then
boolDirectEngrave = True
Else
boolDirectEngrave = False
End If
End If
'_______________________________________
strJustEngr = GetSetting(ECA_K, ECA_set, ECA_Just_Engrave, "Nothing")
If strJustEngr <> "Nothing" Then
'Application.EventsEnabled = False
boolChangeEngr = True
Me.chkJustEngrave.Value = CBool(strJustEngr)
boolChangeEngr = False
'Application.EventsEnabled = True
If CBool(strJustEngr) = True Then
Me.chkDirectEngrave.Enabled = True
boolJustEngrave = True
Me.frLocFoldPath.Enabled = True
Else
Me.frLocFoldPath.Enabled = False
Me.chkDirectEngrave.Enabled = False
End If
End If
Debug.Print Me.ActiveControl.Name & " - 3"
If boolSelectedEngravers Then
Application.EventsEnabled = False
Me.btGo.ForeColor = RGB(45, 105, 7)
Me.txtCsv.BackColor = RGB(153, 255, 51)
Me.btGo.Enabled = False
Me.txtCsv.SetFocus
Application.EventsEnabled = True
End If
strDropbox = GetSetting(ECA_K, ECA_set, ECA_Dropbox, "No value")
If strDropbox <> "No value" Then
If CBool(strDropbox) = True Then
Me.chkDropbox.Value = True
End If
End If
AllRefresh
Me.chkCloseDoc.Value = True
Me.txtCsv.SetFocus
Debug.Print Me.ActiveControl.Name & " - 4"
End Sub
Private Sub AllRefresh()
Application.Optimization = False
Application.EventsEnabled = True
If Documents.Count > 0 Then
ActiveWindow.Refresh
ActiveDocument.PreserveSelection = True
End If
Application.Refresh
End Sub
Is there something else, crossing your mind, to be tested?
In the meantime I did some more tests, respectively:
I created a new project (.GMS file) and I imported the form in discussion.I started commenting all the Initialize event code, except the last two code lines.
It didn't set the focus! Commenting everything, letting only the Activate event code, it worked.
I started to un-comment lines in Initialize event code and I found a line not allowing the focus to be sent to that text box.
Setting the value of the combo: Me.cbPrinters.Value = Me.cbPrinters.List(0), moving it in the Activate event code, before the part pointing to txtCSV, worked well.
Now, I tried to do the same in the original form and it does not work...
The above question has been solved by Disabling followed by Enabling of the text box in discussion, but only doing that in Form Activate event. It did not work in Initialize event...
Private Sub UserForm_Activate()
Me.txtCsv.Disable: Me.txtCsv.Enable
Me.txtCsv.SetFocus
End Sub

VBA Expand/Collapse rows

I have a report in which I am asking the users to click buttons to reveal where they need to add their commentary. I have it working but wanted to put in an If statement in case they have already expanded the row.
I have two macros, the first relates to the button they push and sends to the main macro the name of the button and a row number which is part of the section that is either expanded or collapsed
Sub ROccupancy()
'
Dim RecName As String
RecName = "ROccupancy"
Dim RowNum As Integer
RowNum = 27
Call ToogleRec(RecName, RowNum)
End Sub
The next macro is where I am having the trouble
Sub ToogleRec(RecName, RowNum)
'
Dim Toogle As String
Dim MyObj As Object
Set MyObj = ActiveSheet.Shapes.Range(Array(RecName))
Toogle = Left(MyObj.TextFrame2.TextRange.Characters.Text, 4)
TextName = Mid(MyObj.TextFrame2.TextRange.Characters.Text, 5, 100)
If Toogle = "Show" Then
MyObj.ShapeStyle = msoShapeStylePreset9
MyObj.TextFrame2.TextRange.Characters.Text = _
"Hide" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = False Then
Rows(RowNum).ShowDetail = True
End If
Else
MyObj.ShapeStyle = msoShapeStylePreset11
MyObj.TextFrame2.TextRange.Characters.Text = _
"Show" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = True Then
Rows(RowNum).ShowDetail = False
End If
End If
Range("C" & RowNum).Select
End Sub
The issue is the Rows(RowNum).ShowDetail is always TRUE, no matter if it's expanded or collapsed. I can remove the If section and set it to TRUE or FALSE using "Rows(RowNum).ShowDetail = False" or "Rows(RowNum).ShowDetail = TRUE". However, if the user has manually expanded or collapsed the row it causes an error (which freaks them out)
This question and answer seemed promising but Rows(RowNum).ShowDetail always seems to be TRUE
I put the MsgBox in there for error checking. I'll remove it in the final version.
Have you tried using Hidden property? Something like:
With Sheet1.Rows(5)
.ShowDetail = .Hidden
End With
Take note though that for you to use .ShowDetail method, you'll need to Group the rows first (needs to be in outline form).
True if the outline is expanded for the specified range (so that the detail of the column or row is visible). The specified range must be a single summary column or row in an outline.
Above code toggles hiding/unhiding a grouped row 5. You don't even need an If statement for the toggling. HTH.

How can I make labels invisible on load in Access 2007

I've got a logger form which hides the text and combo boxes until you hit the "start activity" but is there anyway I can also make the labels disappear on load also and then also appear once a start button is pressed?
Here is a sample of my code:
Private Sub Form_Load()
strUserID = Environ("USERNAME")
AppVersion = "Version - " & DLookup("Version", "Version", "ID = 1") & " - " & DLookup("VersionDate", "Version", "ID = 1")
Me.txtVersion = AppVersion
Me.cmdStartCall.Visible = True
Me.txtPolicyClaimReference.Visible = False
Me.txtJobReference.Visible = False
Me.txtNotes.Visible = False
Me.CboContactMethod.Visible = False
Me.CboTitle.Visible = False
Me.CboDepartment.Visible = False
Me.CboLocation.Visible = False
Me.txtScheme.Visible = False
Me.txtFirstName.Visible = False
Me.txtSurname.Visible = False
End Sub
So if we assume that each txt or cbo box has the same name label, how can i get them not to show unless activated?
Thanks
Dan
The labels must be attached (associated) to their textboxes/comboboxes, then they are hidden automatically with them.
To attach separated labels:
Select the label
Issue the Cut command
Select the control to which you want to attach the label
Issue the Paste command.
(from http://www.consultdmw.com/access-control-labels.htm)

How to create expandable/retractable form MS Access 2013 VBA?

I am creating a data entry form for one of my database tables. For one of the sections, I have the text field with ONLY the caption: "Description 1" showing. If the Description 1 textbox is filled out by the user, I want it to show the Description 2 textbox. If the user fills out the Description 2 textbox, the Description 3 textbox will show up and so on up to 10 Description textboxes. Is there a way to hide the extra text boxes kind of like when you fill out the information while creating a macro? For example when you click Create --> Macro, there is only a dropdown box for you to select an action. If you choose Open Form and hit enter, 6 more text boxes with captions appear.
Is there a way to get that kind of functionality in a form? Also, in the Macro builder, it dynamically rearranges the page for you, can this also be done with the form?
Follow these steps:
Mark the visible property as false
Add OnChange event for each textbox.
Write the VBA code to determine if the next control will be showed or hide. Note, the Me!FormControlItem.Text is accessible only if the control is focused.
There is the 3 functions for each control.
Private Sub text1_Change()
If Not Trim(Me!text1.Text) = "" Then
Me!Text2.Visible = True
Me!Label2.Visible = True
ElseIf Not Trim(Me!Text2) = "" Then
Me!Text2.Visible = True
Me!Label2.Visible = True
Else
Me!Text2.Visible = False
Me!Label2.Visible = False
End If
End Sub
Private Sub Text2_Change()
If Not Trim(Me!Text2.Text) = "" Then
Me!Text3.Visible = True
Me!Label3.Visible = True
ElseIf Not Trim(Me!Text3) = "" Then
Me!Text3.Visible = True
Me!Label3.Visible = True
Else
Me!Text3.Visible = False
Me!Label3.Visible = False
End If
End Sub
Private Sub Text3_Change()
If Not Trim(Me!Text3.Text) = "" Then
Me!Text4.Visible = True
Me!Label4.Visible = True
ElseIf Not Trim(Me!Text4) = "" Then
Me!Text4.Visible = True
Me!Label4.Visible = True
Else
Me!Text4.Visible = False
Me!Label4.Visible = False
End If
End Sub
Enjoy!

How to select multiple dates from UserControl calendar in Excel?

I have one userForm Calendar Control in Excel which works fine for a single date. Here is the code:
If Range("C4").Value = "" Then
' do nothing'
Else
If IsDate(Range("C4").Value) = True Then
Load frmCustomCalendarControl
frmCustomCalendarControl.SelectedDayNumber = Day(Range("C4").Value)
frmCustomCalendarControl.SelectedMonthNumber = Month(Range("C4").Value)
frmCustomCalendarControl.SelectedYearNumber = Year(Range("C4").Value)
End If
End If
frmCustomCalendarControl.Show
If frmCustomCalendarControl.SelectedDayNumber = 0 And _
frmCustomCalendarControl.SelectedMonthNumber = 0 And _
frmCustomCalendarControl.SelectedYearNumber = 0 Then
' user click on the cancel button in the calendar control therefore do nothing '
Else
Range("C4").Value = DateSerial(frmCustomCalendarControl.SelectedYearNumber, _
frmCustomCalendarControl.SelectedMonthNumber, _
frmCustomCalendarControl.SelectedDayNumber)
' the following properties are also available from the customer control if you need them
Debug.Print "frmCustomCalendarControl.SelectedDateDDMMYYYY = " & frmCustomCalendarControl.SelectedDateDDMMYYYY
Debug.Print "frmCustomCalendarControl.SelectedDayString = " & frmCustomCalendarControl.SelectedDayString
Debug.Print "frmCustomCalendarControl.SelectedMonthString = " & frmCustomCalendarControl.SelectedMonthString
Debug.Print "frmCustomCalendarControl.SelectedYearString = " & frmCustomCalendarControl.SelectedYearString
End If
Unload frmCustomCalendarControl
This is the code to Run calendar UserForm and select the date.
Now I want to select multiple dates using the same user Control.
It's not clear what calendar control you use. I cannot find any calendar control having properties which are seen in your code. For testing purposes I created a MonthView control. In the properties of MonthView you can see:
MaxSelCount, which represents the maximum number of days to select;
MultiSelect - if it's True, you can have more than 1 date selected.