Proper form of a regex in SELECT of an Access ComboBox - sql

I have a ComboBox which takes it's source from a query. One of the query' fields is a number like H-1234=AAA-0382 or HN903=BBB-94522 (Field is ID Symix, Table is DessinSelonProjet_Choix).
In the combobox, I want to display this number, but not the part before the = sign in both cases. I wanted to use a Mid(number, position of = sign). In SQL I researched that the position function is CHARINDEX, but I did not manage to get it working.
Here is the code when showing the whole thing:
SELECT DessinSelonProjet_Choix.[ID Symix], DessinSelonProjet_Choix.[Numéro Document],
DessinSelonProjet_Choix.Description, DessinSelonProjet_Choix.[Groupe Source]
FROM DessinSelonProjet_Choix
ORDER BY DessinSelonProjet_Choix.[ID Symix];
Here is what I tried:
SELECT Mid(DessinSelonProjet_Choix.[ID Symix],CHARINDEX('=',
DessinSelonProjet_Choix.[ID Symix]) AS [Location]) AS Expr1,
DessinSelonProjet_Choix.[Numéro Document],
DessinSelonProjet_Choix.Description, DessinSelonProjet_Choix.[Groupe Source]
FROM DessinSelonProjet_Choix
ORDER BY DessinSelonProjet_Choix.[ID Symix];
Syntax error (missing operator) in the expression Mid(DessinSelonProjet_Choix.[ID Symix],CHARINDEX('=', DessinSelonProjet_Choix.[ID Symix]) AS [Location]) AS Expr1

SELECT
Mid([ID Symix], INSTR('=', [ID Symix])+1, len([ID Symix])-INSTR('=', [ID Symix])) AS Expr1,
[Numéro Document],
Description,
[Groupe Source]
FROM DessinSelonProjet_Choix
ORDER BY [ID Symix];
The statement you had was using an alias within a function. Also, you have to use len to get all characters after = in your string.

Related

Syntax error in query expression using sub query

