How to make access query criteria in VBA - vba

I am trying to stop a name from appearing in a listbox that is linked to a query. The listbox shows records and fields from a table.
I am unable to just type the following in the criteria field:
Not "Administration"
or
<> "Administration
I am not sure why but I think it might work if i add it in code instead. How do I do this.
Thanks

Probably the field contains invisible characters or leading/trailing spaces. You can check this easily if you change the criteria to = "Administration". If query returns no rows, the data is not exactly equal. You can try criteria Not Like "*Administration*", but this way may not work for all data. Correct condition is <> "Administration"

You have to edit the listbox RowSource property, as follows:
1- show the listbox properties window
2- go to RowSource property under 'Data' Tab
3- click the button next to RowSource property (which will open a Query Editor window)
4- Add the condition:
Trim([ColumnName])<>"Administration"
where you replace ColumnName with your actual column name
I hope this helps out.
(Sorry I'm still using an older MS-Access release nevertheless you should get the idea).

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,

Microsoft Access list box is empty even though RowSource is specified and returns results

I created a ListBox in a form. Its RowSource is being filled by VBA, but the ListBox is empty.
When I open the Query in the RowSource, Access returns rows, but the ListBox itself is empty. Is this a bug, or am I missing something?
list box is empty
Since you state:
Its RowSource is being filled by VBA, but the ListBox is empty.
You may need to Requery the list box after modifying the content of its Row Source -
ListBoxName.Requery
Change ListBoxName to suit the name of your control.
You may also want to check that the Column Width is non-zero.
Seems like there was a problem with the query. The Column "Art" was defined as a boolean. Even though it should work when querying it with 1, when specified as a RowSource, it needed to be queried with true instead. Querying it with 0 works though. Anyways, thanks for all your help!

Combo Box on form to filter table I imported into Access from Excel

I have been searching for a couple of hours but I can only see examples of combo boxes on forms filtering tables that are based from queries.
My table (which I have put on a subForm) is from a static datasheet that I imported into Access from Excel so therefore does not come from a query.
I want to use a combobox (or a textbox) so that the user can enter in the reference number and it will filter the table (rather than using the filters already provided (as they just get confused))
Is this even possible? I should add, the reason why I want the table showing (and not a query from it) is that I want the user to be able to edit the table once they have found the reference number they are looking for.
Any help would be much appreciated.
You can use the form's filter method to programatically filter the rows. careful though, as you probably are going to need a mechanism to remove the filter, like a separate button as it won't be obvious. OR, you may still want to keep the form's "Navigation Buttons" property to true. this would then have allow the user to see that the rows are filtered, and also allow them to remove the filter.
Use the form's filter methods to filter. so say they want to filter on a field called "value1" with value of 1, it would be like this. if your filter condition is text, then use single qoutes around them.
Me.Filter = "[value1] = 1"
Me.FilterOn = True
I think your question is more about a ComboBox updating a view, right. I don't think this really has anything to do with Excel, right. If this is correct, please see the link below.
http://www.fontstuff.com/access/acctut17.htm
Also, see this.
http://www.fontstuff.com/access/acctut18.htm

Combobox filter on a query not working while Null (in the form only)

I am currently working on a project on MS-Access 2010 and I am struggling with a issue for hours. I would like to apply a filter on a SELECT/PIVOT statement. The result of that query is displayed in a ListBox.
The issue is that no results are displayed when the Combobox is set to Null. But when I select specific values in that Combobox it works perfectly.
The Cbbox filter is declared as a Parameter
My query WHERE clause looks like this :
WHERE (Jobs.fk_group=[Formulaires]![frm_MAIN]![lst_filterGroup] AND (fk_otherCritera='XXX'))
OR ((([Formulaires]![frm_MAIN]![lst_filterGroup]) Is Null) AND (fk_otherCritera='XXX'))
The query WORKS while I enter manually the value of the parameter (=when I enter an empty string, it displays all the records = what I want)
Idk if it is important, but also the listview that I use swap dynamically its recordsource (=it runs 2 differents queries), depending from another Cbbox
I checked the parameters values into my VBA code just before MyListview.Requery calls and IsNull(myCbboxReference) returns True and my other criteria is also OK.
I have no clue of what I did wrong, I need help :-(
Best regards, LR
I would recommend to use special value in combobox for displaying all records in main table and don't rely on comparing with Null values. Your query, probably, doesn't work because empty combobox returns "" , not Null.
Also be careful with queries based on references to controls/parameters. Access has a bug: if you apply filter on data in the form, based on query with such kind references, it stops read new values from controls/parameter during Requery. It appears at least for subforms in Datasheet mode. Workaround for this bug - using function instead of reference.

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.)