How do I load something when the page is selected? - vba

I'm trying to make a form with a lot of pages. So far it works ok and I added the controls from the code. There are hundreds of them and it makes the app "heavy".
Now I'm adding all the stuff in the comboboxes in at Initialization but what I want to do is to load the Controls when I change the page.
So when I start the form the controls should be loaded for page 1. What I want to do is when I click on Page 2 of the Multipage to load it's components (instead of adding all of them at the initialization of the UserForm).
Thanks!

If you want to initialize each page only when it becomes clicked, you can track which has been clicked using something like;
Private mbInitialised() As Boolean
Private Sub UserForm_Initialize()
ReDim mbInitialised(MultiPage1.Pages.Count - 1)
SetupPage 0
End Sub
Private Sub MultiPage1_Change()
SetupPage MultiPage1.Value
End Sub
Private Sub SetupPage(index As Integer)
If (mbInitialised(index)) Then Exit Sub
mbInitialised(index) = True
MsgBox "init page " & index + 1
'//setup here
End Sub

I think this is an easier way to do it
Load UserForm: UserForm.MultiPage1.Value = 0: UserForm.Show
where the multipage value starts to "page1" as arrays starts from zero
if you want a different page change .Value = 0 into the page value - 1
ex:
Load UserForm: UserForm.MultiPage1.Value = 4 : UserForm.Show
this will take you to "page3" of multipage upon initializing userform
*edit:
misunderstood the question a bit, my code will take you to the page but not execute something when changing pages, use Alex's code instead

Related

Access cascading combobox autofill When left with one option

I have ~50 cascading comboboxes on a form that I want to autofill if only one option is left. I found some code that worked for cboTwo (second combobox), but the other comboboxes aren't filling in automatically. I still have to use the drop down menu to make a selection. Is there any way that I can make all of my comboboxes autofill if there is only one option left in the drop down? I'd prefer some sort of macro help because that's what I've been using until now, but I'll use VBA if necessary. Thank you for all of your help!
Private Sub cboOne_AfterUpdate()
Me.cboTwo.Requery
If Me.cboTwo.ListCount = 1 Then
With Me.cboTwo
cboTwo.SetFocus
cboTwo.Value = cboTwo.ItemData(0)
End With
End If
End Sub
Private Sub cboTwo_AfterUpdate()
Me.cboThree.Requery
If Me.cboThree.ListCount = 1 Then
With Me.cboThree
cboThree.SetFocus
cboThree.Value = cboThree.ItemData(0)
End With
End If
End Sub
The problem may be with misunderstanding Access control events. Unlike in many other languages, control events are rarely triggered by changes made in VBA code. In other words, the event handler cboTwo_AfterUpdate() is not automatically called when cboTwo.Value = cboTwo.ItemData(0) is executed in code, so there will not be any automatic event cascade. Try the following pattern:
Private Sub cboOne_AfterUpdate()
Me.cboTwo.Requery
If Me.cboTwo.ListCount = 1 Then
With Me.cboTwo
cboTwo.SetFocus
cboTwo.Value = cboTwo.ItemData(0)
cboTwo_AfterUpdate
End With
End If
End Sub
Private Sub cboTwo_AfterUpdate()
Me.cboThree.Requery
If Me.cboThree.ListCount = 1 Then
With Me.cboThree
cboThree.SetFocus
cboThree.Value = cboThree.ItemData(0)
cboThree_AfterUpdate
End With
End If
End Sub

access make list field visible when clicking Button

In an access form, I try to make a list field visible when the fokus is on another textfield in which new data should be filled in. The backround is that one should know the last data inputs to create a new one.
As a first step I tried to make the list (liste91) visible when clicking on a button, but I failed using the following code.
Private Sub Befehl97_Click()
Forms!projects!liste91.SetFocus
Me.liste91.visible = True
End Sub
I get error in the line Me.list91
what is wrong?
thank you for your help!
You can't set focus to something not yet visible. Just switch the order:
Private Sub Befehl97_Click()
Me.liste91.visible = True
Me.liste91.SetFocus
End Sub

VB.NET Call Sub of another form

I know, that there are many questions related to this, but still I cannot find a workable solution.
Usually, it would work like this: A form creates an instance of another form in it's container like this:
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.TopLevel = False
PolInstIn.Name = "Sub From"
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
Me.GroupBox6.Controls.Add(PolInstIn)
PolInstIn.Show()
End Sub
Then it's easy to call a Public Sub from the sub form like this:
Call PolInstIn.MyPublicSubInSubForm1()
However, this doesn't work for me in this case. When I run MyPublicSubInSubForm1() it doesn't throw any error, but does no action. If I write a value to SubForm1 textbox and read it back, it reads, but I don't see it on the screen, so I suspect it is written to some other accidental instance.
I suspect it is because my parent form is also an instance of an form created in very similar way like SubForm1. Basically the ParentForm is a form loaded into tabPage and SubForm1 is a module loaded into ParentForm. It can exist in many copies (tabs).
Could you point to any simple solutions?
Regards,
Libor
I see this question got a lot of views, so here is an answer.
1) No visual response of child form (only results) - this could have happened if I created more then 1 instances of the form. The example is just an example, but if one use it (accidentally) this way, it may result in new definition of a child form every time (and consequent symptoms like the ones described). In practice, I split form loading from loading data into to the form (done by a public sub in that form).
2) If you want also a back reference (to i.e. parent grid form), define a Public ParentFormGrid as GridName (note ParentForm is a reserved name) and on loading a child form, set
PollInstIn.ParentFormGrid = Me
This way you can alway asccess the parent form, i.e. reload the grid when you save changes on a line edited in child form.
make Private Sub LoadDetail() to a public :
Public Sub LoadDetail()
It work on my project. Hopely its what you want
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.Name = "Sub From"
PolInstIn.Show()
PolInstIn.TopLevel = False
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
PolInstIn.Update()
PolInstIn.Refresh()
Me.GroupBox6.Controls.Add(PolInstIn)
End Sub

