How to add functionality for ComboBox <Select All> in Microsoft Access forms - sql

So I posted a question the other day about an access form that needed to have a "Select All" option added to the 2 ComoBoxes. I was able to add in the option to the 2 of them using a union. However, the options do nothing as of yet. I found the Query that takes the form parameters from the ComboBox and this is where I need to add in the option to select all, except after staring at it for hours I've got no clue again.
The Database wasn't written by me, it's approximately 10 years old and was was given to me to add some new features. I have done that, and the owner complained that the "Select All" buttons have never worked. After research, the buttons have VB script that clears the ComboBox input to nullified value. I am planning on scrapping those now since I have added the option to the ComboBox itself.
The SQL query that reads combo input looks like this:
PARAMETERS [Forms]![ReportCentre]![cboTreatmentType] Short, [Forms]![ReportCentre]! [cboTreatmentDate] Short;
SELECT addresses.*,
[firstname] & "" & [lastname]
AS Name,
[street] & "," & [suburb] & "" & [stateorprovince] & "" & [postalcode]
AS
Address
FROM addresses
WHERE ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND ( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) IS NULL )
AND
( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND ( ( addresses.treatmentdate ) IS NULL )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) IS NULL )
AND ( ( addresses.treatmentdate ) IS NULL )
AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
[txtbirthmonth].[Value]
& "*" ) )
OR ( ( ( addresses.treatmentid ) IS NULL )
AND
( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) IS NULL ) )
OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND ( ( addresses.treatmentdate ) IS NULL )
AND ( ( addresses.birthmonth ) IS NULL ) )
OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
[cbotreatmenttype].[Value] )
AND
( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
[cbotreatmentdate].[Value] )
AND ( ( addresses.birthmonth ) IS NULL ) );
I know it's messy and hard to understand, which is why im asking for help. How do I get that to validate a "Select All" option for both ComboBoxes?

