master detail subforms in ms access 2010 - ms-access

I have a customers table in an MS Access 2010 database. I want to create a form with two subforms which together allow the users to select a customer and see more detail about the customer.
The customers table looks like:
CustomerID
FullName
Address
City
StateProvince
Other fields
The mainForm I created in design view by dragging CustomerListForm and CustomerDetailForm onto MainForm.
CustomerListForm is on left side and lets users filter customers from long list
Contains the following controls:
txtFilter textbox with embedded macro where = [LastName] Like [Forms]![CustomerListForm]![txtFilter] & "*"
FullName textbox and hidden CustomerID textbox which are bound to CustomersTable
CustomerDeatilForm located on right, with contents changing based on selection from CustomerListForm
If no customer selected from CustomerListForm, show default message
Else:
Show FullName, address, city, and stateprovince of customer selected from CustomerListForm
txtFullName set =[CustomersTable]![FullName], and so on for other fields
When I run my CustomerListForm separately, it does successfully allow a user to filter customer records by typing in a name. But that filtering ability goes away when I embed CustomerListForm in mainForm. Also, my filtered results in CustomerListForm do not contain any sort of links, which means that CustomerDetailForm does not seem to be able to identify which customer it should be outputting data about.
Can anyone show me how to set this up? I think if I get this much running, I will be able to fill in the blanks from other research that I am doing.
EDIT:
I see that the FullName textbox on CustomerListForm has an OnClick method. FullName is not editable in CustomerListForm. CustomerID is a hidden field alongside each record in CustomerListForm. I also see that there are multiple hyperlink settings in the format tab of the property sheet for the FullName textbox. Is there some way that the OnClick method of the FullName textbox can be used to send the CustomerID to CustomerDetailForm? Perhaps in a hyperlink?

The subforms are presumably linked to the parent form by CustomerID?
Remove this value from the LinkMasterID, LinkChildID properties of the subform control (in the parent form - as opposed to on the form properties of the subform itself)
Remove the macro, and create an AfterUpdate event for txtFilter, which finds the relevant data and displays it in the CustomerListForm; In addition to whatever other code is in this procedure, put
Dim CustID as Long: CustID = [get customerID from wherever it is]
... [your other code]
Me.Parent.LockCustomer CustID
In the outer form, add this in vba:
Public Sub LockCustomer(CustID as Long)
CustomerDetailForm.Form.RecordSource = _
"SELECT * FROM Customer WHERE CustomerID = " & CustID
End Sub
If you've customer details of any sort on the main form in addition to the subforms, and you want to show the details of the selected customer in the listform on both the main form and the detail subform, instead of setting CustomerDetailForm.Form.RecordSource, set Me.RecordSource & ensure that the LinkMasterID, LinkChildID properties of the Detail subform control are set to CustomerID.

Related

Use a text box as a wildcard search on a field within a linked SQL Server Table within MS Access

I've managed to successfully build a combo-box that will allow a user to select a record from a list to get an ID from a table. With the table I'm currently working with running to around 60,000 records, it's not realistic to use this method to find the record.
What I want the user to be able to do is enter a name in a text box, and a combo box be populated with the relevant records from the table where one of the fields matches that. So if the user entered 'This' into the text box, the combo box would present records where the field had 'This', 'This and That' and 'this'. It would not present the record that only had 'That' in the field.
Lets say the Text box is called 'txtBox', the combo-box is called 'comBox' and the field in the linked SQL Server table 'LinkedTable' is called 'SearchField'
I would suggest you not use the combo box. Just have a text box in which the user can type in a few characters and hit enter key.
You THEN display a “list” of results that allows the user to select and click on any of the results to “edit” or “view” the given row of data.
In the following screen shot we working with a VERY small table of 500,000 rows. We looking for smith, so we just type in smi and hit enter. The results are displayed instant, and at that point the user can type in “first name” or just a few chars of first name and further drill down and filter.
The form looks like this:
The code in the after update event of the text box is simply:
Dim strSQL as String
strSQL = "select * from tblCustomers where LastName like '" & me.TextSearch & "*’"
me.RecordSource = strSQL
So very little code is required. You could I suppose fill in the results to a combo box, but then the user has to type into a box, then select something from a combo box and then somehow you bring up that record for editing. That’s like 3+ steps for the user.
Just use what Google or most accounting or darn near any computer software does:
A simple text box – you type in a few characters and when they hit enter the results are displayed for the user to pick.
Note in above the user can click on the “glasses” icon button to launch a form that displays the single record. The code behind that button is:
Docmd.Openform "frmEditDetails",,,"ID = " & ME!ID
I went down a different route to the suggested option in the other answer. This met my needs more accurately and might help someone with a similar issue.
First, I amended the query on the Combo box so that it used the value in the text box as a criteria for one of the fields.
In the Criteria for the 'SearchField' within 'LinkedTable' I entered:
Like "*" & [forms]![*FormName*]![txtBox] & "*"
This will restrict the results in comBox to those where the SearchField contains the value entered into txtBox. This doesn't update dynamically however and will only update after the first value entered into txtBox.
To get around this problem, I added the following to the 'On Get Focus' event for comBox
Private Sub comBox_GotFocus()
Dim ctlCombo As Control
' Return Control object pointing to a combo box.
Set ctlCombo = Forms!FormName!comBox
' Requery source of data for list box.
ctlCombo.Requery
End Sub
This will force the combo box to run the query again, using the current value in txtBox, each time it is 'clicked on'
As a result, I get the restricted list of values in the combo box that I need and allows the user to 'search' for a record that can then be used for creating a new record on a form.