Problems when calling a public sub

I'm facing a deadend When trying to call this sub :
Public Sub backblue(ByVal frm As Form, ByVal boxname As String)
For i = 1 To 3
CType(frm.Controls(boxname & i.ToString()), TextBox).BackColor = Color.LightBlue
Next
End Sub
with button click event :
Private Sub Button1_click and bla bla....
backblue(Me, "txb1_")
End Sub
Can anybody show me a suggestion to fix the code.
It throws "Object Referrence not set to an instance bla bla" error
For information the textbox names are :
txb1_1 , txb1_2 , txb1_3
(these are some of the many textboxes in the form that i want its bakcolor changed)
and these three textboxes are already created through designer, not from execution.
i did check the textboxes names and there's nothing wrong.
the form class is also public.
if they are the only textboxs on said form you can just loop through
For Each box as Textbox In frm.Controls
box.BackColor = Color.LightBlue
Next
This error will occur if you do not declare the Form class to be public.
Also, make sure the textbox names are really correct, although this will probably cause a different error.
If you create the textboxes during execution, make sure they are initialized with New and added to the form's Controls collection.
Try this....
Public Sub backblue(ByVal frm As Form, ByVal prefix As String)
For i = 1 To 3
Dim bxName as String = prefix & i.ToString()
Dim bx as TextBox = CType(frm.Controls(bxName), TextBox)
If bx Is Nothing Then
MsgBox("Unable to find text box " +bxName)
Dim mtch() As Control = frm.Controls.Find(bxName, true)
If mtch.Length> 0 then
bx = mtch(0)
Else
Continue For
End if
End If
Bx.BackColor = Color.LightBlue
Next
End Sub
Although, a better solution would be to either create the textboxes inside a control and pass that control to BackBlue or to create an collection that has the controls and pass that in. Which brings up what is most likely yor problem your control is contained in a sub component and thus is not in the main form control collection
Alternative, you could use either the tag of the control or create a component control that implements IExtenderProvider and add it to the form --all of the above would effectively allow you to define the controls and/how they should be handled at designtime.
It may really seem that the names generated by this loop may not be the names of the original textboxes. My suggestion is before setting this Color property verify that the names generated by this loop are indeed the actual names. Maybe output this in a messagebox:
MessageBox.Show(boxname & i.ToString()) for each loop before you set the property

SetFocus inside a GotFocus procedure initiated by another SetFocus

Objective: Redirect focus from one command button to another using the first's GotFocus procedure.
Context: I have a form-independent procedure in a generic module that, on most forms, sets focus to the NewRecord button after saving the previous record. But on one form, I would like to redirect (based on certain conditions) focus back to the SignRecord button so the user can "sign" a second part of the same record (I may need this for other uses in the future). The target control is enabled and visible and can otherwise be focused and the original control can be focused when the redirect doesn't occur. Reference [2] below implies that this should be possible, though I'm not changing visibility of my controls.
Issue: When the conditions are met to redirect focus in the GotFocus procedure, it redirects as desired but the original (test) SetFocus call throws a "Run-time error '2110', Can't move focus to the control CommandNew".
What I've tried:
Exit Sub after my downstream SetFocus calls.
Call CommandSign.SetFocus in the hopes that it would make it happen outside the previous SetFocus process.
In a module,
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus 'This gets the error '2110'
End Sub
In the 'TargetForm',
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
CommandSign.SetFocus
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
CommandSign.SetFocus
End If
End Sub
References:
[1]: SelectNextControl() a bad idea in a GotFocus event?
[2]: http://www.access-programmers.co.uk/forums/showthread.php?t=100071
I think your problem is that the call to Forms("TargetForm").CommandNew.SetFocus doesn't quite seem to, in fact, finish setting the focus to CommandNew until after Private Sub CommandNew_GotFocus() has finished executing. Because you've called another SetFocus before the first SetFocus could finish, there is a conflict that Access seems to be unable to cope with.
Whether or not that is the case, one thing is clear: the way you have your execution plan set up right now is unfortunately not going to work. You might try adding either a global variable or a public variable to each form that determines whether or not you should set your focus to CommandSign after you set the focus to CommandNew.
Ex. TargetForm:
Public boolSetCommandSignFocusInstead As Boolean
Private Sub CommandNew_GotFocus()
If IsNull(textDateTime) Then Exit Sub 'Works as expected
'I can see these two parts work. The framSign value changes
'and CommandSign gets focus
If checPPC And IsNull(textSigID_PPC) And framSign = 2 Then
framSign = 1
boolSetCommandSignFocusInstead = True
ElseIf checDAS And IsNull(textSigID_DAS) And framSign = 1 Then
framSign = 2
boolSetCommandSignFocusInstead = True
Else
boolSetCommandSignFocusInstead = False
End If
End Sub
Module:
Public Sub test()
Forms("TargetForm").CommandNew.SetFocus
If Forms("TargetForm").boolSetCommandSignFocusInstead Then
Forms("TargetForm").CommandSign.SetFocus
End If
End Sub