I am sure this is really simple but i just cant make it work. I have a form with three sub forms. The main form loads and finds the requested record fine and if i run the below from a button on the main form it works fine. What i want to do is have it run after form loads requested record and with out user interaction. I have tried on load and on current but as the form is not yet on screen there are no values to populate the variables and the msgboxes are blank. But like i said fired from a button on the main it works fine.
any help would be greatly appreciated.
actCount = [ActMovDelete].Form![Text93].Value
PlaCount = [PalnMovDelete].Form![Text91].Value
noCount = [NotesDelete].Form![Text50].Value
MsgBox actCount
MsgBox PlaCount
MsgBox noCount
I'm not sure if I understand your question, but if you are you trying to active the code upon initialize, then just
Private Sub UserForm_Initialize()
[your code here...]
End Sub
If however, you are trying to populate variables of forms that are not loaded, then a better approach might be to store the variables as public variables in the forms. This should give you access to the variables even when they are not loaded. Just as an example of how this might work:
In the form from where you want to get the variable (named UserForm1):
Public strFromTheForm As String
Private Sub UserForm_Activate()
TextBox1.Value = strFromTheForm
End Sub
From another form or a module, you can include the code:
Public Sub Test2()
Dim strTest As String
strTest = UserForm1.strFromTheForm
If strTest = "" Then
UserForm1.strFromTheForm = "Test"
End If
UserForm1.Show
End Sub
Related
Quick and easy one (Probably) for you.
My home form has a lot of sub forms within tab controls. Some of these sub-forms have their own sub-form based off a query and that query has criteria input control on the parent form.
As you can imagine upon loading the home form I would have to enter all the parameters in pop-up boxes as the subforms/queries are also loading.
My work-around for this is setting those query sub-forms recordsource to ""
Then once the user control that applies the where condition is updated it changes their record source to the query.
Only, my code (Below) doesn't work. And After half hour of staring at it I can't figure out why, it produces no errors it just does nothing.
All fields/records just have #NAME?
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = qry_SpkrOnboard
Me.Requery
End Sub
I also tried the following:
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = qry_SpkrOnboard
Me.Refresh
End Sub
Again no results, nothing... All fields/records just have #NAME?
Hopefully I've missed something simple otherwise I'll have to rethink the entire form designs
RecordSource is a string:
Private Sub txt_EventID_AfterUpdate()
Me.txt_forcefocus.SetFocus
Me.sub_SpeakerOnboarding.Form.RecordSource = "qry_SpkrOnboard"
End Sub
To refer to a control on the subform:
Value = Me![SubformControlNAME].Form![txt_EventID].Value
Must be losing my mind. For some reason I can't get this simple procedure to work. It's a pulldown where user chooses what data they would like to see. I just need it to change the recordsource of the subform and refresh it. It's basically just substituting one filtered query for another. Can't seem to access the subform through main form. Not sure if this is the best way, but it's the way I know.
Private Sub PeriodSelect_Change()
If PeriodSelect.Value = "Active" Then
Me!ServiceWindow.SbfmService_Item.RecordSource = Service_Active
ServiceWindow.Requery
Else
If PeriodSelect.Value = "PDI" Then
Me!ServiceWindow.SbfmService_Item.RecordSource = Service_PDI
ServiceWindow.Requery
End If
End If
End Sub
I get Error 438 Object does not support this property or Method. Can't quite figure out what I've missed.
Any help is greatly appreciated.
You're trying to set the RecordSource on the subform control, not the form inside the subform control. Use the Form property to access that form:
Private Sub PeriodSelect_Change()
If PeriodSelect.Value = "Active" Then
Me!ServiceWindow.SbfmService_Item.Form.RecordSource = "Service_Active"
ServiceWindow.Requery
Else
If PeriodSelect.Value = "PDI" Then
Me!ServiceWindow.SbfmService_Item.Form.RecordSource = "Service_PDI"
ServiceWindow.Requery
End If
End If
End Sub
I got query to make some modification that enable user to open multiple instances of the form when they do search data. I got manage to create a new module following Allen Browne guide and now I can open multiple instances of the spreadsheet which is good step forward but I cannot find solution how I can open that specific form.
In module I have got following code:
Public clnClient As New Collection 'Instances of frmClient.
Function OpenAClient()
'Purpose: Open an independent instance of form frmClient.
Dim frm As Form
'Open a new instance, show it, and set a caption.
Set frm = New Form_SurgeriesForm
frm.Visible = True
frm.Caption = frm.Hwnd & ", opened " & Now()
'Append it to our collection.
clnClient.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
End Function
And under my button I replaced bit:
DoCmd.OpenForm "SurgeriesForm", acNormal
Forms!SurgeriesForm.Requery
.. and usef following code:
ModuleInstances.OpenAClient
I recon I need to add / change somehow the code below to be able to open multiple instances but do not have idea how to do it.
DoCmd.OpenForm "SurgeriesForm", acNormal
Forms!SurgeriesForm.Requery
I appreciate for any help with this as i got stuck and do not know how I can achieve this.
I think this is what you want?...
With the button on your SurgeriesForm add this code to the On Click event to open a new instance of the form:
Private Sub Command8_Click()
OpenAClient
End Sub
This will create a new instance of the form each time you click the button.
Next, add a blank combo-box to the form and change it's Row Source Type to Value List.
Add this code to the form:
Private Sub Combo9_GotFocus()
Dim frm As Variant
Combo9.RowSource = ""
For Each frm In clnClient
Combo9.AddItem frm.Caption
Next frm
End Sub
Now, when you click the drop-down it will list each form that you have created an instance of.
Finally, add this code to the form:
Private Sub Combo9_AfterUpdate()
clnClient(Combo9.Value).SetFocus
End Sub
Now, selecting a form name from the combo-box will move the focus to that form.
NB: You'll need to update the name of the controls to match yours.
NB2: Remember to open the first form using OpenAClient or it won't get listed in the combobox (as it's not in the collection).
I've made a graphical navigation system so the user can easily find the record he/she wants to. I'm using this on multiple forms now, and since I'm going to use it on another form, I want to standarize the code into a class instead of copy-pasting the VBA code. This way I can improve the code in one place, instead of doing the same change on all the forms.
This is how it works right now:
Dim v As New clsNav
Set v.button1 = Me.button1
Set v.button2 = Me.button2
v.init
And in v.init, I want to set up all the events like click. So when the user clicks button1, it should run a specified method.
How can I do that?
The events for the button clicks will look like this
Private Sub Button1_Click()
' Your code
End Sub
And they are located within the code found behind the respective forms. To be able to reuse code you simply write a sub in a separate module and then call it in the event.
Private Sub Button1_Click()
call MySub()
End Sub
' This is in a module
Private Sub MySub()
' Your code
End Sub
Now this works as long as the code you have to run doesn't use controls specific to that form. If you do need to write code like that, its simply a matter of passing a control to the sub, instead of calling it by its Name
Example. Lets say when we click our buttons it updates a TextBox with today's date. The textboxes have different names on each form. txtDate1 on Form1 and txtDate2 on Form2. So how that will look is
'Form 1 Button
Private Sub Button1_Click()
call MySub(txtDate1)
End Sub
'Form 2 Button
Private Sub Button2_Click()
call MySub(txtDate2)
End Sub
' This is in a module
Private Sub MySub(t as TextBox)
t.Text = Date()
End Sub
If you're trying to do this during run time
How to add events to Controls created at runtime in Excel with VBA
seems like a good place to start. I can't imagine a situation where this would be worth the effort.
I have a generic class I use called frmCtrl, which has WithEvents pointers set up for different control types, with the corresponding events (click, dbl-click, etc...). I then have a function on that class to "set up" the object, so I pass the control in, and the function assigns it to the object pointer of the correct type. This routine also has a parameter for what function to call on which object, so it knows how to respond to the event.
So when I build a form that uses dynamically-added controls, I just create one of these objects for each of the controls, and set it up to call code within the form. The code in the form takes an argument of just the frmCtrl object, so it can easily see which control fired the code, and go from there. I work in Visio, so I'm able to also call a function by module and function name, so the class can handle that too. You'd have to work in how to make such a call using Access, though.
This approach can be used when your controls have been added via the VBIDE, but it may not be worth the effort to set this class up for that, when you can just use the method in the other answer here.
I have a navigation form with two subforms that looks something like this:
When the button in Subform A is clicked, I would like to call a method from Subform B. The method is defined like this:
Public Sub MyMethod()
Debug.Print "MyMethod called"
End Sub
I tried using Forms!SubformB.MyMethod but I get the error Database cannot find the referenced form 'SubformB'. Referring to this page, I also tried Forms!NavigationForm!SubformB.MyMethod but then I get Database cannot find the referenced form 'NavigationForm'. Does anyone know how to do this properly?
Thank you.
Function and Sub procedures defined in the class module of a Form are considered to be private to that form. If you want a Sub that can be called from several different forms then move that Sub to a "regular" VBA Module (i.e., one that is created when you choose Insert > Module from the menu bar in the VBA editor) and be sure to declare it as Public.
Here is a snippet of code I have that toggest the AllowEdits method of a subform when clicking a button on the mainform. Pretty sure this is what you are after.
Function ToggleEditStatus_Child(WhichForm, WhichChild As String)
'Changes the AllowEdits setting of the SubForm parameter
Dim Cform As Object
Set Cform = Forms(WhichForm).Controls(WhichChild).Form
Cform.AllowEdits = Not Cform.AllowEdits
Forms(WhichForm).Refresh
Cform.Refresh
Forms(WhichForm).Controls(WhichChild).SetFocus
Set Cform = Nothing
End Function
I did figure this out but there's a catch - the other form has to be open. This worked for me though so I'm posting the solution here.
First, navigate to the form:
DoCmd.BrowseTo ObjectType:=acBrowseToForm, _
ObjectName:="SubformB", _
PathToSubformControl:="NavigationForm.NavigationSubform", _
DataMode:=acFormEdit
Then make a pointer to the subform:
Dim f As Form_SubformB
Set f = Forms("NavigationForm").NavigationSubform.Form
And you are free to call the method:
f.MyMethod()
Note: you will most likely have to make the method Public for this to work.