Changing a control's value in a particular Tab of a TabStrip - vba

Basically, I have a multipage for different departments of information called mpageTabsMain.
Inside each page of the multipage is a tabstrip, that contains tabs that represents different people.
What i'm doing for testing right now is using a button click to fill in a textbox's text on my 2nd page of the multipage.
mpageTabsMain.Pages(1).TextBox1.Text = "test"
However, what this does is it fills in the TextBox1's text for every Tab (person) in the TabStrip.
How do I refer to a particular tab (person)'s control in a TabStrip so that only that one is filled?
Thanks.

1) You can't have the same control names within a UserForm.
2) If you want to handle different controls for each page (e.g. TextBox1 on page1, TextBox2 on page2) you should use a Multipage control and maybe this would be the better choice for your requirements.
3) A Tabstrip control makes sense for pages with a similar or identical layout, but uses ONE set of controls only. So if you assign a value to your TextBox1.Text property without further conditions
(i.e. distinguishing between the different kinds of pages) you'll get the same text for the same TextBox1 control independantly of which page you are addressing. Though a Tabstrip can possibly
facilitate to write comparable code for only a few controls (distinguishing at which page you are), it could be the harder part to code.
Addendum (refers to comment below)
OK, you are using a TabStrip within a Multipage and apparently you want to display a tabstrip tab for every person. (BTW, I wouldn't do that, you could just change the tab name dynamically, but that isn't the question).
There is no more than one unique TextBox1 control in the whole UserForm. As TextBox1 is a unique Name, and instead your long code line it's sufficient to assign a string value via Me.TextBox1.Text = "test content for Person A" referring to a current tab view of Person A or TextBox1.Text = "test content for Person B" according to another tab view and another tab value. But it's the same TextBox1 in any case. Me stands for the UserForm itself inside the Userform code module.
You only 'fake' the view to this text (control) depending on the tab situation and are responsible to write it back properly or save it according to the actual tab view :-;

Related

How to add existing winform to multiple tabpages

I want to add the same Form in project to five tabpages in TabControl.
This is what i have tried
For i As Integer = 0 To 4
'XtraTabPage instance (DevXpress)
Dim page As New XtraTabPage()
'ExamsTab is the existing form that i want to add tho the page
Dim fm As New ExamsTab
fm.TopLevel = False
fm.Dock = DockStyle.Fill
page.Controls.Add(fm)
fm.BringToFront()
fm.Show()
'Adding the page to the TabControl in Exams_Class
Exams_Class.ExamsTabControl.TabPages.Add(page)
Next
This only adds the form to the last tabpage but doesn't add to others.
If that didn't work I'd try page.Controls.AddRange(fm.Controls) but I'm not in a position to test it.
In the past when I've wanted to keep my code better organized into one form per tab I've:
made a new form
put a tabcontrol on it with a single page, call the control TheTabControl for example
put all the controls into the tab page, put all the events etc in the form code
put a line of code that retrieves the tab:
Public Function GetTab() As TabPage
Return Me.TheTabControl.TabPages(0)
End Function
on the main form, with the one tabcontrol that should contain all the other tab pages, have a code like:
MainTabCobtrol.TabPages.Add(New SomeOtherForm().GetTab())
This way all the controls and their event handlers stay in one form (a good thing) but you can combine them together into one Tab Control on another form. Putting them inside a tab means you can get an idea of how to size it, and you can specify in the designer other things like the tab name etc
From a code organising perspective, it's something the designer can more easily cope with than having all the TabPages in one. Control and all the event handlers etc (makes for a fairly monster form code behind and it doesn't respond well to being split into partial classes)
If you want to add a tab control to the existing form the Document Outline window might be helpful for moving the controls into it with drag and drop. You could also cut all the controls, add a tab and paste, but check the eventbhabdlers are all still wired up afterwards- I find that sometimes this makes them go missing

How to dynamically open a form within a tab?

I have a parent form with a tab control. The tabs show subforms. Data shown in the parent form is general details about residential property, such as address, etc. In the tabs, the subforms show data that is county-specific and exhaustive, like garage square footage and on and on.
There are 6 tabs with a static subform i.e. that tab always shows the same subform. I'd like the 7th tab to dynamically open a subform depending on which county the property exists in. That is, based on the value of a control (Me.Form.County), populate this 7th tab with the corresponding subform for the highly specific details for that county.
(No, it's not desirable to make one subform that could render data for any county; it'd be huge, relatively. One form per county is the requirement.)
It'd be nice if there's a way to act on the event of clicking on the 7th tab to then open the county-specific subform, though I don't see OnClick for a tab. If there's a way that's close to that simple for the user that'd be fine too.
You can use the Subform.SourceObject property to change which form is displayed in a Subform control.
https://learn.microsoft.com/en-us/office/vba/api/access.subform.sourceobject
You can use the Change event of the Tab Control to respond to the user switching tabs and display the correct subform if they are looking at the desired tab.
Private Sub tabCounty_Change()
If tabCounty.Pages(tabCounty.Value).Caption = "County Info" Then
subCountyInfo.SourceObject = "frmCounty" & Me.Form.County
End If
subCountyInfo.Form.Requery
End Sub

Word document text boxes "remember" what was in them previously

