view unique values in Combobox using VBA in MS Access - vba

please read my question to end because I test all previous solutions in Stackoverflow and have no answer.
I am trying to display Combobox in the form of an MS Access database using VBA, this Combobox takes its values from one table the problem that I can't display just unique values, Combobox views all values even when I use DISTINCT still view all.
also, I am trying to use GROUP BY but not work.
I know it is a simple problem but how can I solve it?
SELECT DISTINCT Exports_imports_Table.ID, Exports_imports_Table.content FROM Exports_imports_Table;

Assuming the ID field is a unique value (and the content field may have duplicates) you cannot include it in a DISTINCT. Try it like this
SELECT DISTINCT Exports_imports_Table.content FROM Exports_imports_Table;
Does that give you what you expect?

Don't take the ID field in your query builder. Instead of that, select the same field twice, leaving one of them as is, and in another you can give your conditions or formats (if any).
Make sure to change unique values in general properties to "yes".
Save it, and there you are.

Related

MS Access: Dependent drop down combobox in Split form without affecting the rest? [duplicate]

This question already has an answer here:
How to query combo box of only current record/row in Access data entry form?
(1 answer)
Closed 1 year ago.
I am trying to make a dependent drop down work with my database here but it is giving me a hard time for different reasons that I will explain.
This is what I have:
A form called "tblOTS" (split form based on an actual "tblOTS" table):
As well as a table called "tblAlphaCode":
When looking at the property sheet for the "Alpha Code" combobox (actually called "strOTSAlphaCode") on my tblOTS form, this is what I have:
The current SQL statement under "Row Source" for this strOTSAlphaCode is:
SELECT tblAlphaCode.strAlphaCode, tblAlphaCode.strCategory,
tblAlphaCode.ID, tblAlphaCode.numSortingOrder
FROM tblAlphaCode
ORDER BY tblAlphaCode.strCategory, tblAlphaCode.numSortingOrder;
Basically, when user select an Alpha code in the drop down from "tblOTS" form, it adds the ID of the tblAlphaCodes into the actual tblOTS record.
My goal is to have the user select first a "Category" (here FASTENERS/HARDWARE ...) when adding/modifying an OTS record, and then have the "Alpha Code" drop down dependent on what category was just selected.
I first tried to changed the SQL statement using a WHERE condition on the category field, based on the form category field itself:
SELECT tblAlphaCode.strAlphaCode, tblAlphaCode.strCategory, tblAlphaCode.ID, tblAlphaCode.numSortingOrder
FROM tblAlphaCode
WHERE (((tblAlphaCode.strCategory)=[Forms]![tblOTS]![strOTSCategory]))
ORDER BY tblAlphaCode.strCategory, tblAlphaCode.numSortingOrder;
And created a strOTSAlphaCode.requery in my Form_Current event to update it everytime.
However, it affects the whole form itself as my control source is directly affected by my Row Source in this instance, and the form looks like this:
You can see that the drop down is working beautifully, however, all the fields that do not have the same category as the current record that I work on (FASTENER <> HARDWARE here), are missing their Alpha code (the second record is missing "O-RING" compared to the first picture from this post); this behavior is problematic!
I tried to find workarounds by changing strOTSAlphaCode into a simple text box only containing the Alpha Code ID, added an unbound text box on top with dlookup function to find the actual alpha code related to this ID, and an unbound combobox in between with the exact same SQL statement inside the Row Source Property, that would update the strOTSAlphaCode with some VBA ... not ideal right?? Ahaha. This is why I believe there is a simpler way that I am not aware of, but also I would like to use a "search as you type" code for this combobox later on and this solution was making it very difficult.
How can I make this work? I believe the answer is a simple/different SQL statement to put in my Row Source property, that is probably something related to some type of JOIN statement? Or something else? I expect to write some VBA code to make all of this work flawlessly, but I want to make sure that I have the right SQL statement first.
Thank you June7;
Yes, the link you gave me ( this link ) states 2 solutions, and the second one is the one that I talked about when describing my problem:
for forms in Continuous or Datasheet view, include lookup table in form RecordSource, bind a textbox to descriptive field from lookup
table, position textbox on top of combobox, set textbox as Locked Yes
and TabStop No
So I guess, the answer is that I MUST have a textbox with dlookup setup for my case; I put it on "locked" so that if the user wants to change it by typing in it, he will first have to select the arrow. This will work with what I wanted to do
Cheers,

MS Access: Conditional formatting - highlight duplicates

Is there an expression that I can use in MS Access ,to highlight Duplicate entries in Reports?
I tried something like Expression is : Count(*)>1 but it doesn't work.
Br,
I suspect that the original query will need to be bulked up with a sub-query that has an ID column and count of ID. The outermost query will then need to also return the ID count.
Within the report you'd then need add another field that would show the linked ID count if it was > 1.
Access reporting (and forms) allows conditional formatting to be used in a similar way to excel.
See Ribbon: Report Design Tools>Formt>ControlFormatting...
It will let you change the format of a control depending on the value it, or another control, contains.
It's a very nice feature and will also let you add bar charts to you list forms to graphically represent the values sorted in a control.
However, the data set will need to have a column that indicates whether the current row has duplicate records. The snippet from you current query that you provided (that I repeat below) will not do this:
...OR (((Object.Key) In (SELECT [Key] FROM [Object] As Tmp GROUP BY [Key] HAVING Count(*)>1 )));
Without seeing the whole query I can't really help much, but you will need to remove the use of IN and make the SELECT statement a subquery of the main SQL Statement. The main query resultset will need to be LEFT JOINED to the sub query using the Key field. Because of the LEFT JOIN you can use "isnull(Key)" in the SELECT clause and isnull(Key) will be true for non-duplicate rows.
You can then refer to thiscolumn in your conditional formatting
I hope this makes some sense.
You
For a quick and dirty way to highlight duplicate data:
Select the object you want to highlight if it's duplicated, and make
the background white (or whatever the colour of your background is).
Create a copy of the object that you want highlighted if it's
a duplicate.
Format the copy so it has a highlight, and/or add extra text
(eg: DUPLICATE)
Put the copy behind the original (so it can't be seen).
On the original object, select "Hide duplicates" in properties.
Ensure "Hide duplicates" is NOT selected on the copy.
So when the duplicate appears, Access will hide it, but then the object you've created that was originally hidden beneath now becomes visible - effectively highlighting the field.
(Unfortunately it will only highlight the field itself, not the entire section.)

Create SQL String in Microsoft Access Dynamically

I apologize if this is an easy one, but I can't find this on the web anywhere!
I have a list of tables and queries, and a full list of fields from each table and query.
I want to choose my fields in an Access form, and then, on another form, choose a query/table in one column (along with a field) and join it to a table/query in another column (along with the field.) My form for the joins would look like this:
Object1 Field1 Object2 Field2
and so on. I would want to be able to choose my fields through combo boxes, THEN make the SQL string dynamically. I can't use a where clause -- it has to be a join.
The problem is, the structure will change every time. Access gets funny about putting in parentheses. Also, if an object is chosen more than once, Access will want to join it (assuming I do it right) in a different manner than T-SQL.
Is there a way to write a query in T-SQL, and quickly convert to Access? Does anyone know the FROM clause algorithm Access uses to construct the FROM clause? I am stuck here, and I cannot find a solution to save my soul! Thank you in advance, David

Access query using * and sort criteria on columns - how to make field show just column name?

What I am doing
I have a query in Access which as SQL view of:
SELECT Projects.*, Projects.MySortField
FROM Projects
ORDER BY Projects.MySortField DESC;
This query works fine and sorts my data correctly. It is also very, very clean to look at and to understand what is happening.
However, when I load it into my form, I have run into an unexpected problem. Prior to adding Projects.MySortField and the associated sorting, I was able to refer to MySortField as follows:
Me.Recordset("MySortField")
However, now that I've added it to the sort criteria, the query returns slightly difference fields as the record associated field is:
Me.Recordset("Projects.MySortField")
I refer to this somewhat often in VBA which is where problems happen.
What I am trying to do and why
I would like my stakeholders to be able to more easily add or modify sort criteria. Unfortunately some of these are also referred to in the code more explicitly, which means if at any time I want to add sort criteria to my list, it will also adopt the Projects.mFieldName syntax in the RecordSet, which means any code referring to that data will break.
This would be nice to avoid, obviously, and if it was possible to modify the query somehow to facilitate my users adding fields and not having to change any code that would be wonderful.
Specific Question
How can I include Projects.* and still have specific fields from that table for sorting but keep all RecordSet fields reflected as just the field name?
The problem you are running into is that by having Projects.* as well as a named field, you are ending up with two output fields of the same name. I'm not sure why you feel you must use "*" instead of just explicitly naming the fields (which is generally considered the best practice) which would allow you to sort without the problem.
If you want to stick with "*" then you need to uncheck the show box in the query grid for your named fields, which will allow you to have the field explicitly enumerated for sorting or use as a criteria.
The SQL view when you do this should look like:
SELECT Projects.*
FROM Projects
ORDER BY Projects.MySortField DESC;
You can use the unchecking of the show box trick for both sorting or for doing criteria when you are using "*".
You can refer to the sort field without the qualifying table name in the SQL. Here is what I am thinking.
SELECT Projects.*
FROM Projects
ORDER BY MySortField DESC;
This
SELECT Projects.*
FROM Projects
ORDER BY Projects.MySortField DESC;
can be done.
But if you insist on adding , Projects.MySortField,then
SELECT Projects.*, Projects.MySortField as whatever
FROM Projects
ORDER BY Projects.MySortField DESC;

Access form - how to make text field have a control source from a SQL query?

Background
I have two tables:
Projects
EmployeeID
Employee
EmployeeID
Name
I have a query I am basing a form on containing, among other items:
SELECT e.Name FROM Projects p JOIN Employee e ON e.EmployeeID=p.EmployeeID
When I make a form in Access based on this query, I can very easily display e.Name on my form because it is joined from the query.
My real example is of course considerably more complicated than this simple example. The above works fine for read-only queries and scales well. However I would like to use a Splitform view and this becomes very slow with many joins for even small numbers of recordsets. Considering a large percentage of my joins are simple like the above, I am hoping for a way to remove as many as possible.
On the form, e.Name will be read only and not be update-able.
Similar question
In this question I am able to successfully change a combo-box into a lookup. The accepted answer allows a combo box to control Projects.EmployeeID by displaying Employee.Name field in the combo box.
Possible work-arounds
I know one way I could do this is use a combo-box but disable it. This would look a bit weird since it'd have the drop down selector but not be selectable.
Alternatively, I could make it a completely unbound field and write VBA code to update the form each time the recordset changes by running quick queries, getting the text value I am searching for, and updating a label accordingly.
Neither of these are overly appealing, however.
Question
How can I display a single text field on an Access split-form which is the result of a very simple query lookup, based on an ID from the main table field?
You can use Dlookup to return this reference very simply
Construct a Dlookup formula like:
=DLookUp("Name", "Employee", "EmployeeID =" & "[EmployeeID]"
and use this as the ControlSource for the textbox.
Some notes:
The & is important as it binds the formula to the single record displayed on the form
[EmployeeID] refers to the current record displayed on the form. This assumes that "EmployeeID" is included in the query for the form, whether bound to the Projects table or included in the query