SELECT
(
SELECT Sum(tbl_allTransactions.transAmount) AS SumOftransAmount
FROM tbl_allTransactions
WHERE (((tbl_allTransactions.[transType])='Expense')) OR (((tbl_allTransactions.[transType])='Budget') AND ((tbl_allTransactions.[transMonth])=[#transMonth]))
GROUP BY tbl_allTransactions.transMonth
,
SELECT Sum(tbl_allTransactions.transAmount) AS SumOftransAmount
FROM tbl_allTransactions
WHERE (((tbl_allTransactions.[transType])='Expense')) OR (((tbl_allTransactions.[transType])='Budget') AND ((tbl_allTransactions.[transMonth])=[#transMonth]))
GROUP BY tbl_allTransactions.transMonth
)
FROM tbl_allTransactions WHERE (((tbl_allTransactions.[userID])=[#userID]))
I am getting following error:-
System.Data.OleDb.OleDbException (0x80040E14) Syntax error in query
expression '(SELECT Sum(tbl_allTransactions.transAmount) AS
SumOftransAmount FROM tbl_allTransactions WHERE
(((tbl_allTransactions.[transType])='Expense')) OR
(((tbl_allTransactions.[transType])='Budget') AND
((tbl_allTransactions.[transMonth])=[#transMonth])) GROUP '. at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResulthr) at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS
dbParams, Object& executeResult) at
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Obje
The original code was missing a closing parenthesis on the first subquery. The code was also simplified by consolidating the transType conditional from 2 statements to 1. Extra brackets were removed to enhance readability. Also column aliases were added to output of each subquery. Since both queries render the same data, the second column was called "SumOftransAmount2"
SELECT
(SELECT Sum(tbl_allTransactions.transAmount)
FROM tbl_allTransactions
WHERE tbl_allTransactions.transType in ('Expense', 'Budget') AND tbl_allTransactions.transMonth = #transMonth
GROUP BY tbl_allTransactions.transMonth) as SumOftransAmount
,
(SELECT Sum(tbl_allTransactions.transAmount)
FROM tbl_allTransactions
WHERE tbl_allTransactions.transType in ('Expense', 'Budget') AND tbl_allTransactions.transMonth = #transMonth
GROUP BY tbl_allTransactions.transMonth) as SumOftransAmount2
FROM tbl_allTransactions
WHERE tbl_allTransactions.userID=#userID

Append Query Trouble

I am having trouble with the final piece of my append query. I have the records generating just like I want with the exception of not triggering until the Expression Event Date is <=Date(). It is giving me a unmatched error when I place the <=Date() in the criteria field of the query builder. I tried it with DateSerial and a few other variations. I'm sure it has to do with the expression being that and not a hard date. Any assistance would be appreciated.
INSERT INTO SchedulingLog (
UserID
, LogDate
, EventDate
, Category
, CatDetail
, [Value]
)
SELECT Roster.UserID
, Date() AS LogDate
, DateSerial(Year(Date()),Month([WM DOH]),Day([WM DOH])) AS EventDate
, SchedulingLog.Category
, SchedulingLog.CatDetail
, Max(tblAccrual!WeeksAccrual*Roster!Schedule) AS [Value]
FROM tblAccrual
, [Schedule Type]
, Category
INNER JOIN CatDetail
ON Category.CategoryID = CatDetail.CategoryID
, SchedulingLog
INNER JOIN Roster
ON SchedulingLog.UserID = Roster.UserID
WHERE (((tblAccrual.Years)<=Round((Date()-[wm doh])/365,2)))
GROUP BY Roster.UserID
, Date()
, DateSerial(Year(Date()),Month([WM DOH]),Day([WM DOH]))
, SchedulingLog.Category
, SchedulingLog.CatDetail
HAVING (((SchedulingLog.Category) Like "Vac*")
AND ((SchedulingLog.CatDetail) Like "Ann*"));
I believe the issue is not explicitly converting the user input date with CDate. I suspect it's fine in most of the query because the [MW DOH] parameter is provided directly to functions which will convert it to date. However the WHERE clause would need an explicit conversion.
The following generates the error "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to the variables."
SELECT Date()-[userinput] AS something;
Whereas the following code does not
SELECT Date()-CDate([userinput]) AS something;

Syntax Error (missinng operator) in Query expression

I am attempting in Access to show some data in a listbox, but only data which meets a certain condition should be displayed, however my SQL is flagging the error;
Syntax Error (missing operator) in query expression tblItem.[Status] WHERE (tblItem.[Status] = 'Unassigned
Here is my full query
`SELECT tblItem.[Serial Number],
tblItem.[Item Description],
tblItem.[Model],
tblItem.[Status]
WHERE (tblItem.[Status] = 'Unassigned') FROM tblItem;`
Where am i going wrong?
try this
SELECT tblItem.[Serial Number],
tblItem.[Item Description],
tblItem.[Model],
tblItem.[Status]
FROM tblItem
WHERE tblItem.[Status] = 'Unassigned'
I think You have misplaced the from and where clause
Remember the syntax for select:
SELECT ... FROM ... WHERE ... ORDER BY

access sql query aggregate function

I m trying to run a sql in access 2007 which gives the error
"you tried to execute a query that does not include the specified expression 'round(volumePass/Volume*100,2)' as part of an aggregate function".
Whereas i have mentioned it in the sql statement
group by round(volumePass/Volume*100,2)
SELECT s.[sub process name], Round(Avg(s.[TollGate FTEs]),2) AS TollGateFTEs,
Sum(w.volume) AS Volume,
Sum(Switch(w.TAT='Pass',w.Volume,w.TAT='Fail',0)) AS VolumePass,
Sum(IIf(w.[combined accuracy]='PASS',1,0)) AS AccuracyPass,
Sum(IIf(w.[combined accuracy]='',0,1)) AS TotalAccuracy,
Round((VolumePass/Volume)*100,2) AS TATPercentage,
Round((AccuracyPass/TotalAccuracy)*100,2) AS AccuracyPercentage,
Format(w.[reporting month],'mmm-yy') AS [Rep Month] FROM Work_Log1 AS w,
[sub process] AS s WHERE w.[sub process] In (SELECT s.[sub_process_id] FROM
[Sub Process] s,
[process mapping] m where m.[process name] like 'Accounting Control%'
and s.[mapped to process id] = m.[mapping id]) And w.[sub process]=[s].[sub_process_id]
AND (w.[Activity_start_date] Between #06/01/2012# And #06/15/2012#)
AND ([w].[sla applicable]=1 Or 0 Or '') and
(w.[status] Like 'Complete%') group BY Format(w.[reporting month],'mmm-yy'),
s.[sub process name], (Round((VolumePass/Volume)*100,2));
Where is that it is not able to pick up correctly.
giving the alias "TATPercentage" still not works and gives the same error.
This query works well in access query designer but cannot be run with sql inside vba.
Update:
Got it to run using the dao engine in vba.. no luck with adodb engine
Round((VolumePass/Volume)*100,2) AS TATPercentage
^^^^^^^^^^ ^^^^^^
round() is not an aggregate function. You'll have to group on those fields as well (volumepass and volume), and you'll run into the same error with your next two lines with accuracypass, acuracytotal, reporting month, etc...

Using iif clause in Ms Access for a column with "Yes/No" Data Type

I have a column called DayShift in a table which is of Yes/No data type (Boolean). The result Output I want is: if the value is true, display "Day" else display night.
I have tried the following:
SELECT iif(DayShift=Yes,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift=True,"Day","Night") as Shift FROM table1;
SELECT iif(DayShift=1,"Day","Night") as Shift FROM table1;
But none of the above work. It just gives me a list of blank check boxes in the output datasheet window. I am using Ms Access 2003. Any help appreciated.
Update:
After a bit of research that the yes/no data type in Ms Access 2003 cannot handle null values appropriately. Hence, the error. Check this link for details.
Update 2
Real Query with the joins. Didnt mention it since i though the information provided above would work.
SELECT tblovertime.contfirstname AS [First Name],
tblovertime.contlastname AS [Last Name],
tblovertime.employeenumber AS [Employee Number],
tblsignup.thedate AS [Sign Up Date],
Iif([tblOvertime.DayShift] =- 1, "Day", "Night") AS shift,
(SELECT Mid(MIN(Iif(sector = 1, "," & sector, NULL)) & MIN(
Iif(sector = 2, "," & sector, NULL)) & MIN(
Iif(sector = 3, "," & sector, NULL)) & MIN(
Iif(sector = 4, "," & sector, NULL)), 2) AS concat
FROM tblempsectorlist
WHERE tblempsectorlist.empnum = tblsignup.employeenumber
GROUP BY empnum) AS sectors,
tblovertime.timedatecontact AS [Date Contacted],
tblovertimestatus.name AS status
FROM (tblsignup
INNER JOIN tblovertime
ON ( tblsignup.thedate = tblovertime.otdate )
AND ( tblsignup.employeenumber = tblovertime.employeenumber ))
INNER JOIN tblovertimestatus
ON Clng(tblovertime.statusid) = tblovertimestatus.statusid
WHERE (( ( tblsignup.thedate ) ># 1 / 1 / 2011 # ))
ORDER BY tblsignup.thedate;
Your second one has it right
SELECT iif(DayShift,"Day","Night") as Shift FROM table1;
I suggest trying the following to see what's actually being evaluated
SELECT iif(DayShift,"Day","Night") as Shift, DayShift FROM table1;
You could equally do
SELECT iif(DayShift = -1,"Day","Night") as Shift FROM table1;
Since MS Access is storing true as -1 and false as 0 (it's not as intuitive as true = 1, but it's probably faster to evaluate in twos-compliment)
-- edit --
Since you appear to be using a join, which can result in Nul's for Yes/No's, use the nz() function.
select iff(nz(DayShift, 0), "Day","Night") as Shift FROM table1;
When DayShift comes out null, this will return 0 (false/no) as a result instead.
This one might be stupid, but ....
In case you have some Null values in the DayShift field, Access will not be able to evaluate the formula. You could write your test this way:
iif(Nz(DayShift,0)=-1,"Day","Night")
I ran the following query successfully:
SELECT IIf([Table1]![isDayShift]=True,"Day","Night") AS Shift
FROM Table1;
I built this in Access 2007 (sorry, I didn't have a copy of 2003 lying around). The only difference I saw was that it gave the full path to the field instead of just the field name. However, that shouldn't be an issue. If it is giving you checkboxes, it would seem that it is not seeing this field as a text field but rather as a checkbox field still.
Bottom line is the above query should work.
You are binding to "DayShift" rather then the derived "Shift" in the form.
The 3rd query (=True) show when run in isolation should show Day/Night.
The Access database engine (ACE, Jet, whatever) has a clever/stupid feature that allows an expression in the SELECT clause to refer to an AS clause ("column alias") in the same SELECT clause if that AS clause is to the left of the expression. This makes it easy to test expressions with test data without using a table at all (assuming ANSI-92 Query Mode, I think) e.g. try executing any of the following statements individually: all should work and produce the expected result:
SELECT CBOOL(TRUE) AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT CBOOL(FALSE) AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT NULL AS DayShift,
IIF(DayShift = TRUE, 'Day', 'Night') AS Shift;
SELECT CBOOL(TRUE) AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;
SELECT CBOOL(FALSE) AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;
SELECT NULL AS DayShift,
IIF(DayShift, 'Day', 'Night') AS Shift;