Good day
Created Tables = Suppliers, Products
I created a form.I created a combo-box that displays all products of a particular company.
I need that combo-box that displays all products to have a check-box or any other control next to each product SO that I will be able to select more than one product.
Then how do i create a report of only the products checked.
....newbie
ms access 2007
Unfortunately that slick new multi-value drop down combo box with a check box included in the columns is not available from a programmer’s development point of view. (The ability to use this control for our own needs is rather becoming a frequent request these days - it is very slick contorl)
I’m not sure if others posters are confused or not aware of this new feature in access 2007. So it’s very natural that you’re asking for this ability to use this cool combo box feature to drive and feed parameters to a report.
Unfortunately without a lot of coding tricks and use a bound table for the selected values you can’t use that control. ( So it’s possible, but not easy).
The recommended approach in this case is to simply drop a list box on to your prompt form.
Make the first column of the listbo the primary key or so called product ID that’s used internally in your application. Make the 2nd second column the description that the user will see and select in the list box. (the wizard should make the first collumn lenght zero so that the product IDs are not seen by the user - if not you can set this manually after the wizard creates the list box for you).
You’ll then have to open up the form in design mode and change in the other tab of a list box to set and allow multi select=yes. This setting will allow the user to select more than one option in the list box.
You’ll then need to place a button on the form with the following code behind that button that takes the list of parameters or values from the list box and passes them to the report
Dim MySql As String
Dim MySelection As Variant
MySql = ""
If Me.lstProducts.ItemsSelected.Count > 0 Then
For Each MySelection In Me.lstProducts.ItemsSelected
If MySql = "" Then
MySql = "("
Else
MySql = MySql & " or "
End If
MySql = MySql & " ProductID = " & Me.lstProducts.Column(0, MySelection)
Next MySelection
MySql = MySql & ") "
End If
End If
Docmd.OpenReprot “nameOfRepor”,acViewPreview,,mySql
The above assumes your field for product id is ProductID
Related
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.
I have been using MS Access to aid in generating pdf reports based on a table. I have created a form that contains a text box for entering a client's name (this value is in the main table) and a button that when clicked runs the code:
Private Sub cmdPrintRecord_Click()
Dim strReportName As String
Dim strCriteria As String
strReportName = "Current SP Report"
strCriteria = "[Owner]='" & Me![Owner] & "'"
DoCmd.OpenReport strReportName, acViewPreview, , strCriteria
End Sub
The idea here is to generate an individual PDF report based on the clients name.
The above procedure has been able to do that successfully however, I have encountered that as I run it, the data in my table is affected, specifically the client name field.
For example: I'll run a report for client "Anthony" and it shows 10 products which is correct, but then if I go back and run that same report again it will show 11 products. It is as if the procedure here is altering the data table.
How can I troubleshoot this issue and or are there any alternatives recommended?
Thanks.
Attached is the MS link where I obtained the source code:
https://support.microsoft.com/en-us/kb/209560
If the Control Source of a form control (textbox, combo box) is linked to a table then it will modify that table. In your case, you want to receive user input based on selections from a table and not to modify the table itself. You want to use the "Row Source" property to limit the selection to table items and clear the Control Source. This pulls potential options from the table without changing existing table entries.
Like this:
Notice that the source of options for the combobox is a query that defines what items should appear in the dropdown but no Control Source is specified.
I can find plenty of examples of how to return specific records in a subform through altering the underlying source query in code and re-querying, but I'm struggling to use the same principle to alter the fields that are returned.
I have the following situation:
Combobox to select 1 of 5 fields. Subform is supposed to show the selected field plus a couple of static fields, lets call them fields 6 and 7
So in the case the user selects field 1 from the dropdown, subform should show fields 1, 6 and 7. In the case they pick field 4, subform should show fields 4, 6 and 7 etc.
This is what (amongst other things) I've tried:
Set the subform up through the wizard with a query (select field1, field6, field7) as source, amend said query after combobox selection is made:
Set qd = CurrentDb.QueryDefs("myqueryname")
qd.SQL = "Select " & mycomboboxselection & ",field6,field7 from mytablename"
Form_mymainformname.mysubformname.Requery
The query itself updates fine if I run that standalone after the change, but the subform within the main form doesn't change and when I click on the subform itself from the navigation window it seems to be stuck looking for field 1 as it asks me to input a parameter value
Can anyone help with how to achieve this please?
Set the RecordSource of the subform to the SQL:
Dim SQL As String
SQL = "Select " & mycomboboxselection & ",field6,field7 from mytablename"
Me!YourSubformControl.Form.RecordSource = SQL
It will force a requery of the subform.
Ok I found a solution after a few more hours searching and trial and error.
There are two seperate problems here. Firstly creating the subform uses the newly created form as the source object rather than directly using the query, and it seems that no matter how you try to manipulate the record source of said form, it doesn't like changing the fields it was built with.
So, create the subform, change the source object from the form to your query (and delete the now pointless newly created form), then you can use the code I started with:
Set qd = CurrentDb.QueryDefs("myqueryname")
qd.SQL = "Select " & mycomboboxselection & ",field6,field7 from mytablename"
Which works! sort of....
You have to close and reopen the form every time to see the actual updates though, which obviously isn't what we're going for
The other line does nothing:
Form_mymainformname.mysubformname.Requery
It works fine for a change of records, but apparently not for a change of fields. I suspect this is a bit of a ms quirk
The below, however, works, even though I feel like it should do exactly the same as the code line above:
Form_MyForm.MySubForm.SourceObject = Form_MyForm.MySubForm.SourceObject
I am creating a small Access DB for our Data Entry guys. Our main DB is mysql but due to ease of use we are creating an Access DB to make it easier for them to enter the Data. I have done most of it but am stuck with this problem (which I know how to solve in mysql+php) Please pardon my ignorance, but I have just started using MS Access.
I have two tables - ClientPhones and sales. The ClientPhones table has phone, clientid fields. sales table has salesid, clientid, date, etc fields.
I also have a Form which has all relevant fields for the sales table. I want to add a new field, phone_no in that form. When a user inputs the number and on focus lose event, I was access to run a query on the clients table to see if the phone number exists in any of the 3 phone number fields. If it finds a client with that phone number, the client ID should be populated, else a new form to input the client details should be shown.
Is this possible with MS access or am I doing this incorrectly?
Use the text box's After Update event to retrieve the clientid which matches the phone number the user entered.
If a clientid is found, store it in the text box which is bound to clientid.
If no match is found, ask whether the user wants to add a new client, and open that form if they respond yes.
This code outline assumes txtSearchPhone is the name of the text box where the user enters the target phone number, and txtClientId is the name of the text box where you want to store clientid.
Private Sub txtSearchPhone_AfterUpdate()
Dim varClientId As Variant
Dim strCriteria As String
strCriteria = "[phone]='" & Me.txtSearchPhone.Value & "'"
Debug.Print strCriteria '<-- inspect this in Immediate window; Ctrl+g will take you there
varClientId = DLookup("clientid", "ClientPhones", strCriteria)
If IsNull(varClientId) Then
If MsgBox("Add new user?", vbYesNo) = vbYes Then
'DoCmd.OpenForm ... (see Access help topic)
End If
Else
Me.txtClientId.Value = varClientId
End If
End Sub
Make sure the text in txtSearchPhone does not include a single quote character (') because the DLookup will break if it does. You can use the text box's Validation Rule and/or Before Update event to make sure a single quote is not present.
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.