VBA: userform multipage template - vba

I'm making a Userform that would allow the user to changes the bounds on several charts at once. I figured out how everything should look on the first multipage, and now I'm wondering if there's any way I could add 11 pages at once to look just like the first page. My userform appears below.
I am still in the design phase for this. If there's a way to do this in the design that would be great. If there's a way to do in the Initialize sub that would also be great.

Based on your intended usage, you should be using a TabStrip instead of MultiPage being most controls within are the same layout (same amount of controls). MultiPage is intended for categorizing data with different controls on each page.
Consider this simple userform to demostrate the benefit of using TabStrip here:
The left side square is a Picture holder I have not put codes for.
With below code to handle tab changes, certain elements in the userform will change when a different Tab is clicked.
Option Explicit
Private Sub TabStrip1_Change()
Dim TabX As String
With Me.TabStrip1
TabX = .Tabs(.Value).Caption
Debug.Print "ActiveTab:", TabX
End With
Me.Frame1.Caption = "Maximum Bounds (" & TabX & ")"
Me.Frame2.Caption = "Minimum Bounds (" & TabX & ")"
Me.TextBox1.Value = "TextBox2 for " & TabX ' Forgot to change this Value to TextBox1 before the screenshot...
Me.TextBox2.Value = "TextBox2 for " & TabX
End Sub
Upon launching the userform (without setting UserForm_Initialize):
Tab2 clicked:
Tab1 clicked:

Related

Multiple text boxes to call the same VBA procedure

I am developing an Access Database and have several forms; all of which have the same text box on them.
The text box for each form is from the same record source, same name, same properties, etc. After the textbox is updated I have VBA running an Instr procedure which captures key phrases commonly used in these text boxes and replaces them with a common phrase.
How can I get each text box from each form to call the same procedure, that way if I have to improve the code over time I am only doing so in one place versus going to each form to update the code.
Example code.
textbox1_AfterUpdate()
Dim A as TextBox
Set A= Me.Textbox1
If InStr(A," Attachment Number ") Then
Me.FunctionalArea.SetFocus
A=Replace(A,"Attachment Number","<<Att."&" "& Left(Me.FunctionalArea).text,1)&""&"XXX>>")
A=SetFocus
End If
If InStr(A, " Program Name ") Then
A = Replace(A, " Program Name ", " <<ProgramNameXX>> ")
End If
If InStr(A, " Office Address ") Then
A = Replace(A, " Office Address ", " <<OfficeAddressXX>> ")
End If
You just call the code with a parameter of the textbox.
Something along the lines of
Public Sub textbox1_AfterUpdate()
DoTextBoxActions Me.Textbox1
End Sub
Public Sub DoTextBoxActions(ByRef ipTextBox As TextBox)
If InStr(ipTextBox.Text, " Attachment Number ") Then
ipTextBox.FunctionalArea.SetFocus
ipTextbox=Replace(ipTextbox.Text,"Attachment Number","<<Att."&" "& Left(ipTextbox.FunctionalArea).text,1)&""&"XXX>>")
ipTextBox.Parent.SetFocus = SetFocus
End If
If InStr(ipTextBox.Text, " Program Name ") Then
ipTextBox = Replace(ipTextBox.Text, " Program Name ", " <<ProgramNameXX>> ")
End If
If InStr(ipTextBox.Text, " Office Address ") Then
ipTextBox = Replace(ipTextBox, " Office Address ", " <<OfficeAddressXX>> ")
End If
End Sub
You can do this.
When you place that text box on each form, in place of building a "event" code stub, you can enter this:
=MyFunctionName()
Or, thus in your case, you would place this code in a standard code module (NOT the forms code module).
Public Function MyGlobalAfterUpdate
' pick up the form and the control
' do this first, do this fast, do this right away
' since a timer event, mouse click, focus change etc. can
' case the screen.ActiveForm, and screen.ActiveControl to change
' once we grab these values, then you ok
Debug.Print "global after"
Dim MyControl As TextBox
Dim MyForm As Form
Set MyForm = Screen.ActiveForm
Set MyControl = Screen.ActiveControl
Debug.Print "Control name = " & MyControl.Name
Debug.Print "Text of control = " & MyControl.Value
Dim strText As String
strText = MyControl.Value
Debug.Print strText
' note that we have FULL use of the form values
' in place of me!Some value, or control?
' you can go
MyForm.Refresh
MyForm!LastUpdate = now()
' save the data in the form
' If MyForm.Dirty = true then MyForm.Dirty = False
End sub
So you are free to do whatever you want in this code. And you simple replace "me" the forms reference with MyForm, but once you grabbed the active form, then anything you would or could do with "Me", you can do the SAME with MyForm. As noted, you could in theory using Screen.ActiveForm, but you are MUCH better to pick up a reference as fast as possible and as soon as possible, since those values and focus could change, and often it will - so get/grab/take a reference to the controls as fast and as soon as possible. Once you grabbed the reference from screen, then minor changes in focus etc. don't matter - since you picked up the form and control right away.
The key concept, the key takeaway? you can grab both the current form with screen.ActiveForm, and you can get/grab the current control that fired the after update event with Screen.ActiveControl.
So, in summary:
Don't create a code stub in the form. In the controls after update event, place the name of the PUBLIC function in a STANDARD code module.
eg like this:
=MyGlobalAfterUpdate()

