Here is my problem:
I have a simple VBA UserForm and a Label (Label1) inside it which I called it u1_Status to inform users to what is happening behind the processes of my code. Its label will have different text at different stages of my code.
I want my UserForm size change according to the label inside it. For example when I have long informative text in Label1 I want my UserForm size increases and if label1 text in later stages is short my UserForm fits its dimensions to this updates text.
Here is my simplified code so far:
sub Test1()
Dim htLabelHeight: htLabelHeight = u1_Status.Label1.Height
Dim wdLabelWidth: wdLabelWidth = u1_Status.Label1.Width
u1_Status.Height = htLabelHeight
u1_Status.Width = wdLabelWidth
u1_Status.Show
end sub
The problem is that the width is Ok but the height seems to be Zero.
How my userform should looks:
How it Looks with my code:
The height of the userform also includes the height of the title bar. So you have to use the (read only) InsideHeight property of the userform.
Option Explicit
Private Sub UserForm_Activate()
With Me
Dim titleBarHeight As Long
titleBarHeight = .Height - .InsideHeight
.Label1.Caption = Sheet1.Range("A10")
.Height = .Label1.Height + titleBarHeight
.Width = .Label1.Width
End With
End Sub
I have a PowerPoint quiz which calculates a score. If the score is above 80% I would like a button(shape with name "PrintCert") to become visible so the user may print a certificate. Currently the button is always visible. Here is the code that I've tried with no success. (The first and last part of the code updates labels.)
Sub showresult()
Percentage.Caption = Int((CA.Caption) * 100 / (TQ.Caption)) & "%"
With ActivePresentation.Slides(37)
If ((CA.Caption) * 100 / (TQ.Caption)) > 79.9 Then
.Shapes("PrintCert").Visible = True
Else
.Shapes("PrintCert").Visible = False
End If
End With
SlideLayout41.PercentageCertificate.Caption = Percentage.Caption
ActivePresentation.SlideShowWindow.View.Next
End Sub
Simplest thing might be to add a new first slide with a "Start the show" action button that users click to begin the presentation. You'd apply a Run Macro action setting to the button and have it run a macro something like this:
Sub StartShow()
' set your button shapes to be invisible
With SlideShowWindows(1).Presentation.Slides(37)
.Shapes("PrintCert").Visible = False
End With
' etc, for any other shapes you want to have
' hidden to start with
' go to the actual first slide
SlideShowWindows(1).View.GotoSlide (2)
End Sub
So I have a multipage where I want the same button to show on both pages. I could place it outside but the border from the multipage is so ugly so I tried to place everything on the pages. Unfortunately you can't name the items (such as the combobox) with the same name. Is there a workaround to remove the borders and just show the page names or have the same name on the item on two pages?
Had some fun with this goal.
Consider this UserForm in Editor with TabStrip, 2 frames and some other controls.
Frames are named from Frame0, Frame1, etc.
Assuming the Frame0 is the location reference and first to display when UserForm is displayed, code below will be what you want.
Code:
Option Explicit
Private Sub TabStrip1_Change()
Dim i As Long, lActiveTabIndex As Long
lActiveTabIndex = Me.TabStrip1.Value
For i = 0 To Me.TabStrip1.Tabs.Count - 1
Me.Controls("Frame" & i).Visible = (i = lActiveTabIndex)
Next i
End Sub
Private Sub UserForm_Initialize()
Dim i As Long
With Me
.Height = 288 ' Adjust to your desired height
' Align all FrameX top/left to same as Frame0, delete Caption and SpecialEffect
For i = 0 To Me.TabStrip1.Tabs.Count - 1
With Me.Controls("Frame" & i)
.Top = Me.Frame0.Top
.Left = Me.Frame0.Left
.Caption = ""
.SpecialEffect = fmSpecialEffectFlat
End With
Next i
End With
' Ensure frame for first tab is displayed
TabStrip1_Change
End Sub
Userform first load (didn't save screenshot, neither the workbook, sorry).
Next tab clicked:
I am working in Excel 2010 on a large project. Part of it checks for balanced amounts in several different files and if any of them are out of balance, a form pops up with a list of clickable links to the files in question and the expected values. Up until recently, this has been working fine, but this morning I see that the Activate procedure isn't being called when I Show the form. If I click elsewhere and then click on the form again, the Activate procedure runs as it should. It just doesn't run the first time. I even tried stepping through the code and it's not called at all.
As you can see, I didn't change the name of the default "UserForm_Activate". I will say that I have added a lot of code to the project since the last time this functionality worked, but none of that code relates to this form or the procedure that calls it. (That being said, I still plan on looking for a connection there.)
The form is set up to be non-modal. I also have another form that I use as a user interface that is also non-modal and therefore stays visible when this form pops up. The OOBItems object in the code is a Dictionary object with the expected value as the data and the file paths as the keys. LabelLink is a class that makes the links clickable.
Option Explicit
Private LinkLabel() As LabelLink
'***********************************************************************
'Name: UserForm_Activate
'Description: Adds links as clickable labels to form when it is shown.
'
'Revision History:
'Date Author Changes made
'***********************************************************************
Private Sub UserForm_Activate()
Const LABELHEIGHT As Integer = 10
Const LABELSTARTTOP As Integer = 40
Const LABELWIDTH As Integer = 428
Const LABELLEFT As Integer = 6
Const MCSLABEL As String = "Forms.Label.1"
Const BLUE As Long = 16711680 'RGB(0, 0, 255)
Const PURPLE As Long = 16711935 'RGB(255, 0, 255)
Dim Links() As String
Dim LinkStart As Integer, LinkEnd As Integer
Dim LinkCount As Integer, LinkNum As Integer
'split the tag back into links
Links() = Split(Me.Tag, "||")
LinkStart = LBound(Links)
LinkEnd = UBound(Links)
LinkCount = LinkEnd - LinkStart + 1
ReDim LinkLabel(LinkStart To LinkEnd)
'loop through links to add them as labels
For LinkNum = LinkStart To LinkEnd
'create new instance of the LabelLink class
Set LinkLabel(LinkNum) = New LabelLink
'add the label to the form
Set LinkLabel(LinkNum).mObjLbl = Me.Controls.Add(MCSLABEL)
'format the label
With LinkLabel(LinkNum).mObjLbl
.Caption = Links(LinkNum)
.Height = LABELHEIGHT
.Left = LABELLEFT
.Width = LABELWIDTH
.Top = .Height * LinkNum + LABELSTARTTOP
.Font.Underline = True
.Font.Size = 8
'trying to fix the issue of the clicked link text going back to
'blue if you click on the main form then click back to this form
If .ForeColor = PURPLE Then
.ForeColor = PURPLE
Else
.ForeColor = BLUE
End If
End With
Next LinkNum
'refresh form
Me.Repaint
End Sub
'***********************************************************************
'Name: DisplayOOBLinks
'Description: Takes an array of links and opens the form to show them.
'
'Revision History:
'Date Author Changes made
'***********************************************************************
Public Sub DisplayOOBLinks(OOBItems As Object)
Const FORMBASEHEIGHT As Integer = 72
Const LABELHEIGHT As Integer = 12
Dim LinkCount As Integer
Dim TotalsText As String
'get count of OOB items
LinkCount = OOBItems.Count
'resize userform for the number of links to show
Me.Height = LinkCount * LABELHEIGHT + FORMBASEHEIGHT
'resize the expected totals textbox for the number of OOB items
Me.Controls("ExpectedTotals").Height = (LinkCount + 1) * LABELHEIGHT
'add OOB values to expected totals textbox text
Me.Controls("ExpectedTotals").text = "Expected totals:" & vbLf & Join(OOBItems.Items, vbLf)
'preload the form so that the tag can be set
Load Me
'join the links into one string to assign to the tag since forms don't take
'any parameters
Me.Tag = Join(OOBItems.Keys, "||")
'show the form
Me.Show
End Sub
This is the entirety of the code module for the form in question.
Can anyone see why the Me.Show line would show my form, but not run the Activate procedure? I guess I could call it specifically after I show the form but I'd rather not use a workaround when I shouldn't have to.
Well, since no one (including me) could come up with anything better, I'll share my workaround and give it another few days before I close the question.
After I show the form, I call the activate procedure manually: UserForm_Activate. It works, but I'd still like to know why it isn't triggering automatically.
I have complete my form to use around the office, however, when opened on different computers the form doesnt resize.Instead, the scroll bar appears. How can i make the form and controls automatically resize ?
Here is some VBA code you could add to your form that will keep the form looking the same no matter how large or small the user has made the window on their monitor or what their monitor resolution is.
Also you can make the text larger or smaller by holding the Ctrl key and scrolling the mouse wheel up and down (or, alternatively, holding the Shift key and hitting the + key or the - key.)
To use this functionality, just open Access and open your form in design view. First, right-click on the image of the form and add the Form Header/Footer.
If you don't add the header and footer to the form, the code below will error out. However, you can shrink the height of both the header and the footer to nothing if you don't want them to appear on your form.
Select the Form itself by clicking the little box at the top left of the form, just below the tab:
This will make sure we are looking at the properties for the form itself when we view the Property Sheet.
To view the Property Sheet for the form (if it isn't visible already), hold the Alt key and press the Enter key.
Choose the Event tab.
You'll then need to add the literal text [Event Procedure] to the following five events behind the form itself:
On Load
On Key Up
On Key Down
On Resize
On Mouse Wheel
You can either type the literal text [Event Procedure] into the text box next to these events, or click the ellipsis (...) button next to each event and choose Code Builder from the pop up menu.
It will look something like this:
...
...
...
...
Also, at the bottom of the list of events, you'll also need to change the Key Preview property to Yes:
Finally, you'll probably want to turn Scroll Bars off on the form so that they don't overlap any content. To do this, go to the Format tab of the Property Sheet for your form in design view and change the Scroll Bars property to Neither.
Now, to add the VBA code, hold Alt and hit F11 to view the VBA editor.
Once inside the VBA editor, double click on the Form_YourFormName option under the Microsoft Access Class Objects folder:
If you do not see the Microsoft Access Class Objects folder, then go back to the form in design view and click the ellipsis (...) next to the literal text [Event Procedure] on any of the events you just modified.
This will take you back to the VBA editor and you should now be inside the Form_YourFormName code area. There will already be some code there, but you can erase all of it before proceeding to the next step.
Then in the main part of the screen on the right, just copy and paste the code below and you're done.
Option Compare Database
Option Explicit
'Set an unchangeable variable to the amount (10% for example) to increase or
'decrease the font size with each zoom, in or out.
Const FONT_ZOOM_PERCENT_CHANGE = 0.1
'Create the fontZoom and ctrlKeyIsPressed variables outside of
'the sub definitions so they can be shared between subs
Private fontZoom As Double
Private ctrlKeyIsPressed As Boolean
'Create an enum so we can use it later when pulling the data out of the "Tag" property
Private Enum ControlTag
FromLeft = 0
FromTop
ControlWidth
ControlHeight
OriginalFontSize
OriginalControlHeight
End Enum
Private Sub Form_Load()
'Set the font zoom setting to the default of 100% (represented by a 1 below).
'This means that the fonts will appear initially at the proportional size
'set during design time. But they can be made smaller or larger at run time
'by holding the "Shift" key and hitting the "+" or "-" key at the same time,
'or by holding the "Ctrl" key and scrolling the mouse wheel up or down.
fontZoom = 1
'When the form loads, we need to find the relative position of each control
'and save it in the control's "Tag" property so the resize event can use it
SaveControlPositionsToTags Me
End Sub
Private Sub Form_Resize()
'Set the height of the header and footer before calling RepositionControls
'since it caused problems changing their heights from inside that sub.
'The Tag property for the header and footer is set inside the SaveControlPositionsToTags sub
Me.Section(acHeader).Height = Me.WindowHeight * CDbl(Me.Section(acHeader).Tag)
Me.Section(acFooter).Height = Me.WindowHeight * CDbl(Me.Section(acFooter).Tag)
'Call the RepositionControls Sub and pass this form as a parameter
'and the fontZoom setting which was initially set when the form loaded and then
'changed if the user holds the "Shift" key and hits the "+" or "-" key
'or holds the "Ctrl" key and scrolls the mouse wheel up or down.
RepositionControls Me, fontZoom
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'PURPOSE: Make the text on the form bigger if "Shift" and "+" are pressed
'at the same time and smaller if "Shift" and "-" are pressed at the same time.
'NOTE: Using the "Ctrl" key instead of the "Shift" key conflicts with Access's
'default behavior of using "Ctrl -" to delete a record, so "Shift" is used instead
'Was the "Shift" key being held down while the Key was pressed?
Dim shiftKeyPressed As Boolean
shiftKeyPressed = (Shift And acShiftMask) > 0
'If so, check to see if the user pressed the "+" or the "-" button at the
'same time as the "Shift" key. If so, then make the font bigger/smaller
'by the percentage specificed in the FONT_ZOOM_PERCENT_CHANGE variable.
If shiftKeyPressed Then
Select Case KeyCode
Case vbKeyAdd
fontZoom = fontZoom + FONT_ZOOM_PERCENT_CHANGE
RepositionControls Me, fontZoom
'Set the KeyCode back to zero to prevent the "+" symbol from
'showing up if a textbox or similar control has the focus
KeyCode = 0
Case vbKeySubtract
fontZoom = fontZoom - FONT_ZOOM_PERCENT_CHANGE
RepositionControls Me, fontZoom
'Set the KeyCode back to zero to prevent the "-" symbol from
'showing up if a textbox or similar control has the focus
KeyCode = 0
End Select
End If
'Detect if the "Ctrl" key was pressed. This variable
'will be used later when we detect a mouse wheel scroll event.
If (Shift And acCtrlMask) > 0 Then
ctrlKeyIsPressed = True
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'Change the ctrlKeyIsPressed variable to false when
'any key is let up. This will make sure the form text does
'not continue to grow/shrink when the mouse wheel is
'scrolled after the ctrl key is pressed and let up.
ctrlKeyIsPressed = False
End Sub
Private Sub Form_MouseWheel(ByVal Page As Boolean, ByVal Count As Long)
'If the "Ctrl" key is also being pressed, then zoom the form in or out
If ctrlKeyIsPressed Then
Debug.Print ctrlKeyIsPressed
'The user scrolled up, so make the text larger
If Count < 0 Then
'Make the font bigger by the percentage specificed
'in the FONT_ZOOM_PERCENT_CHANGE variable
fontZoom = fontZoom + FONT_ZOOM_PERCENT_CHANGE
RepositionControls Me, fontZoom
'The user scrolled down, so make the text smaller
ElseIf Count > 0 Then
'Make the font smaller by the percentage specificed
'in the FONT_ZOOM_PERCENT_CHANGE variable
fontZoom = fontZoom - FONT_ZOOM_PERCENT_CHANGE
RepositionControls Me, fontZoom
End If
End If
End Sub
Public Sub SaveControlPositionsToTags(frm As Form)
On Error Resume Next
Dim ctl As Control
Dim ctlLeft As String
Dim ctlTop As String
Dim ctlWidth As String
Dim ctlHeight As String
Dim ctlOriginalFontSize As String
Dim ctlOriginalControlHeight As String
For Each ctl In frm.Controls
'Find the relative position of this control in design view
'e.g.- This control is 5% from the left, 10% from the top, etc.
'Those percentages can then be saved in the Tag property for this control
'and used later in the form's resize event
ctlLeft = CStr(Round(ctl.Left / frm.Width, 4))
ctlTop = CStr(Round(ctl.Top / frm.Section(ctl.Section).Height, 4))
ctlWidth = CStr(Round(ctl.Width / frm.Width, 4))
ctlHeight = CStr(Round(ctl.Height / frm.Section(ctl.Section).Height, 4))
'If this control has a FontSize property, then capture the
'control's original font size and the control's original height from design-time
'These will be used later to calculate what the font size should be when the form is resized
Select Case ctl.ControlType
Case acLabel, acCommandButton, acTextBox, acComboBox, acListBox, acTabCtl, acToggleButton
ctlOriginalFontSize = ctl.FontSize
ctlOriginalControlHeight = ctl.Height
End Select
'Add all this data to the Tag property of the current control, separated by colons
ctl.Tag = ctlLeft & ":" & ctlTop & ":" & ctlWidth & ":" & ctlHeight & ":" & ctlOriginalFontSize & ":" & ctlOriginalControlHeight
Next
'Set the Tag properties for the header and the footer to their proportional height
'in relation to the height of the whole form (header + detail + footer)
frm.Section(acHeader).Tag = CStr(Round(frm.Section(acHeader).Height / (frm.Section(acHeader).Height + frm.Section(acDetail).Height + frm.Section(acFooter).Height), 4))
frm.Section(acFooter).Tag = CStr(Round(frm.Section(acFooter).Height / (frm.Section(acHeader).Height + frm.Section(acDetail).Height + frm.Section(acFooter).Height), 4))
End Sub
Public Sub RepositionControls(frm As Form, fontZoom As Double)
On Error Resume Next
Dim formDetailHeight As Long
Dim tagArray() As String
'Since "Form.Section(acDetail).Height" usually returns the same value (unless the detail section is tiny)
'go ahead and calculate the detail section height ourselves and store it in a variable
formDetailHeight = frm.WindowHeight - frm.Section(acHeader).Height - frm.Section(acFooter).Height
Dim ctl As Control
'Loop through all the controls on the form
For Each ctl In frm.Controls
'An extra check to make sure the Tag property has a value
If ctl.Tag <> "" Then
'Split the Tag property into an array
tagArray = Split(ctl.Tag, ":")
If ctl.Section = acDetail Then
'This is the Detail section of the form so use our "formDetailHeight" variable from above
ctl.Move frm.WindowWidth * (CDbl(tagArray(ControlTag.FromLeft))), _
formDetailHeight * (CDbl(tagArray(ControlTag.FromTop))), _
frm.WindowWidth * (CDbl(tagArray(ControlTag.ControlWidth))), _
formDetailHeight * (CDbl(tagArray(ControlTag.ControlHeight)))
Else
ctl.Move frm.WindowWidth * (CDbl(tagArray(ControlTag.FromLeft))), _
frm.Section(ctl.Section).Height * (CDbl(tagArray(ControlTag.FromTop))), _
frm.WindowWidth * (CDbl(tagArray(ControlTag.ControlWidth))), _
frm.Section(ctl.Section).Height * (CDbl(tagArray(ControlTag.ControlHeight)))
End If
'Now we need to change the font sizes on the controls.
'If this control has a FontSize property, then find the ratio of
'the current height of the control to the form-load height of the control.
'So if form-load height was 1000 (twips) and the current height is 500 (twips)
'then we multiply the original font size * (500/1000), or 50%.
'Then we multiply that by the fontZoom setting in case the user wants to
'increase or decrease the font sizes while viewing the form.
Select Case ctl.ControlType
Case acLabel, acCommandButton, acTextBox, acComboBox, acListBox, acTabCtl, acToggleButton
ctl.FontSize = Round(CDbl(tagArray(ControlTag.OriginalFontSize)) * CDbl(ctl.Height / tagArray(ControlTag.OriginalControlHeight))) * fontZoom
End Select
End If
Next
End Sub
Here are some screenshots of what a form looks like when shrunk.
Before:
After:
Also, you can make the text larger by holding the Ctrl key and scrolling the mouse wheel up (or, alternatively by holding the Shift key and pressing the + key.)
And, you can make the text smaller by holding the Ctrl key and scrolling the mouse wheel down (or, alternatively by holding the Shift key and pressing the - key.)
A few notes:
Have a look at how to anchor controls to the form so they can resize with the form.
Design your forms so they display properly on the smallest screensize that your users have.
It's important that you think about how your users will interact with your application. You cannot expect Access to magically reflow and resize everything, it's something you, as the designer of the application, need to think about.
So limit the number of controls on your form, and keep them small enough that they display correctly on whatever is the smallest reasonable screen resolution in your office.
If you do not want bars to appear, look at the form's scrollbars properties.
Look into the various form styles you can use: in Access 2007 and above, you can use forms in tabs. You can also make them popup, and prevent them from being resized.
Look into the following form properties and play around the various combinations to get the desired effect: