I am working with MS Access and I am currently trying out the navigation sub-forms. However I am finding it difficult to understand how to simply change the recordsource of a sub form. One of the tabs within my "NavigationSubform" is called "nbCustomerList", which has the target navigation name "CustomerList". Within the CustomerList form, there is a button which when clicked opens a popup which allows you to filter the query on CustomerList. How do I achieve a change to recordsource from an event like this?
Private Sub btnSearch_Click()
On Error GoTo HandleError
If CurrentProject.AllForms("MainMenu").IsLoaded Then
[Forms]![CustomerList].RecordSource = CustomerListFilter()
[Forms]![MainMenu]![NavigationSubform].Requery
End If
''ErrorHandling'''''''''''''''''''''''''''''''''''''''''''''''''''''''
HandleExit:
Exit Sub
HandleError:
MsgBox (Err.Number & ": " & Err.Description)
Resume HandleExit
End Sub
The following test worked for me:
Forms![Navigation Form].NavigationSubform.Form.RecordSource = "SELECT * FROM Rates WHERE ID=2"
Assuming your form design has the default names of [Navigation Form] and NavigationSubform assigned by Access, in your db try:
[Forms]![Navigation Form].NavigationSubform.Form.RecordSource = CustomerListFilter()
Requery command was not necessary.
I don't use Navigation Form design. Keep in mind that no matter how many tabs are set up, only one subform is available at any time. Nature of Navigation Form is that it loads and unloads subforms as called by click on tabs.
Related
In my Clients form I have 2 textboxes (tbHHName, and tbHHID) and a navigation control (nav) displaying subforms/subreports.
When the user clicks tbHHName, a modal form opens to select which client to view.
Before switching clients, the form shows:
tbHHName: Client123
nav: Account1, Account2, Account3
After switching to Client456, here's what it displays:
tbHHName: Client456
nav: Account1, Account2, Account3
Here's what it should be displaying:
tbHHName: Client456
nav: Account4, Account5, Account6
After the user clicks on one of the nav tabs it displays correctly, but I can't figure out how to force refresh the nav control without that click. Any advice would be very much appreciated.
All of the nav's subforms/subreports have the NavigationWhereClause set to: "[HH_ID] = tbHHID"
Below is the Form_Load, and the tbHHName_Click subs
Private Sub Form_Load()
On Error Resume Next
'Set form recordsource
Me.RecordSource = "SELECT * FROM _HOUSEHOLDS WHERE HH_ID = " & GetSetting(AppName, Reg_JBGeneral, Reg_HH_ID, 0)
'Apologize to users
MsgBox "Please click on the Holdings tab again to refresh the data." & vbNewLine & _
" Sorry for the extra click," & vbNewLine & _
" -MaybeOn8", , AppName
End Sub
Private Sub tbHHName_Click()
On Error Resume Next
'open the form that sets new HH_ID value using the SaveSetting method
DoCmd.OpenForm "Households_Select", WindowMode:=acDialog
Form_Load
End Sub
Thanks in advance, SO
I tested with UNBOUND combobox sitting on main form to select filter parameter and NavigationWhereClause has criteria expression HH_ID=Forms![NavForm]!Combo9. Code in combobox AfterUpdate event:
Forms![NavForm]!NavigationSubform.Requery
Then I tested with code to change value of combobox and requery.
Forms!NavForm.Combo9 = 17
Forms!NavForm.NavigationSubform.Requery
This all works and does not involve changing RecordSource. Now just decide where to place code that changes control's value and requery subform.
Also, can use DoCmd.BrowseTo method to set focus on particular Navigation tab.
Found a fix. I tacked this onto the end of the Form_Load sub:
Forms("Households").NavigationSubform.Report.FilterOn = False
Forms("Households").NavigationSubform.Report.FilterOn = True
Even though there are mixed subforms and subreports in the navigation control, everything seems to load fine.
Is there anyway to apply a filter (without using SQL script), when using a navigation form?
When I do the docmd.applyfilter outside of the navigation form it works.
When I move the form to my navigation form it no longer works - I have looked at how I reference my form etc.
Code I have is
Dim BusinessTermLookup As String
BusinessTermLookup = Me!CmbSimilarMatch
Me.Filter = "[businessterm]like ' & BusinessTermlookup '"
Me.FilterOn = True
I tried to use docmd.applyfilter - but it gives an error
The action or method is invalid because the form or report isn't bound to a table or query
Filter text should be "[businessterm] Like '*" & BusinessTermlookup & "*'" and make sure that the filter in navigation form applied to navigation form subform control, not to the form object you use as navigation form page. Each time when you click navigation button, it replaces form object in subform control dynamically. VBA code should work fine if called from inside the form object used as page.
In access I am trying to not let the user exit a textbox unless they have filled it out (i.e. not left it null). This is to be done in a function in order to cut down on code. In VBA is there some way to stop the user from exiting the function? (I know about setting the focus but I need the code to work from a multitude of different textboxes)
For reference my current code is as follows;
Function Reload()
If IsNull(EmployeeID.Value) Or IsNull([First Name].Value) Or IsNull([Surname].Value) Or IsNull(DOB.Value) Or IsNull(Position.Value) Or IsNull(Position.Value) Or IsNull(Mobile.Value) Or IsNull(Email.Value) Or IsNull(Address.Value) Or IsNull(Suburb.Value) Or IsNull(Postcode.Value) Or IsNull([Start Date].Value) Or IsNull(UserLogin.Value) Or IsNull(UserPassword.Value) Then
MsgBox "Please fill out all fields"
Else
DoCmd.RunCommand acCmdSaveRecord
Form.Refresh
End If
End Function
Thanks
The best technique I know is to use the BeforeUpdate event to run data validation. Using your if statements if the data is not valid then set
Cancel = True
and the data will not write. You would probably prefer to disable the default record navigation and form close buttons and use custom buttons so that you can trap the invalid data error and prevent moving to a new record or closing the form. But even if you allow the user to use the built in navigation buttons or close button Access will throw up error messages about being unable to save the current record and prompt the user if he wants to proceed.
You can also go a step further and captue which fields are invalid and present a message box.
Msgbox ("The following fields were left blank and must be entered to save this record:" & vbCrLf & "Field 1" & vbCrLf & "Field 2")
A little modification of the text input limiter i use in our chat room should help. New function I have called it MustINPUT and call it on the LostFocus of the text boxes that you don't want to move from with out input.
Private Sub MyTextBox_LostFocus()
Call MustINPUT (Me.MyTextBox, 0)
End Sub
Just change the message for your users.
Sub MustINPUT(ctl As Control, iMaxLen As Integer)
'Call MustINPUT(Me.txtSayThis, 0)
If Len(ctl.Text) = iMaxLen Then
MsgBox "Please Type something", vbExclamation, "Input Needed"
ctl.Text = Left(ctl.Text, iMaxLen)
ctl.SelStart = iMaxLen
End If
End Sub
Hope this help. DR ,
I have the following code on a subform's on current, but it errors on open of the main form:
If IsNull([Forms]![frmMContacts]!ID) Then
With Forms!frmMContacts!frmMContacts_SubPeopleContacts.Form
.Enabled = False
End With
Else
With Forms!frmMContacts!frmMContacts_SubPeopleContacts.Form
.Enabled = True
End With
If Me.NewRecord Then
Me.txtCurrRec = "New Contact Role Record"
Else
Me.txtCurrRec = CStr(Me.CurrentRecord) & " of " & _
DCount("ID", "tblContactPeople", "FKClient = " & Me.Parent.CM_CID) & " Contact Roles"
End If
End If
The point of it is, simply to update a text box in the form's footer, to provide a count of records for the sub form. The issue is that when a user opens the main (parent) form, from the main menu, the main record has no record, and the sub form is still loading. In reality, I'm hiding the detail of the main form, and hence this sub form is hidden. The user has to hit 1 of 2 buttons in the main form header. Either find a record (pop-up form) or start a new one (another pop-up form).
When I try to open this main form, I get the error "run-time error '2465' application-defined or object-defined error".
I'm not sure how to trap this and just either make the sub-form disabled or prevent this on-current code form firing, until the parent has a record.
Thanks for any help!
I have a Form named frmSearchAirport
I have two tab controls on the form named TabControl1 and TabControl2.
Each tab control has five pages.
On another Form i have command buttons.
I want to use each command button to open a page on a TabControl and to make the inactive TabControl invisible
I wrote this code for one of the command buttons so that it would open the first page on TabControl2 and and make TabControl1 invisible but i get an error message saying the action or method requires a form name argument
DoCmd.OpenForm FormName:=frmSearchAirport, View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControl1.Visible = False
and this code for the On Load event of the Form named frmSearchAirport
Private Sub Form_Load()
If IsNull(Me.OpenArgs) = False Then
Me.TabControl1 = Me.OpenArgs
Me.TabControl2 = Me.OpenArgs
End If
End Sub
Can anyone help with this.
Thank you
Edit 1:
Ive since changed the code to this and im getting an Application Defined or Object Defined error
DoCmd.OpenForm FormName:="frmSearchDublinAirport", View:=acNormal,OpenArgs:=0
Forms![frmSearchDublinAirport].Form.TabControl1.Visible = False
Edit 2:
ive changed the code to this. The form is actually opening on the correct page but the other TabControl is still visible and the pop up error message appears saying Application Defined or Object Defined error
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControl1.Visible = False
Edit 3:
This code is working. the problem was I named the TAbControl incorrectly. I used TabControl1 instead of TabControlOne
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControlOne.Visible = False
The FormName parameter of DoCmd.OpenForm needs a string - the form name.
So you probably need:
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0