retrieving data from multiple textboxes created during runtime in an Excel userform using vba

Okay...so I got some great help resolving my issue with adding multiple labels and textboxes to an Excel userform during runtime using vba. Now, the issue is I'm trying to retrieve data from those dynamically created textboxes. It's not that I can't get the data, it's that I can't pull it out of the form and use it once the form is closed.
I used the following code sets for retrieving data from a single option textbox and it works perfectly:
Private Sub SOSubmitButton_Click()
FLNAmt = SingleOptionForm.SOBox
Unload SingleOptionForm
End Sub
and
SingleOptionForm.Show
SingleOptionForm.Hide
When I use FLNAmt shortly after hiding the form, there's data in the variable. However, using some of the much similar coding:
Private Sub AsgnFLNButton_Click()
Dim MultFLNAmt As String
Dim i As Integer
i = 1
If i = 1 Then
MultFLNAmt = MultipleOptionForm.Controls("Textbox" & i)
Else
MultFLNAmt = MultFLNAmt & "," & MultipleOptionForm.Controls("Textbox" & i)
End If
Unload MultipleOptionForm
End Sub
and
MultipleOptionForm.Show
MultipleOptionForm.Hide
MsgBox MultFLNAmt
When the MsgBox appears, MultFLNAmt is blank. Is there something different I need to do when gathering data from dynamically created textboxes and using it after the form closes than I do when using a textbox already hard coded into the form?

Is there a way to programatically add Form Controls?

I'm working on a spreadsheet with a Form Control that opens a Userform used for data entry purposes. The submit button of the form fills a row of cells and adds two buttons on the last two cells of the row. It will insert as many rows as the user does and each time the two new buttons are created with their own distinct name. However, these are ActiveX controls and they are giving me compatibility problems with other Windows/Office versions once colleagues open the file and try to use it on their laptop.
This is the code I'm using to add one of the command buttons on the spreadsheet (it is essentially the same for the other button, just different variables):
Dim i As Long, Hght As Long
Dim Name As String, NName As String
i = 0
Hght = 305.25
NName = "cmdAction" & i
For Each OLEObject In ActiveSheet.OLEObjects
If Left(OLEObject.Name, 9) = "cmdAction" Then
Name = Right(OLEObject.Name, Len(OLEObject.Name) - 9)
If Name >= i Then
i = Name + 1
End If
NName = "cmdAction" & i
Hght = Hght + 27
End If
Next
Dim UpdateEntry As OLEObject, N%
Set UpdateEntry = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=Selection.Offset(0, 23).Left, Top:=Selection.Offset(0, 23).Top, Width:=72, Height _
:=24)
UpdateEntry.Name = NName
UpdateEntry.Object.Caption = "Edit Entry"
With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
N = .CountOfLines
.InsertLines N + 1, "Private Sub " & NName & "_Click()"
.InsertLines N + 2, vbTab & "Code for button goes here"
.InsertLines N + 3, vbTab & "End Sub"
End With
I was wondering if it was possible to do the same thing, but that the buttons created are form control instead of ActiveX?
The error that is displayed is Run-time error 32809: Application-defined or object-defined error. After extensive research, I've found that it happens due to the sheet getting corrupted once a different version of Windows has altered it. The only way to fix it is to create a new sheet, copying all of the contents to the new sheet, deleting the corrupted sheet and renaming the new one to the name it had previously. This works, but it is not possible to copy the ActiveX Controls, because they will be renamed and the appropriate code will not be in them, however the Form Control on the spreadsheet used to open the UserForm will work just fine, which is why I think my only solution would be to change all ActiveX to Form Control.
Would appreciate some help.
Instead of using ActiveX buttons, use Shapes and set the Shape.OnAction property to call the macro that you would normally place behind the ActiveX button. In the macro you can use Application.Caller to get the name of the shape that was clicked, thus allowing you to know which button was clicked and to branch your code accordingly.

VBA and Publisher - replacing text in a master page