I am using a VBA userform to randomly generate values to be sent into a Word document to provide random questions on various math topics (for my students).
This seems to work well except that when I exit the userform (me.hide) and the values are sent and focus is back in the document, the previous values are still briefly visible every time I move the mouse.
This only happens for an instant. The correctly sent values appear if there is no mouse (or window slider) activity taking place. If I view another application and do some activity there and then return to the Word document the display is fine as well.
The flipping of values is visually annoying. Has anyone out there encountered a similar issue?
To reproduce:
Create a Word document with an ActiveX Textbox and button. The button activates a userform which also contains a Textbox and a button to send the Textbox value back to the Textbox in the document.
In my case, when the button in the document is clicked the form opens and the value does get returned to the Word document, but when rolling the mouse or moving the slider at the edge of the window the previous value in the text box briefly flickers into view. Here is the code from the document button:
Private Sub cmdOk_Click()
ThisDocument.TextBox1.Value = TextBox1.Value
Me.Hide
End Sub
I am using Word 2016 and Windows 10.
I tried to use the "new" Word textboxes but I do not know how to communicate with them from a userform. It is so simple using the "old" text boxes.
ActiveX controls were designed for VBA UserForms. It's possible to insert them on the surface of a Word document or Excel spreadsheet, but they don't always behave optimally. Content controls (and the legacy form fields), on the other hand are designed for the surface of a Word document.
In order to write to a content control you can use code like this:
ActiveDocument.SelectContentControlsByTitle("name").Item(1).Range.Text
Closer to what you're trying to do with identifying the content controls could be:
ActiveDocument.SelectContentControlsByTitle(TextBox1.Name).Item(1).Range.Text = TextBox1.Value
Why it's so complicated: The team that designed content controls wanted to avoid the problems people ran into with bookmarks and form fields, that a name could not be duplicated in a document. It's possible to give the same name to multiple content controls. So it's not possible to identify a content control using Document.ContentControls("name").
Instead, Document.SelectContentControlByTitle needs to be used, which returns an array of content controls. If there's only one (or if you want only one), then you can add on .Item([index]) to get the single content control directly.
Rather than type in a static name, you can query the UserForm's textbox control for its name, if that makes things easier for you.

HowTo Dynamic ToolTip or changing tooltip on one button

I have a small VB winforms app with a forward and back button. These two buttons allow you to cycle through numerous "pages". I would like to display the next and previous page title in tooltips when the mouse hovers over the buttons(arrows).
Example, when hovering over the left arrow button, I would like to display the name previous page.
So page 1 is applications, page 2 is emails, page 3 is pdf documents etc...
I added the tooltip property from the toolbox in Visual Studios, but it only allows me to type a single item as opposed to scrolling through my list of pages and determining what the previous page name is and displaying it.
The page or form itselt doesn't change, and neither do the buttons, just a panel contained inside the form has the "pages" that change, so I need to be able to write a function that figures out which page is previous and which page is next on the fly when mousehovers the buttons in question... Then returns the data so I can display it in the tooltip.
Better Question;
How can I pass any variable into a tooltip instead of setting it as a property.
Given a tooltip named ToolTip1, then you can set variable text to a control, like this:
Dim myToolTipText = "Dummy text"
ToolTip1.SetToolTip(Me.Button1, myToolTipText)
I wanted to assign the button's text to the tooltip title at runtime. I used the following code:
Private Sub ToolTip1_Popup(sender As Object, e As System.Windows.Forms.PopupEventArgs) _
Handles ToolTip1.Popup
ToolTip1.ToolTipTitle = e.AssociatedControl.Text
End Sub

Option Group frame: can I add text boxes that are part of the frame instead of rad button options?

Ok so this maybe a simple/silly question but I don't know so here goes:
In access let's say I want to have a frame control, so I click the option group button and add it to the desgin surface. However, I am not wanting to use this as a option group with radio button selection, instead I would like to add text boxes instead the frame, so that when I reference the frame, it references every control instead of it, hence the text boxes, cbo boxes, etc.....just as it would if they were radio option selections.
So can you do this?
I want whatever controls I add inside the frame to be easily referenced (i.e. make all controls visible just by using frameExample.visible = true) so that I can build my own tab control groupings.....
can this be done?
Thanks!
EDIT:
What I am trying to accomplish is having a form that includes a collection of controls (input controls - cbo boxes, text boxes, etc), that serve as the Main record information. These are saved to a table via an INSERT statement on button_click because this form is unbound.
Next I have 8 categories that are relative per each main record (and data that goes along with it). Each of these categories could have a sub form area and a button click that bring it's relative form into the sub form area. These sub forms would be unbound as well as I would just save data via SQL statement. So i know I could accomplish this by running the insert statement from the parent form, on the main collection control's data that would create the KeyID number, then run a SQL statement that would turn around and load that KeyID number right back onto the page in a hidden text box.
Then when I click one of the sub forms and load its relative collection of controls, I could then save that data along with KeyID for each of these sub-forms/tables.
SO......
I was wondering if instead you could define these controls as a collection so that you could hide and make visible all the ones you need on button clicks and avoid the need for additional forms (subs). I know that if a user enters data into a text box, and then somewhere along the way that box becomes hidden, the data still exists in it and still ends up in the SQL statement....
So I want all these controls to exist on the same form, but I thought what is I could encapsulate them into a frame like an option group, then I could call the frame and all the relative controls would be called up (made visible) as needed.
Sorry for the long explanation but I thought it would help.
I do not think you can do it with an Option Group, but what you are describing is pretty much a subform, yesno?
Some examples of hiding the tab control from an app that went live in March 1998:
Tab driven by transparent command buttons over labels styled to look like colored command buttons:
Same approach, more buttons:
In this case, fake colored command buttons don't drive the tab, but insted show/hide the tab and a subform. In this case, the tab is actually driven by the listbox:
A view of when the tab is hidden and the subform revealed. The listbox drives navigation within the subform, which has a visible tab on it:
So, there's a lot that can be done without showing the tab control.