How to program a textbox so that multiple values are separated by quotes? - sql

Good morning everyone!
I am experimenting with an Access 2007 form which contains a textbox that inserts values into a query. I want to be able to enter multiple values into the textbox. The values are separated by a comma but are also wrapped in quotes. The values in the textbox are inserted in a HAVING Clause.
Here is what the code looks like:
Example:
HAVING Max([COLOR].[COLOR_CODE] In ("11111","22222"))
Now I want to use a textbox on a form to insert the value but I need to handle the quotes. So I converted the values to Numbers both at the SELECT Statement and at the HAVING Clause.
Example:
HAVING Max(CLng([COLOR].[COLOR_CODE] In ([Forms].[MyForm]![txtColorCode])))
The problem is when I enter 11111 into the textbox on the form the query works but when I enter 11111,22222 the query does not work.
So I have this idea; what if I could enter, e.g: 11111,22222,33333,44444 and use code to format the values to "11111","22222","33333","44444"
This way I can abandon the Conversion/CLng and just use the IN Operator with the string.
How can I formatting or transformation so that it is inserted into the query. Maybe on Submit?
I would really appreciate and input or ideas.
Thanks everyone.
Guy

Where are these values coming from? Is a listbox out of the question? Do not forget that you can add and delete values from a list box easily enough and even more easily if you can reference a table. Once you have the values selected, you can iterate over them to create the clause you need.
You will always have to build the query in VBA, you will not be able to refer to a textbox like this in the query design window.

Related

ADD Datagridview values to database

I have a datagridview that i fill and after i click on a button and all the rows goes to my database, that is working, but when i want to insert more rows and insert into the database it insert all the rows again and i just want the last ones added.
If anyone could help me i appreciate.
Can't comment so I will write here...
So if I do understand you correctly you are filling gridview sending it to database. After that you fill once more the gridview and send it but this time it also send the one already are on the gridview?
Edit/Update (after the extra info [see below comments])
You can use the SELECT statment to put only values that does not exist.
Before you start using this method/statment you need to know very well which values can be put multiple times so you will write your WHERE statment that way...
I did make a example for you check it out:
Example on DBFIDDLE
There are also another option of you doing this and it is using the IF statment. I will also make a SQLFIDDLE of this but first I need to go to the market and will edit this post with that too once I come home.

view unique values in Combobox using VBA in MS Access

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.

Calculating the Sum of values in ms-access

I have created a query to calculate the sum of all of the profit values in a table, I tried to output this to a textbox on the main form of my database and I just the error #NAME?.
Has anyone tried this before and are there any major things I am missing?
We would need to see some code/design details to understand why your text box gets that #Name error.
Without those details, I'll just suggest you consider a DSum() expression, instead of a query, to load the text box. And DSum() is kind of like a SELECT query, but returns only a single value instead of a result set.
DSum("YourNumericField", "YourTable")
Examine the DSum online help topic for more details. You might find the optional Criteria parameter useful (like a WHERE clause in a SELECT statement) if you ever want to sum only a subset of rows from your table.
DSum("YourNumericField", "YourTable", "account_status = 'ACTIVE'")
If you have create query to calculate the sum called "querySum". On the form property sheet, make sure selection type you change to FORM, go to Data and select "querySum" as record source.
And then, click text box, go to the property sheet and choice Data > Control Source. So you can choice column from query to the text box.
Otherwise, if you want to use VBA. You can do like this
DSum("NumericField", "YourTable")
Or with condition
DSum("NumericField", "YourTable", "type = 'Payment'")

Filter wrong database records in a DataGridView control (Visual Studio 2005)(Visual Basic) (Normalization)

I have this database and I need to load it with a visual basic form in Visual Studio 2005.
The database has 6 tables and they contain wrong data that I must filter when loading the database in the form.
A quick example:
First table is Category, which has 3 fields (id, name, description). In one record I have 2a for id (a numeric only field) and in other record I have m1lk for name (a string only field).
When I click a button I load the database into a DataGridView control. How do I stop visual basic from loading into the DataGridView control those records that have invalid data (like in my example).
I have try with SQL queries using WHERE and LIKE, like in
SELECT IdCategoría, NombreCategoría, Descripción
FROM Categorías
WHERE (NombreCategoría LIKE '[!l]%') AND (IdCategoría LIKE '[!abcdefghijklmnñopqrstuvwxyz]')
but it's really difficult to filter things like Angel* and P3ter and Hood8, all at the same time...
Please show me a example code in your answer so I can try it.
Thanks in advance.
If you define a column in an MS Access table using a numeric data type, it simply won't allow values like "2a". But text columns will allow values like "m1lk", because "m1lk" is a valid string.
Quick workaround
To select only those values that can be interpreted as numbers, use the isnumeric() function. You need a SQL statement along these lines.
SELECT your-column-list
FROM some-table
WHERE isnumeric(numeric-column-having-bad-data)
Be aware that expressions like isnumeric("2e5") and isnumeric("2d5") will return True. VBA interprets both "2d5" and "2e5" as 2^5 ("two to the fifth power").
To select data that contains no numerals at all, use a regular expression like this. (I'm not certain you're trying to do this.)
SELECT your-column-list
FROM some-table
WHERE some-column-name Not Like "*[0-9]*";

ms-access the ability to enter multiple values into a textbox on a form

i have a textbox on a form and i would like the user to be able to choose from different values but also be able to enter their own values, how do i do this?
i would like to clarify that the user should be able to enter multiple values into the same textbox
I suggest that you don't. A subform to a table of values related to the main table would seem to be the way to go. You can make data entry for the field a combo that is not limited to list. Often a link back to the sub-table will suit in such a case.