Ms Access Gurus      

UserForms Presentation

{ image: UserForms }

Quick Jump

Goto the Very Top  

Download

Presentation

Access Lunchtime hosted by Maria Barnes 25 August 2026

PDF with Slides

UserForms_presentation_s4p.pdf

Get UserForm tools

GoToField UserForm

Go To Field or Control

https://msaccessgurus.com/tool/Userform_GoToField.htm

FindModule UserForm

Find Module and other VB components

https://msaccessgurus.com/tool/Userform_FindModule.htm

Goto Top  

Steps to Create a UserForm to Change CaSe

Create a UserForm to change case of text: UPPERCASE, lowercase, Title Case, Sentence case, tOGGLE cASE

This is what the UserForm will look like in design view after controls have been added

{ image: Change CaSe UserForm in design view }

Insert a new UserForm, add controls, set properties, tab order, and save

  1. Press Alt-F11 to go to the Visual Basic Editor (VBE)
  2. Insert a New UserForm — Insert menu or right-click in Project Explorer. (Remember the trick if you're in Access: Customize a Toolbar with the Insert UserForm icon).
  3. View menu: Toolbox (if not showing)
  4. View menu: Project Explorer (if not showing)
  5. View menu: Properties Window (if not showing) — choose Categorized tab
  6. Set UserForm Properties
    Appearance
    CaptionChange CaSe
    Behavior
    (Name)uform_ChangeCase
    ShowModalFalse
    Position Make the userform bigger for designing then come back and set these after controls are added.
    Height204
    Left0
    Top0
    Width485
  7. Save. Save icon, Ctrl-S, or File, Save
  8. Create a ListBox control using the Toolbox and set Properties
    (Name)Lst_Case
    Appearance
    Caption  Choose CaSe for the Result
    BackColor  Windows Background (White)
    BorderStyle  0 - fmBorderStyleNone
    ForeColor  Window Text (Black)
    SpecialEffect  0 - fmSpecialEffectFlat
    Data
    ColumnCount  2
    ColumnWidths  0 pt;150 pt
    Font
    Font  Calibri, 12 point
    Position
    Height  80
    Left  12
    Top  10
    Width  150
  9. Create 2 Textboxes and 2 Labels for Original text and Result text
  10. txt_Original TextBox Properties
    (Name)Label_Original
    Appearance
    BackColor  Windows Background (White)
    BorderStyle  0 - fmBorderStyleNone
    ForeColor  Window Text (Black)
    SpecialEffect  2 - fmSpecialEffectSunken
    Font
    Font  Calibri, 12 point
    Position
    Height  28
    Left  54
    Top  102
    Width  414
  11. txt_Result TextBox Properties
    (Name)txt_Result
    Appearance
    Caption  
    BackColor  Windows Background (White)
    BorderStyle  0 - fmBorderStyleNone
    ForeColor  Window Text (Black)
    SpecialEffect  2 - fmSpecialEffectSunken
    Font
    Font  Calibri, 12 point
    Position
    Height  28
    Left  54
    Top  138
    Width  414
  12. Label_Original Properties
    (Name)Label_Original
    Appearance
    Caption  Original
    BackStyle  0 - fmBackStyleTransparent
    BorderStyle  0 - fmBorderStyleNone
    ForeColor  Blue
    SpecialEffect  0 - fmSpecialEffectFlat
    Font
    Font  Calibri, 12 point
    Position
    Height  20
    Left  6
    Top  108
    Width  42
  13. Label_Result Properties
    (Name)Label_Result
    Appearance
    Caption  Result
    BackStyle  0 - fmBackStyleTransparent
    BorderStyle  0 - fmBorderStyleNone
    ForeColor  Green
    SpecialEffect  0 - fmSpecialEffectFlat
    Font
    Font  Calibri, 12 point
    Position
    Height  20
    Left  6
    Top  144
    Width  42
  14. Save
  15. Create 3 CommandButtons to Paste, Copy, and Close
  16. cmd_PasteOriginal CommandButton Properties
    (Name)cmd_PasteOriginal
    Appearance
    Caption  V Paste from Clipboard to Original
    BackColor  Gray, Button Face
    ForeColor  Blue
    Behavior
    WordWrap  TRUE
    Font
    Font  Calibri, 12 point
    Misc
    Accelerator  V
    Position
    Height  60
    Left  180
    Top  12
    Width  80
  17. cmd_CopyResult CommandButton Properties
    (Name)cmd_CopyResult
    Appearance
    Caption  Copy Result to Clipboard
    BackColor  Gray, Button Face
    ForeColor  Green
    Behavior
    WordWrap  TRUE
    Font
    Font  Calibri, 12 point
    Misc
    Accelerator  C
    Position
    Height  60
    Left  282
    Top  12
    Width  80
  18. cmd_Close CommandButton Properties
    (Name)cmd_Close
    Appearance
    Caption  Close
    BackColor  Gray, Button Face
    ForeColor  Window Text (Black)
    Font
    Font  Tahoma, 10 pt
    Misc
    Accelerator  o
    Position
    Height  24
    Left  414
    Top  30
    Width  38
  19. Save
  20. Set the Tab Order
    1. Right-click on the canvas and choose Tab Order from the shortcut menu
      { image: Right-click the canvas to set the Tab Order for a UserForm }
    2. Move controls up and down to match this:
      { image: Tab Order for the UserForm }
  21. Save
  22. Press F5 to open the form (or from the menu: Run, Run) and look at it.

Add VBA to the UserForm

  1. Go to the code behind the userform: Right-click on the userform or its name in the Project Explorer. Choose View Code from the shortcut menu.
    or use shortcuts to switch: F7 to View Code, Shift-F7 to view object.
  2. Ctrl-A to Select all the code and press the Delete key to remove it.
  3. Copy the code below
  4. Paste into the module behind the userform
  5. Debug, Compile, and Save.
  6. To run, press F5 on the userform or in the code behind it. Whatever is on the clipboard will be in the Original textbox.
  7. Choose a CaSe from the listbox to see the Result.

    { image: Change CaSe UserForm showing conversion to Title Case }

Goto Top  

VBA code to copy

'The quick brown fox jumped over the lazy fence.

'*************** Code Start *****************************************************
' code behind userform: uform_ChangeCase
'-------------------------------------------------------------------------------
' Purpose  : Switch CaSe of text between
'              UPPERCASE, lowercase, Title Case,
'              Sentence case, and tOGGLE cASE
' Author   : crystal (strive4peace)
' instructions to create this userform:
'   https://msaccessgurus.com/presentation/Userforms.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark changes. Use at your own risk.
'------------------------------------------------------------------------------
'           to Run!
'------------------------------------------------------------------------------
' 1. in the Project Explorer, select:  uform_ChangeCase
'        press F5 to Run!
'        or from the menu, choose Run, Run Sub/UserForm
' OR 2. in VBA or Immediate window:    uform_ChangeCase.Show
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark Changes. Use at your own risk.
'-------------------------------------------------------------------------------
'  LATE binding is used. for EARLY binding:
'      Microsoft HTML Object Library ( MSHTML )
'-------------------------------------------------------------------------------

Option Compare Binary  'upper and lower case not the same
Option Explicit  'declare variables

Private Sub UserForm_Initialize() 
'260823,24
' Set Original text to contents of Clipboard.
' Load listbox with CaSe types.

   'CALLs
   '  ClipboardGetText
   
   Dim vList() As Variant 
   
   'paste from Clipboard to Original text
   Me.txt_Original.Value = ClipboardGetText() 
   
   'arrays start with 1
   ReDim vList(1 To 5,1 To 2) 
   '1. Value
   '2. Example

   vList(1,1) = 3: vList(1,2) =  "UPPERCASE" 'msoCaseUpper
   vList(2,1) = 2: vList(2,2) =  "lowercase" 'msoCaseLower
   vList(3,1) = 4: vList(3,2) =  "Title Case" 'msoCaseTitle
   vList(4,1) = 1: vList(4,2) =  "Sentence case." 'msoCaseSentence
   vList(5,1) = 5: vList(5,2) =  "tOGGLE cASE" 'msoCaseToggle

   With Me.Lst_Case 
      .List = vList 
      'set default to Title Case  
      'thanks to Neil Sargent for this tip!
      .Selected(2) = True  '3rd item in list is index 2
   End With 
   'calculate and show the Result
   Call DoChangeCase   
   
End Sub 

Private Sub cmd_Close_Click() 
'260823 Unload userform when click Close button
   Unload Me 
End Sub 

'=========================================== DoChangeCase
Private Sub Lst_Case_Click() 
'260823
   'Calculate and show the result
   Call DoChangeCase 
End Sub 
Private Sub txt_Original_AfterUpdate() 
'260823,24
   'Calculate and show the result
   Call DoChangeCase(True) 
End Sub 

Private Function DoChangeCase( _ 
      Optional pbSayMessage As Boolean = False _ 
   ) As Boolean 
'260823
'Calculate and show the Result
'by changing case of Original text
'whatever way the user selects

   Dim vsOriginal As Variant 
   Dim vsResult As Variant 
   Dim nCaseResult As Long 
   Dim i As Integer 
   
   Dim asSentence() As String 
   
   DoChangeCase = False 
   
   vsOriginal = Null 
   vsResult = Null 
   
   vsOriginal = Me.txt_Original.Value 
   
   With Me.Lst_Case 
      If IsNull(.Value) Then 
         .SetFocus 
         If pbSayMessage <> 0 Then 
            MsgBox  "Choose CaSe for result text" _ 
               ,, "Can't convert"
         End If 
         Exit Function 
      End If 
      nCaseResult = .Value 
   End With  'Me.Lst_Case
   
   vsResult =  ""
   If vsOriginal <>  "" Then 
      Select Case nCaseResult 
      Case 3  'UPPERCASE
         vsResult = UCase(vsOriginal) 
      Case 2  'lowercase
         vsResult = LCase(vsOriginal) 
      Case 4  'Title Case
         vsResult = StrConv(vsOriginal,vbProperCase)  '3
      Case 1  ' Sentence case.
         asSentence = Split(vsOriginal, ". ") 
         For i = LBound(asSentence) To UBound(asSentence) 
            vsResult = vsResult _ 
               & UCase(Left(asSentence(i),1)) _ 
               & IIf(Len(asSentence(i)) > 1 _ 
                  ,LCase(Mid(asSentence(i),2)) _ 
                  , "") _ 
               & IIf(i <> UBound(asSentence), ". ", "") 
         Next i  ' Sentence
      Case 5  'tOGGLE cASE"
         'Option Compare Binary at top for binary compare
         For i = 1 To Len(vsOriginal) 
            If Mid(vsOriginal,i,1) _ 
               <> LCase(Mid(vsOriginal,i,1)) _ 
            Then 
               vsResult = vsResult & LCase(Mid(vsOriginal,i,1)) 
            Else 
               vsResult = vsResult & UCase(Mid(vsOriginal,i,1)) 
            End If 
         Next i  ' tOGGLE
      End Select 
   End If 
   
   Me.txt_Result.Value = vsResult 

   DoChangeCase = True 
   
End Function 

'=========================================== Clipboard
Private Sub cmd_CopyResult_Click() 
'260823
   'Copy the Result to the Clipboard
   ClipboardSetText (Me.txt_Result.Value) 
End Sub 

Private Sub cmd_PasteOriginal_Click() 
'260823
   'Paste what's on the Clipboard to Original
   Me.txt_Original.Value = ClipboardGetText() 
   'calculate and show the Result
   Call DoChangeCase 
End Sub 

'==============================================================================
' from module: mod_Clipboard_MSHTML_s4p
' full module is here:
'     https://msaccessgurus.com/VBA/Clipboard_MSHTML.htm
' must use variants for this to work right
'-------------------------------------------------------------------------------
'  LATE binding is used. for EARLY binding:
'      Microsoft HTML Object Library ( MSHTML )
Public Sub ClipboardSetText(ByVal pvText As Variant) 
'240612 strive4peace
   With CreateObject("htmlfile")    'HTMLDocument
      .parentWindow.clipboardData.setData  "Text", pvText 
   End With 
End Sub 

Public Function ClipboardGetText() As Variant 
'240612
   With CreateObject("htmlfile") 
      ClipboardGetText = .parentWindow.clipboardData.getData("Text") 
   End With 
End Function 
'==============================================================================

'*************** Code End ******************************************************

Goto Top  

References

Microsoft Learn

UserForm object

Initialize event

Show method

Unload statement

UserForm toolbar

Toolbox

List property (Microsoft Forms)

Selected property (ListBox)

Value property (Microsoft Forms)

Microsoft Forms reference

Examples (Microsoft Forms)

TypeName function

Goto Top  

Share with others

here's the link to this page:

https://msaccessgurus.com/presentation/UserForms.htm

Goto Top