Solved How to create command button on tab control with vba in design mode on click from other form (1 Viewer)

mnaeem90

New member
Local time
Today, 10:41
Joined
Mar 14, 2020
Messages
7
I want to create button on tab control with VBA from different form. When i click to create button than with vba other form will open in a design view and create button on specific tab page.
example i have four tab pages page1 page2 page3 page4 now i want to create button on page 2

Private Sub pos_Click()
Dim FormName As String
Dim tab_page As pages
Dim frm As form
Dim tab_control as Control

FormName = "pos"
DoCmd.OpenForm FormName, acDesign

Dim newBt As Control

Set frm = Forms!pos
Set tab_control = frm!pos_products
Set tab_page = tab_control!pages(1)

Set newBt = CreateControl(frm.name, acCommandButton, Left:=100 + 3000, Top:=500)

End Sub
 
Last edited:

mnaeem90

New member
Local time
Today, 10:41
Joined
Mar 14, 2020
Messages
7
I am creating point of sale with different categories and each category have different products . category part i am covering with tab control pages it is done but i want to create products on each category..only one command button if i am able to create on specified control page than multiple with loop i can create multiple. with form i do not want to create.


I have searched on internet but not lucky to find any solution.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:41
Joined
May 7, 2009
Messages
19,247
advise not to over do it when you can enter the category in tabular form. the category being a combobox.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
As Arnel indicates, there is a better way of going about doing this other than creating controls.

However if you wish to continue along the route you are following, by creating controls, then that's a noble goal, and you will learn much!

However from my experience, where I have created some controls in the past, it is very difficult and not at all intuitive. You will find yourself reading through the obscure Microsoft help files and manuals. It is unlikely that you will receive any help, unless you are very lucky, because it's something that's just not done regularly. There will be very few people with the experience to enable them to offer you good advice.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 03:41
Joined
May 21, 2018
Messages
8,536
I disagree that creating controls dynamically is hard, it is really easy. I do agree that it is a really bad idea for a lot of reasons. If you want to make it appear dynamic create the buttons ahead of time and show/hide as necessary. You can even "float" the button above the tab control (do not put it in a page) and make it appear as if it is on every tab. Then show, hide, change caption, change method for each tab change. It will appear to be assigned to a page.
 

Dreamweaver

Well-known member
Local time
Today, 08:41
Joined
Nov 28, 2005
Messages
2,466
I think It's a very bad Idear and I would think I may show a design floor somewhere

mick
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
I disagree that creating controls dynamically is hard, it is really easy.

Well possibly you haven't tried to create controls on a Tab.

I fancied having a form with 26 tabs, one for each letter of the alphabet, after trying to do it manually, I decided to programmatically create the tab with 26 tabs and 26 subforms, one on each tab.

I can assure you it wasn't easy!
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
I found my project where I was creating a Form with a Tab Control, Tab Pages and then a subform on each page. I made a note of the Following useful links in the project module:-

'CreateControl Method [Access 2003 VBA Language Reference]
'From HERE:-

'Rename most controls in all forms in a database
'From HERE:-

'DeleteControl Method [Access 2003 VBA Language Reference] === DeleteControl frm.Name, ctlNew.Name
'From HERE:-

I've posted them here, as they may prove useful.
 

mnaeem90

New member
Local time
Today, 10:41
Joined
Mar 14, 2020
Messages
7
category part done ,only want to create button on tab control with VBA on design mode, i want to know is it possible with VBA or not
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
button on tab control with VBA on design mode, i want to know is it possible with VBA

Well, I know for sure you can place a subform/subreport control on a "Tab Page", but not, if my memory serves me correctly, the tab control itself.

I suspect that is the problem I was referring to, there is something odd about the object structure of tab controls. Again I'm remembering, possibly incorrectly!
 
Last edited:

mnaeem90

New member
Local time
Today, 10:41
Joined
Mar 14, 2020
Messages
7
i need one hint that is it possible with vba to create command button on tab control on pages...... than i will move..on
 

jdraw

Super Moderator
Staff member
Local time
Today, 03:41
Joined
Jan 23, 2006
Messages
15,379
I searched for something similar to your question. I found this response that dealt with command button, page, and tab and I don't think it is helpful, but I'm passing it on for you to judge.
 

mnaeem90

New member
Local time
Today, 10:41
Joined
Mar 14, 2020
Messages
7
no this will work on the normal form view... not on design view as we can not add controls on normal view
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
I found more links to threads in my notes which may be useful:-


 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
no this will work on the normal form view... not on design view as we can not add controls on normal view
I'm not sure this comment make sense! However, to make such changes you will need to open the form in design view.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
There will be very few people with the experience to enable them to offer you good advice.

MajP is one...
If you were to ask me who could help with this, then I would have recommended MajP, he is very knowledgeable in such matters. Seeing as he has already chipped in on this thread, then I suspect your chances of getting some help have gone up considerably.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
Attached is another example, with code for the command button in the form module "Hello World"

The command button is created and added to the "Tab Page" using VBA. Interestingly, the "OnClick" subroutine is also added with VBA "AND" the code contained within the subroutine, code which opens a message box displaying the text "Hello World"... All created with VBA.

This suggests two interesting projects to me, the first one, create a table with all of the information in it to build a Form and then use that information to build the Form. Basically what Microsoft Access would term a "Wizard"..

The second project follows, you could write code that examines a Form, extracts the information into a table. Now you have an interesting way of backing up your database, your Forms, and I assume other objects as well. You can back them up to a Table.
 

Attachments

  • TabAddButtonWITH_CodeStub.zip
    27.4 KB · Views: 550
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:41
Joined
Jul 9, 2003
Messages
16,282
The following code creates a Command Button on a Tab Page.

The Code Routine names the Button,
adds a Caption to the Button,
then creates an Event in the Form Module for the Buttons "On Click Event"
The Button "On Click Event" opens a Message Box saying --- "Hello World"

To do this yourself without downloading the sample file, in other words, it should work with any recent version of MS Access
Create Two new Forms Form1 and Form2

Place the following Code in Form1 and call it with a Command Button.

Code:
Sub NewControls()
Dim strFrmName As String
strFrmName = "Form2"

DoCmd.OpenForm strFrmName, acDesign

Dim mdl As Module
Dim frm As Form
Dim Ctrl As Control

Dim intDataX As Integer
Dim intDataY As Integer
Dim lngReturn As Long
Dim R$

        ' Open Existing Form with NO record source.
        Set frm = Forms(strFrmName)
        frm.RecordSource = ""

                ' Set positioning values for new controls.
                intDataX = 2000
                intDataY = 2000

                        Set Ctrl = CreateControl(frm.Name, acCommandButton, , "Page4", "", intDataX, intDataY)
                            Ctrl.Caption = "&Back"
                            Ctrl.Name = "btnBack"

                Set mdl = frm.Module
                    lngReturn = mdl.CreateEventProc("Click", Ctrl.Name)
                    mdl.InsertLines lngReturn + 1, vbTab & "MsgBox " & Chr(34) & " >>> " & _
                    Chr(34) & " " & Chr(38) & " " & Chr(34) & "Hello World" & Chr(34)

' Restore form.
DoCmd.Restore

End Sub      'NewControls()

Place a Tab control in Form2 and name one of the tabs "Page4"

That's it...
 
Last edited:

Users who are viewing this thread

Top Bottom