One very easy way is to set the bound column of the combo to *:
SELECT "*" As ID, "Select All" As AText
FROM Table1
UNION SELECT Table1.ID, Table1.AText
FROM Table1;
Using your combo:
Select "*" As TreatmentID, "<<All Records>>" As Treatment
FROM Treatment
UNION
Select Treatment.TreatmentID, Treatment.Treatment
From Treatment;
You can then use LIKE:
SELECT Table1.ID
FROM Table1
WHERE Table1.ID Like [forms]![MainForm]![Combo]
Using your SQL:
... WHERE (((Addresses.TreatmentID)
Like [Forms]![ReportCentre]![cboTreatmentType]) AND ...
If you only have a single column, you can use:
SELECT Table1.Atext
FROM Table1
WHERE AText Like
IIf(Forms![MainForm]!Combo="Select All","*",Forms![MainForm]!Combo)

Related

How to create SQL Server tree in one row?

create table Test
(
Id int identity,
Name varchar(50) not null,
SName varchar(50) null,
ParentId int null
)
insert into Test
values ('aaa', 'bbb', null), ('adf', '22b', null), ('aad', 'bbsd',2),('asdsaa', 'bf', 3),('sdfs','sdf',3),('iopio','uiopio',3)
select * from Test
I have a table with parentid, and I want to get something like that
Name SName "aaa" "bbb" , "adf" {"aad":{"asdsaa":"bf"}}
from the selected values
I have been trying to get it with a recursive query, but...
WITH tree_view AS
(
SELECT
Id, ParentId, Name, SName
FROM
Test
UNION ALL
SELECT
parent.Id, parent.ParentId, parent.Name, parent.SName
FROM
Test parent
JOIN
tree_view tv ON parent.ParentId = tv.Id
)
SELECT DISTINCT *
FROM tree_view
I want to get this
if the parent id is not null, in one sell I want to get Name:SName in Sname field
I think you need to use PIVOT:
Below is similar solution which you can try
Sample Table creation:
CREATE TABLE [dbo].[TestTable](
[Id] [int] NULL,
[Name] [nvarchar](50) NULL,
[Description] [nvarchar](50) NULL)
Inserting sample values:
INSERT INTO TestTable VALUES (1,'Test1a','TestDesc1a')
INSERT INTO TestTable VALUES (2,'Test1b','TestDesc1b')
INSERT INTO TestTable VALUES (3,'Test2a','TestDesc2a')
INSERT INTO TestTable VALUES (4,'Test2b','TestDesc2b')
Query to get the desired output using Pivot:
SELECT 'Name' AS [Column], [1], [2],[3],[4]
FROM
(SELECT Name, id from TestTable) AS ST
PIVOT
(Max(Name) FOR ID IN ([1], [2],[3],[4])) AS PT
UNION
SELECT 'Description' AS [Column], [1], [2],[3],[4]
FROM
(SELECT id,[Description] from TestTable) AS ST
PIVOT
(Max([Description]) FOR ID IN ([1], [2],[3],[4])) AS PT
ORDER BY [Column] DESC
OutPut:
Column 1 2 3 4
Name Test1a Test1b Test2a Test2b
Description TestDesc1a TestDesc1b TestDesc2a TestDesc2b
Hope this helps to solve your question.
If you ar sure any parent leads to only 1 leaf, it's possible to generate the JSON you want by playing only with string manipulation:
with rel_hier(id, parentid, name, sname, js) as (
select m.id, m.parentid, m.name, m.sname,
CAST(CASE WHEN m.parentid IS NULL THEN
CONCAT('"' , m.sname , '"') ELSE
CONCAT('{"' , m.name , '" : "' , m.sname , '"}') END AS VARCHAR(2000))
js
from Test m
where not exists(select 1 from Test t1 where t1.parentid = m.id)
union all
select m.id, m.parentid, m.name, m.sname,
CAST(CASE WHEN m.parentid IS NULL THEN r.js ELSE CONCAT('{"' , m.name ,
'" : ' , r.js , '}') END AS VARCHAR(2000)) js
from rel_hier r
join Test m on m.id = r.parentid
)
SELECT name, js as sname FROM rel_hier r
WHERE parentid IS NULL
;
name sname
aaa "bbb"
adf {"aad" : {"asdsaa" : "bf"}}
But you should also better explain the substitution rules for a number of levels > 3.

Return random value for each row from different table

I'm trying to get random name for each row, but it just shows same random value for every row. What am I missing?
SELECT TOP (1000) v.id,v.[Name], RandomName
FROM [V3_Priva].[dbo].[Vehicle] v
cross join
(Select top 1 ISNULL([Description_cs-CZ], [Description]) RandomName
from crm.Enumeration e
join crm.EnumerationType et on e.EnumerationType_FK = et.Id
where EnumerationType_FK = 12
order by NEWID()) RandomName
Result table
Try using something like the following to drive your lookup.
DECLARE #Rows AS TABLE ( I INT NOT NULL )
DECLARE #Vals AS TABLE ( ID INT IDENTITY, Val NVARCHAR(255) NOT NULL )
INSERT INTO #Rows
VALUES ( 0 )
, ( 1 )
, ( 2 )
, ( 3 )
, ( 4 )
, ( 5 )
, ( 6 )
, ( 7 )
, ( 8 )
, ( 9 )
INSERT INTO #Vals
VALUES ( 'Apple' )
, ( 'Banana' )
, ( 'Peach' )
, ( 'Plum' )
, ( 'Pear' )
WITH cte AS ( SELECT *, ABS(CHECKSUM(NEWID())) % 5 ID FROM #Rows )
SELECT cte.I
, cte.ID
, V.ID
, V.Val
FROM cte
JOIN #Vals V ON V.ID = cte.ID + 1
ORDER BY I
This way new ID is generated for each row, rather than once for the lookup.

How write CHECK condition for every element in array in postgreSQL

I don't know how work with loops and arrays in PostgreSQL but i want make table with array which only accepts the given values.
('Icelandic','English','Polish','Danish','Norwegian','Swedish','French','German','Spanish')
How write this condition in shorter and prettier form?
CREATE TABLE Test(
UUID UUID NOT NULL PRIMARY KEY ,
Name varchar(40) NOT NULL UNIQUE , --
Languages text[] NOT NULL CHECK (
Languages[1] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
)
AND
Languages[2] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
)
AND
Languages[3] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
)
AND
Languages[4] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
)
AND
Languages[5] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
) AND
Languages[5] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
)
AND
Languages[6] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
)
AND
Languages[7] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
) AND
Languages[8] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
)
AND
Languages[9] IN (
'Icelandic','English','Polish','Danish','Norwegian',
'Swedish','French','German','Spanish'
) )
);
A better solution would be to use the #> or <# operators as described here https://www.postgresql.org/docs/12/functions-array.html
...CHECK (languages <# ARRAY['Icelandic','English','Polish','Danish','Norwegian','Swedish','French','German','Spanish'])
However I suggest you use the common two-or-three letter language codes, instead of the full names. It's just more useful during migrations.

converting rows into columns in sql server

I wonder if anyone can help: I want to convert rows into columns. This is the original table:
I have tried using pivot but this case it is too complex for me.
declare #Table AS TABLE
(
TYPE VARCHAR(100) ,
SERIE VARCHAR(100) ,
CUR1 INT,
CUR2 INT
)
INSERT #Table
( TYPE, SERIE, CUR1, CUR2)
VALUES
( 'CORP', 'S1' ,2122,322 ),
( 'CORP', 'S2' ,321,546 ),
( 'SER', 'S1',543,788 ),
( 'SER', 'S2' ,655, 988 )
I expect the output to be like the attached table:
Please try this, a variant of this will help:-
;with cte as (
select SERIE, [CORP] as [CORP_CUR1], [SER] as [SER_CUR1] from (
select type , serie, cur1 from #Table)
as d
pivot
( max(cur1) for [type] in ( [CORP], [SER]) ) as p
),
ct as (
select SERIE, [CORP] as [CORP_CUR2], [SER] as [SER_CUR2] from (
select type , serie, cur2 from #Table)
as d
pivot
( max(cur2) for [type] in ( [CORP], [SER]) ) as p
)
select cte.SERIE, cte.[CORP_CUR1], cte.[SER_CUR1], ct.[CORP_CUR2], ct.[SER_CUR2] from cte inner join ct on cte.SERIE=ct.SERIE

How to use IN inside of CASE

SELECT T_MW.*
INTO #temp
FROM T_MWP T_MW
WHERE T_MW.CompanyID = 2
AND (
CASE
WHEN #Currency IS NOT NULL
THEN (
T_MW.Currency IN (
SELECT *
FROM #Currency
)
)
END
)
AND (
CASE
WHEN #Asset IS NOT NULL
THEN (
T_MW.Asset IN (
SELECT *
FROM #Asset
)
)
END
)
Here #Asset is a table containing assets and #Currency is a temporay table containing Currency. #Asset and #Currency are created using following query
SELECT *
INTO #Asset
FROM dbo.Split(#Asset, ',');
Use OR Instead of Case
SELECT
T_MW.*
INTO #temp
FROM T_MWP T_MW
WHERE T_MW.CompanyID = 2
AND
(
(
#Currency IS NOT NULL
AND
T_MW.Currency IN
(
SELECT * FROM #Currency
)
)
OR
#Currency IS NULL
)
AND
(
(
#Asset IS NOT NULL
AND
T_MW.Asset IN
(
SELECT * FROM #Asset
)
)
OR
#Asset IS NULL
)
SELECT
T_MW.*
INTO #temp
FROM T_MWP T_MW
WHERE T_MW.CompanyID = 2
AND
(
(
#Currency IS NOT NULL
OR
T_MW.Currency IN
(
SELECT * FROM #Currency
)
)
)
AND
(
(
#Asset IS NOT NULL
OR
T_MW.Asset IN
(
SELECT * FROM #Asset
)
)
)