Access VBA - Selecting newest option in combobox

I have a database with three tables: Employees, Consultations, and Customers. Here are the relationships:
Employees<--->Consultations
Consultations<--->Customers
I have a form for entering consultations (called "Consultations"), and the user can select the Customer from a combo box. This then displays the customer information on the form in a read-only format. If the user needs to update the customer information (name, type, department, etc.) they can click a button that opens another form to that customer's record. They can update the info, then close that form, and the "Consultations" form is updated with the new customer information. This all works fine.
I also want users to be able to enter a new customer record if they don't exist in the customers table. Currently, the user can click on a button, and a form is opened where they can enter all of the new customer information, called "Add-Customer". Once they close the form, the "Consultations" form is displayed again. Here is where I am having issues.
What I want to happen is that after the user enters the new customer, the new customer should be selected in the combo box. The combo box is holding the customer's "LastName, FirstName". I was able to get the new record to appear in the combo box, but the user still needs to manually select it. I want this to happen automatically.
Here is my code that runs when the user clicks OK on the "Add-Customer" form:
Private Sub Command1_Click()
'save customer record
DoCmd.RunCommand acCmdSaveRecord
'make add-customer form invisible
Me.Visible = False
'requery the customerlastname field on consultations form
DoCmd.Requery "CustomerLastName"
'close add-customer form
DoCmd.Close acForm, "Add-Customer"
End Sub
I tried adding a variable that stores the ID of the new record, then tried to have the combo box select that record, but couldn't get it to work. I've removed that from the above code.
Thanks!
UPDATED: Realizing you are working with a second form - which is not a subform, here is one possible solution.
You will need to pass the new value back to the original form. You can do this in several ways: (a) a global variable; (b) a hidden textbox; (c) create public function in main form. I chose to use a hidden field.
I assume you are opening the ‘Add’ form as modal. If not, then you need to do that.
(You will need to change the following code to use your combobox name and form name)
Add a hidden textbox on the main form i.e. named txtNameHidden
In the close code for the 'Add' form, add the following:
Forms!frmMyMainForm!txtNameHidden = me.txtName ' The value as it will appear in the combobox
In the Main form, after the line of code that opened the form, add the following code:
Me.cboNames.Value = Me. txtNameHidden
I am curious about your code 'DoCmd.Requery "CustomerLastName"' - does this work? How does it know to reference the control on the main form?

Access 2007 Filter Subform Based On Field In Main Form?