Recently I have been struggling with publisher at work, and the most recent source of concern is the software inability to give something a simple as an auto-updating "total pages" count.
I decided to look into macros for a solution, but with my limited knowledge of VBA I only managed to get so fat before hitting a roadblock.
I want to create a macro that automatically starts when the document is opened and as frequently as possible updates a textbox in the master page.
The textbox should show "page X of Y", but due to publisher limitations only the "X" is automatically updated without using a macro.
What I have right now:
Private WithEvents PubApp As Publisher.Application
Private Sub Document_Open()
Set PubApp = Publisher.Application
End Sub
Private Sub PubApp_WindowPageChange(ByVal Vw As View)
MsgBox "Your publication contains " & _
ActiveDocument.Pages.Count & " page(s)."
End Sub
The macro automatically starts when the document is opened and creates a popup with the total page number every time the user changes page.
So, I accomplished the first half of my goal, but I need some help with the rest.
Here's a working macro for anyone with the same problems
Private WithEvents PubApp As Publisher.Application
Private Sub Document_Open()
Set PubApp = Publisher.Application
End Sub
Private Sub PubApp_WindowPageChange(ByVal Vw As View)
Dim mp As MasterPages
Set mp = ActiveDocument.MasterPages
With mp.Item(1)
.Shapes(19).TextFrame.TextRange.Text = ""
.Shapes(19).TextFrame.TextRange.InsertPageNumber
.Shapes(19).TextFrame.TextRange.InsertBefore _
NewText:="Page "
If ActiveDocument.Pages.Count > 9 Then
.Shapes(19).TextFrame.TextRange.InsertAfter _
NewText:=" of " & _
ActiveDocument.Pages.Count
Else
.Shapes(19).TextFrame.TextRange.InsertAfter _
NewText:=" of 0" & _
ActiveDocument.Pages.Count
End If
End With
End Sub
X In mp.Item(X) identifies your master page (in case you have more than one).
Y in Shapes(Y) identifies the target text box.
The macro runs in background without the need of active input and refreshes the content of the target text box on every page change.
Since the automatic page numbering format used in the document is "01, 02, 03 ... 11, 12 13" I included an If selection to add a 0 before the total page count if said number is lower than 10.
I'm sure there are more elegant ways of solving this problems, but hey, at least it works.
Here is a clarification for anyone else trying to research the limited information on programmatically changing the header or footer in the master pages using VBA in Publisher. This tip also includes a clarification of how to Add Text To the Left,Center,Right portions of the header:
All the credit goes to M.Baroni
Function FixHeader1()
Dim mp As MasterPages
Set mp = ActiveDocument.MasterPages
With mp.Item(1)
.Header.TextRange.text = "My Publication" & vbTab
.Header.TextRange.InsertPageNumber
.Header.TextRange.InsertAfter NewText:=vbTab & MonthName(Month(Date)) & " " & Year(Date)
End With
End Function

VBA How do you copy and paste in a Userform using right-click?

I want to allow users to be able to paste values into TextBoxes in a userForm in VBA. You can use Ctrl-v just fine, but not everyone knows how to do that.
How do I enable copy and pasting using a right-click menu?
I realize this is an old post but I believe there is a more efficient method.
Userform Contextual Menu class code
http://www.andypope.info/vba/uf_contextualmenu.htm
There are even sample excel spreadsheets for the code examples.
The class module handles the construction of the contextual menu, the capture of right clicking in textboxes and the actual Cut. Copy and Paste actions. The class makes use of the userform's ActiveControl object. The code even handles controls within container controls such as Frames and Multipage.
The follow Initialization code, from the userform, shows how simple it is to define and use the class object. You only need declare a variable to the object and then set a reference for each textbox you want to have contextual menu capabilities. You can loop through all controls and automatically reference each textbox.
Private m_colContextMenus As Collection
Private Sub UserForm_Initialize()
Dim clsContextMenu As CTextBox_ContextMenu
Dim cTRL as Control
Set m_colContextMenus = New Collection
For Each cTRL In Me.Controls
Select Case TypeName(cTRL)
Case "TextBox"
'MsgBox cTRL.Name & ": " & Me.Controls(cTRL.Name).Value
Set clsContextMenu = New CTextBox_ContextMenu
With clsContextMenu
Set .TBox = Me.Controls(cTRL.Name)
Set .Parent = Me
End With
m_colContextMenus.Add clsContextMenu, CStr(m_colContextMenus.Count + 1)
Case Else
'MsgBox TypeName(cTRL) & ": " & cTRL.Name
End Select
Next
End Sub
Download example workbook which contains both .xls and .xlsm files
This may be of interest: http://word.mvps.org/faqs/userforms/AddRightClickMenu.htm