Advanced
DataObject
A holding area for formatted text data used in transfer operations. Also holds a list of formats corresponding to the pieces of text stored in the DataObject.
MSForms.DataObject
A DataObject can contain one piece of text for the Clipboard text format, and one piece of text for each additional text format, such as custom and user-defined formats.
A DataObject is distinct from the Clipboard.
A DataObject supports commands that involve the Clipboard and drag-and-drop actions for text.
A DataObject supports commands that involve the Clipboard and drag-and-drop actions for text. When you start an operation involving the Clipboard (such as GetText) or a drag-and-drop operation, the data involved in that operation is moved to a DataObject.
The DataObject works like the Clipboard. If you copy a text string to a DataObject, the DataObject stores the text string.
If you copy a second string of the same format to the DataObject, the DataObject discards the first text string and stores a copy of the second string.
It stores one piece of text of a specified format and keeps the text from the most recent operation.
Dim oDataObject As MSForms.DataObject
oDataObject = New MSForms.DataObject
oDataObject.SetText "some text"
oDataObject.PutInClipboard
oDataObject.GetFromClipboard
Data Binding
RowSource is a table/query for a ComboBox or ListBox.
ControlSource is a field to which a control is bound.
Row Source - It is used to fetch the data to the control for which the property is set.
Control Source - If more than one field/column is retrieved in the Row Source property
The Control Source, as was mentioned by Rural Guy, is the property of a control which is normally set to a field name that exists in the form's Record Source (table, query or select statement). You can type a formula in there too but then it will not update a table. It has to be "bound" to a field from the table, query, or Select statement so that Access knows where to store or edit the data for that control.
Shortcut Menus
Const PopUpName As String "MyPopUpMenu"
Sub DeletePopupMenu
CommandBars(PopUpName).Delete
End Sub
Sub CreatePopupMenu
Dim cb As CommandBar
Dim cbp As CommandBarPopup
Set cb = CommandBars.Add(PopUpName, msoBarPopup, False, True)
With cb
With .Controls.Add(Type:=msoControlButton)
.OnAction = ""
.FaceId = 10
.Caption = ""
.ToolTipText = ""
End With
Set cbp = .Controls.Add(Type:=msoControlPopul
With cbp
.BeginGroup = True
.Caption = "sub menu"
With .Controls.Add(Type:=msoControlButton)
End With
Set cbp = Nothing
End With
End With
Set cb = Nothing
End Sub
Sub DisplayPopUp
Application.CommandBars(PopUpName).ShowPopup
End Sub
In the Userform code module
Private Sub UserForm_MouseMove(byVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
if Button = 2 Then
'number 2 is the right mouse button
Call DisplayPopUp
End If
End Sub
Creating Dynamically
Creating userforms dynamically
adding controls
adding the event subroutines
moving and sizing controls
Importing a Userform
The quickest way to add a userform to a project is to just import it from a saved file.
Dim objVBC As VBComponent
Set objVBC = Application.VBE.ActiveVBProject.VBComponents.Import("MyForm.frm")
The advantage of doing it through code is that the userform can be given a size appropriate to the user's screen resolution and size, and its controls positioned correctly.
Windows API call to freeze a window.
Private Declare Function LockWindowUpdate Lib "User32" (ByVal hwndLock As Long) As Long
Application.ScreenUpdating does not affect the VBE
You should use the above API function to freeze and unfreeze the window accordingly.
It is a good idea to leave 6 points between controls and between a control and the edge of the userform.
Freeze the VBE window
The HWnd property is a hidden property of the MainWindow object.
LockWindowUpdate Application.VBE.MainWindow.HWnd
Add a new userform
Set objVBC = Application.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_MSForm)
set the height and width
objVBC.Properties("Width") = Application.UsableWidth * 2 / 3
objVBC.Properties("Height") = Application.UsableHeight * 2 / 3
Adding controls
Dim objFrmDesign As ??
Dim objControl As ??
Set objFrmDesign = objVBC.Designer
Set objControl = objFrmDesign.Controls.Add("Forms.CommandButton.1"),"bnOK")
objControl.Caption = "OK"
objControl.Default = True
objControl.Height = 20
objControl.Width = 20
The name of the control to add can be found by adding a control from the Control Toolbox to a worksheet and then examining the EMBED function.
Add the references "Microsoft Forms 2.0 Object Library"
Add the references "Microsoft Visual Basic for Applications Extensibility 5.3"
Dim obVBE As VBIDE.VBE
Dim frm1 As VBIDE.VBCompionent
Dim frm2 As MsForms.Userform
Set obVBE = Application.VBE
Set frm1 = obVBE.ActiveVBProject.VBComponets.Add(vbext_ct_MSForm)
Dim cmd1 As MsForms.Control
With frm1
.Properties("Height") = 280
End With
With frm2
.Font.Name = "Tahoma"
End With
Set cmd1 = frm2.Controls.Add("Forms.CommandButton.1","cmbButton1")
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext