Create a dynamic button via VBA at runtime - vba

I created a Userform, that shows a few values from the table (within textboxes) that can be edited and a command button to save those changes back into the table.
Since I use the same template for various values I decided to dynamically create the Userform at runtime. But I can't get the command button to work at all. I cross-checked with various examples (on StackOverflow and other sites, e.g. here and here), but I can't find the problem.
This is my (simplified) code. The procedure is being called by a button click event itself (CommandButton1). As a workaround, I created a static (empty) UserForm UserForm1 which I use as a starting point to create a specific configuration of the form dynamically during runtime. Essentially it means that just the form elements are created dynamically. At least for now, but that's fine since I only need one single instance of the form to be displayed at any given time. Eventually, I would like to create the form at runtime too:
Private Sub CommandButton1_Click()
'Set Form
UserForm1.Caption = "Test"
'Create Form-Elements (Commandbutton)
Dim cmdButton01 As MSForms.CommandButton
Set cmdButton01 = UserForm1.Controls.Add("Forms.CommandButton.1", "dynCmdButton01")
cmdButton01.Width = 50
cmdButton01.Caption = "Save"
'Show Form
UserForm1.Show
End Sub
Private Sub dynCmdButton01_click()
MsgBox "Test"
End Sub

You should create a class module and assign the event through it:
dynamically create buttons and assign code for the click event (during runtime)
dynamically assign code to static buttons

Related

Get value from command button VBA

I have created A userform with few command buttons.
I can't seem to figure out how do I get the information from them.
I want to open this userform from another one and then I want the user to choose an option from one of the buttons which will change a specific cell in the table itself
the userform I created
I did not write anything on this userform therefor there is no code from it to show only the design.
how do get the information from the buttons to be written in A specific cell on a specific worksheet?
double click on one of the buttons, in the code menu a new sub should appear. this looks something like:
Sub CommandButton1_onClick()
End sub
Alongside this event method, it also has a few properties. The one that is usefull here is the CommandButton1.Value, this represents the state of the button (either true or false iirc).
So build for each button (I strongly advice giving them custom names to prevent getting lost in the trouble) as sub like this:
Sub CommandButton1_onClick()
if CommandButton1.Value then
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = "Some value"
else
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = ""
end if
End sub

How to use VBA to add a button to a sheet and assign its click event upon another button press

I am trying to automate a quarterly patient report for a local pharmacy and in doing so I have transferred it to excel. One portion of the automation is an add patient button on the cover page of the report that goes to a form for relevent information. The ok button on the form takes the information and formats it in a new sheet named according to the patient's name. The button also adds two newly created buttons to the patient sheet, a delete and edit button. I can create the buttons and place them, but I can not find any way to assign a click event to the buttons, since they are considered new objects on each page.
I have moved the button's main code to the workbook itself, so all I really need to put in the button's click event is a call to that method, but I can't find any way to access the new buttons' click events through vba, and since I need to call a method in VBA itself, I'm not sure I can use a macro either (fair note, I am not all that familiar with excel macros, so if the solution lies in them, I can use that too).
Here is the code that instantiates and places/sizes the delete button on the new sheet:
Dim btn As OLEObject
Set btn = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False)
With btn
.Name = "deletePatientButton"
.Object.Caption = "Delete Patient"
.Top = 5.25
.Left = 290.25
.Width = 75
.Height = 24.75
.PrintObject = False
End With
Here is the main method of the delete button placed in the workbook code itself (note it only really calls another verification form, so this may be redundant, but I wanted to put it in the workbook section for testing since I assumed it would have the largest scope):
Public Sub mainDeleteButton(sheet As Worksheet)
Dim confirmer As New deleteConfirmationForm
sheet.Activate
confirmer.Show
End Sub
Finally, here is an example of the click event I am hoping to be able to place, or replace with another solution:
Private Sub deletePatientButton_Click()
Call ThisWorkbook.mainDeleteButton(Me)
End Sub
Any help is more than appreciated!
It is possible to add the event code programmatically to the worksheet module (see this post). However, it may be easier to keep your buttons on template worksheets that already have the event code in them. Just copy your template to a new sheet, rename it, and add your patient data.

How can I destroy the previous instance of object and create new one whenever a function is called?

Whenever a user selects a cell in a sheet I want a windows form to appear and any previous windows form that was connected to the previously active cell to be dissapear. How can I do this?
I'm thinking a solution like the following would work, but I don't know if it is good practice and I also don't know how to execute the command that I have commented out
Sub CreateNewFormForActiveCellAndDeleteThePreviousOne()
'Remove all previous instances of class Frm
Dim Frm as New Frm
End Sub
Instead of creating a new form instance I'd suggest updating the data on the form instead. Thus, you will save resources required for creating a new form instance. For example, you may define a public field in the Form's class and update it according to the selected cell in Excel.

Access Subform Source object

What I am trying to achieve is for a combo box (Combo_sf) selection to dictate the form in the subform control (sf_record) I have about 10 forms, their names are in the combo box data. I am new to VBA and am not sure if my approach is right:
Private Sub Combo_sf_AfterUpdate()
Dim strLoadTable As String
strLoadTable = "Form." & Me.Combo_sf.Value
MsgBox strLoadTable
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
End Sub
I have placed this in the combobox's after update event but when I make my selection nothing happens in the form. Am I approaching this right or would another way work better?
Your approach should work. I put a combo box named cbxSubform on my main form and added one line of code to its AfterUpdate() event handler...
Private Sub cbxSubform_AfterUpdate()
Me.mySubform.SourceObject = Me.cbxSubform.Value
End Sub
...and changing the selection in the combo box switches the subforms immediately. Are you sure that the AfterUpdate() code for your combo box is actually firing? (You could add a MsgBox or a Debug.Print to check.)
It could be this line which is tripping you up:
strLoadTable = "Form." & Me.Combo_sf.Value
What is your form object called? If your form is called Form.myTableName it could be the . that is throwing it out, try setting it to a form without a dot in its name.
In this line, it seems the code attempts to change the SourceObject property of a Form object.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
However, SourceObject is a property of a subform control, not the form contained in that control. So if the subform control is named sf_record, do it this way.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.SourceObject = strLoadTable
Also, if the after update procedure runs from [frm_Mnu_Manage Configuration Settings], you can use Me to refer to the form.
Me!sf_record.SourceObject = strLoadTable
Finally, if Me.Combo_sf.Value is the name of a form, you don't need to prefix its name with "Form.". It worked either way in my test, but I would just leave off "Form.".
strLoadTable = Me.Combo_sf.Value

How to reference a subform in MS Access

In my MS Access application, I am using a form which contains only two controls - a textbox and a command button. This form is named as HEADER FORM.
HEADER FORM is used as a subform in header section of various other forms.
What I want to do is that whenever a particular form loads, I want to fill details in the textbox of the HEADER FORM (that will be name of the person who has logged in. The same would be clear from the picture below).
I am trying to call a global subroutine named updateHeader in form load event of all the forms.
Public Sub updateHeader()
Me![HEADER FORM].Form.txtHeaderName.Value = strPerson
End Sub
Following is the picture showing HEADER FORM in Design View and the same being used as a subform in a login form.
I tried various other options but am not able to come out with the correct way to reference the form. Am I doing something wrong fundamentally?
The error that I am seeing is invalid use of Me keyword.
Also, my updateHeader subroutine is a global subroutin which is called from Form_Load event of all the forms.
If your updateHeader() procedure is contained in a standard module, that would explain the complaint about the Me keyword ... it's not valid in a standard module.
In a form module, Me means "this form".
You could change the procedure declaration to accept a reference to a form.
Public Sub updateHeader(ByRef TheForm As Form)
' Me![HEADER FORM].Form.txtHeaderName.Value = strPerson
TheForm![HEADER FORM].Form.txtHeaderName = strPerson
End Sub
.Value is the default property and therefore not needed here, so I left it out. But it won't hurt to add it back if you prefer.
You can then call the procedure from the parent form, and pass the procedure a reference to itself (the parent form).
updateHeader Me
I got these "syntax versions" from Wiley.Microsoft.Office.Access.2007.Bible:
When referencing subform controls:
Forms![FormName]![SubformName].Form![ControlName]
When using/referencing subforms within subforms, use the following syntax:
Forms![FormName]![SubformName].Form![SubSubformName].Form.[ControlName]