I was tasked with creating a simple app to maintain a user's collectibles collection using Access 2007. There were some requests, which I have created and implemented. Those being:
One main form listing all of his collectibles
That same main form has a tabbed control below, with each tab containing a subform that in effect "filters" data based on different criteria from the main form. For example, the 1st subform takes the name of the collectible figure in the main form, and displays all other records using that name in the subform. In other words, if the figure is "Darth Vader", the subform would list all collectibles that have the name "Darth Vader".
I have created the application as per user request, but there is one thing that is bothering both of us so far. The subform's first record is the same as the main form. We both find that redundant, and annoying. Granted, my Access skills is weak at best, so this may be a simple fix, but is there a way to remove the duplicate record in the subform? I have tried implementing a where clause in the subform stating to not include the "Figure ID" in the main form. Problem is, it is acting like a Parameter prompt, asking for the main form's FigureID when I open the subform, or the main form. If I type in the Figure ID, it works, but the prompt is something that is obviously not wanted.
FYI:
The main form is based on a query that basically selects all records from the "Figures" table and other related tables
The subform was created when I dropped the subform control onto the tab control, where I linked the necessary master and child fields
Say you have a form named frmMain. That form includes two fields in its record source: FigureID; and Figure_name. The form also includes a text box control named txtFigureID which is bound to the FigureID record source field.
frmMain also contains a subform control based on a form named frmSub. The record source for frmSub also includes FigureID and Figure_name fields. The subform control's link master/child field property is Figure_name. Therefore frmSub will display all the rows where Figure_name matches the corresponding value in the frmMain's current record.
Now, if you want frmSub to exclude the specific record (as identified by the unique FigureID value) which is the current record in frmMain, add a WHERE clause to frmSub's record source query:
WHERE FigureID <> Forms!frmMain!txtFigureID
I'm only guessing here, but hope that description is close enough to your actual situation to be useful. If not, show us the SQL you're using as the record source for your subform.
Edit: You get the parameter prompt only when you first open frmMain. Afterwards, you can navigate between records in frmMain, and frmSub shows you only the records you want to see ... without asking you again to supply a parameter value.
The reason that happens is because the subform loads before its parent form ... so a control on the parent form is not available when the subform loads.
I think the cure may be to save the subform without the WHERE condition in its record source. Then, when the main form loads, it can re-write the subform's record source to include the WHERE condition.
So, in frmMain's load event:
Private Sub Form_Load()
Dim strSql As String
strSql = "SELECT FigureID, Figure_name FROM YourTable" & vbCrLf & _
"WHERE FigureID <> Forms!frmMain!txtFigureID"
Debug.Print strSql
Me.subformControlName.Form.RecordSource = strSql
End Sub
Watch out for subformControlName. It's a control, not a form. The subform control may have the same name as the form it contains. But it could be a different name.

ms access auto fill main form based on focus of subform

I have a data entry/editing form with two combo boxes (Name and Group).
Each Group correlates to multiple names but each name is in only one group.
At the bottom of this form is a subform which is a continuous form
displaying a query of the associated table filtered based on an
unbound combo from which you select the group.
Basically, you choose which group you want to see and it displays a list of all the Names in that Group.
I want to make this form able to add and delete Names from the table (which it does with buttons already), but I also want to be able to select a Name from the subform and have the main form focus on that entry and auto fill the two bound combos.
And then from there I would like to be able to edit and save that entry or just delete the entry.
Similarly, I would like to be able to add a new entry without worrying about writing over a current entry. Access might do this automatically; if this is the case:
Is it even possible to edit an entry without deleting and then replacing it?
I also want to be able to select a
Name from the subform and have the
main form focus on that entry and auto
fill the two bound combos.
To do that, you can do a FindRecord on the main form's recordset, using the ID from the subform. The form will move to the correct record. From the subform's OnCurrent Event:
Forms!MyMainForm.Recordset.FindFirst "MyID = " & desiredRecordID
or
Forms!MyMainForm.Recordset.FindFirst "MyID = '" & desiredRecordID & "'"
Similarly, I would like to be able to
add a new entry without worrying about
writing over a current entry.
To do that, execute the following code:
DoCmd.GoToRecord acDataForm, "MyMainForm", acNewRec

ms access check box

I created a form, displaying company.
created combo-box in the form to list all products of that company
with a check-box next to each item*(IN THE COMBO BOX). how do I create a report on only the items that were checked,
OR
soloution2, I tried showing all products of that company on that form in sub-data-sheet, with a checked box field. how do I create a report on only the items that were checked,
not very proficient in access...
thanks a MIL
In this article, Microsoft shows how to retrieve the values from a listbox as a string. This can then be used to create an SQL statement, for Openargs (depending on your version of Access) or as the WHERE argument for the report:
DoCmd.OpenReport "ReportName",acViewPreview,,"ID IN (" & ListOfIDs & ")"
Note that you will need quotes for a list of strings:
"A","List","Of","Strings"
But not for numbers:
1,2,3,4
This would be similar for the subform, but the best bet would be to build the sql statement and to use that as the Record Source:
strSQL="SELECT ID, SomeField FROM SomeTable WHERE ID IN (" & ListOfIDs & ")"
Me.[NameOfSubformControl].Form.RecordSource=strSQL
You might like to use a command button to do this.
Be sure to use the name of the subform control, not the the form contained.
It would be easier with a simple listbox that allowed only one company to be selected, because you could then set the Link Child (to the company ID) and Link Master (name of the listbox) fields for the subform control.
In both cases, it is best that the listbox is setup with two columns, Company ID and Company Name, with Company ID as the hidden, bound column.