I want to concatenate two fields and in that 2 field should come in (). I am able to do the "(" but not able to get the closing brace
This is what I tried
[student].[Undergraduate Degree] & " (" & Year([student].[Graduation Date]) AS [Degree Recieved (Year)]
When I tried this, its showing error
[student].[Undergraduate Degree] & " (" & Year([student].[Graduation Date]) AS [Degree Recieved (Year)] & ")"
Tried few more combinations by keeping the braces at different places but none worked
[student].[Undergraduate Degree] & " (" &
Year([student].[Graduation Date]) & ")" AS [Degree Recieved (Year)]
Related
I am very new to SQL and think I have a simple problem but was unable to figure it out from other posts. I have the following code:
INSERT INTO tblShortScores ( TradeNum, FilterNum, Rank, ScoreNum )
SELECT [Forms]![frmOpenTrades]![TradeNum] AS TradeNum, tblFilters.FilterNum, tblFilters.SBBExh AS Rank, tblFilters.SBBExh AS Score
FROM tblFilters
WHERE (((tblFilters.SBBExh) Is Not Null));
but instead of using the literal "SBBExh" in tblFilters.SBBExh, I want to do something like
tblFilters.("S" & [Forms]![frmOpenTrades]![Strategy])
where something like
[Forms]![frmOpenTrades]![Strategy] contains the value "BBExh".
It's in MS Access and I seem unable to find a syntax that works
any help is appreciated
Can't dynamically build field name in query object. Use VBA to construct and execute action SQL, like:
strField = "S" & Me.Strategy
CurrentDb.Execute "INSERT INTO tblShortScores (TradeNum, FilterNum, ScoreNum) " & _
"SELECT " & Me.TradeNum & " AS TradeNum, FilterNum, " & strField & " " & _
"FROM tblFilters WHERE " & strField & " Is Not Null;"
Assumes TradeNum is number type - if it is text, use apostrophe delimiters:
SELECT '" & Me.TradeNum & "' AS .
If SQL injection is a concern review, How do I use parameters in VBA in the different contexts in Microsoft Access?
I'm trying to open a report from a form, where the user fills in a start and end date. My code is:
Docmd.OpenReport (rptAllSalesByDate, acViewReport, ,
"DateOfTransaction >= #" & me.txtStartDate & "# and DateOfTransaction =< #" & me.txtEndDate & "#",,,)
The above was broken into 2 lines for easier readability, but it's actually on one line.
It keeps saying it's expecting an expressing and highlighting the closing parenthesis.
Any ideas what I'm missing? I know my Access is rusty.
Do not leaving trailing commas when no parameters are provided for those arguments. This will trigger the indicated error. So will parentheses when not executing a function.
Consider:
Docmd.OpenReport "rptAllSalesByDate", acViewReport, ,
"DateOfTransaction BETWEEN #" & Me.txtStartDate & "# AND #" & Me.txtEndDate & "#"
Microsoft Access subform filter output looks like this:
**([qryPOExamDetail subform].[Line Description]="1"" CONDUIT - EMT")**
The actual value in the field is 1" CONDUIT - EMT. I've converted the above argument to [Line Description]='1"" CONDUIT - EMT' but the dynamic query returns zero records.
I've built a SQL statement to create a dynamic query for export to a comma delimited file via VBA doCmd.TransferText function. I filter on other fields (without "inch" marks) and it works fine. I've searched the internet for an answer and cannot find anything.
How do I get SQL to recognize that 1"" CONDUIT - EMT = 1" CONDUIT - EMT?
Below is the SQL string for creating the dynamic query:
strSQL1 = "SELECT tblOpenCommittment.[Job Number], tblOpenCommittment.[Job Name], tblOpenCommittment.[Order Number], tblOpenCommittment.[Supplier Name]," _
& "tblOpenCommittment.[Order Date],tblOpenCommittment.[Cost Code], tblOpenCommittment.[Line Description], tblOpenCommittment.Qty, tblOpenCommittment.Tax, " _
& "tblOpenCommittment.Price, tblOpenCommittment.Unit, tblOpenCommittment.[Total Line Value],tblOpenCommittment.[Total Line Value]-tblOpenCommittment.[Line Total Amount invoiced]" _
& "AS [Open]FROM tblOpenCommittment WHERE (((tblOpenCommittment.[Job Name])=[Forms]![frmPrintByProject]![txtBoxJobName]) AND ((tblOpenCommittment.[Cost Code])" _
& "Like" & Chr(34) & Chr(42) & Chr(34) & Chr(32) & Chr(38) & Chr(32) & "[Forms]![frmPrintByProject]![txtBoxFrameValue]) AND " & strWashedstrFilter & " )"
The very last argument strWashedstrFilter represents the output of the subform filter listed at the head of my original post. As I've stated earlier it works as long as the value filtered on does not contain ( " ).
I've read this site for years and gotten outstanding help. This is the first time I've ever posted a question. Thank you all in advance for taking the time to comment.
As an example, (as I am not really certain of your data structure or columns) you would use something like this for MS Access:
SELECT "My Quote goes > "" < right there"
So, to compare the values it would be like this:
SELECT ...
WHERE [MyColumn] = "1"" CONDUIT - EMT"
Or (if you prefer)
SELECT ...
WHERE [MyColumn] = '1" CONDUIT - EMT'
You are missing some spaces in the construct so words don't run together. And concatenate references to form controls:
strSQL1 = "SELECT *, [Total Line Value]-[Line Total Amount invoiced] AS [Open] " & _
"FROM tblOpenCommittment " &
"WHERE [Job Name]='" & [Forms]![frmPrintByProject]![txtBoxJobName] & _
"' AND [Cost Code] LIKE '*" & [Forms]![frmPrintByProject]![txtBoxFrameValue] & _
"' AND " & strWashedstrFilter
If code is behind frmPrintByProject, can use Me.:
"WHERE [Job Name]='" & Me.txtBoxJobName & "
I have a very weird problem occurring in MS Access which I can't seem to figure out.
Summary: I have a table from Sharepoint that is connected to my MS Access database and a Person table in my Ms Access db. I pull the information row by row from the Sharepoint table and add it to Person Table.
However, before adding the new data I must check if that specific Person already exists in my table. I check for 'Lastname', 'Firstname' and 'Date created' using DLookup function.
Here where everything goes side ways. DLookup returns me a NULL for almost half of the records that already exist in Person Table. After playing a lot with the condition in DLookup statement my conclusion is that there is a problem with the 'Date created' parameter, yet I have tried using "#" and CDate and even Format, nothing works.
I can't share the data, since it's sensitive, however the syntax for DLookup I'm using is the following:
sqlStr = "LastName=" & Chr(34) & rs![Last Name] & Chr(34)
& " AND FirstName=" & Chr(34) & rs![First Name] & Chr(34)
& " AND DateLastModified=" & Format(dateVar, "dd/mm/yyyy")
DLookup("LastName", "table_Person", sqlStr)
P.S: I have tried DCount, same thing happens. DCount returns 0 yet I know for a fact the record is there.
To build criterias BuildCriteria is your Friend.
Sub TestBuildCriteria()
Dim strCriteria As String
strCriteria = BuildCriteria("OrderDate", dbDate, [Date created])
MsgBox strCriteria
End Sub
Sub YourCode()
sqlStr = BuildCriteria("LastName", dbText, "=" & rs![Last Name]) & _
" AND " & BuildCriteria("FirstName", dbText, "=" & rs![First Name]) & _
" AND " & BuildCriteria("DateLastModified", dbDate, "=" & dateVar)
End Sub
This echoes the proper formated date. Also useful for other data-type. E.g. it escapes Quotation Marks in Strings. Read Custom Filters using BuildCriteria() too.
But there is a far easier alternative.
Create a unique composite index on LastName, FirstName and DateLastModified in the the table. Now you can't insert a duplicate as it has to be unique. If you try you will receive an error msg. Be aware of transaction rollbacks (e.g. Multiple inserts, one fails by key violation -> all actions will be reverted due transaction rollback if you use db.Execute SQL, dbFailOnError).
To check for dates use:
"DateLastModified=#" & FormatDateTime(dateVar, vbShortDate) & "#"
if dateVar can be null you need something like this:
FormatDateTime(Nz(dateVar,CDate("1/1/2000")), vbShortDate)
And of course that just checks the date part. If your dateVar can also have a time part then you have to use
DateValue(dateVar)
Your syntax is not correct. You should put square brackets around field names as is pointed out in documented examples at MSDN
sqlStr = "[LastName]=" & Chr(34) & rs![Last Name] & Chr(34)
& " AND [FirstName]=" & Chr(34) & rs![First Name] & Chr(34)
& " AND [DateLastModified]=#" & Format(dateVar, "dd/mm/yyyy") & "#"
DLookup("[LastName]", "table_Person", sqlStr)
In this situation, my advice would be to simplify the criteria part of the DLOOKUP/DCOUNT until you get something that works, and only then start to make the criteria more complex. I call this 'sanity checking'.
Date/Time criteria often cause problems, so first check that you can make it work without the Date part of the criteria.
For example, in your case, check that this works.
Use the Debug Window (Ctril+G) to test this:
? DCount("*", "table_Person", "LastName=" & Chr(34) & rs![Last Name] & Chr(34))
Then try:
? DCount("*", "table_Person", "LastName=" & Chr(34) & rs![Last Name] & Chr(34) & " AND FirstName=" & Chr(34) & rs![First Name] & Chr(34))
Once you have that working, add in the Date criteria.
Building the criteria up in stages like this, allows you to confirm which part is actually causing the problem.
I'm in the UK, and so I have my dates displayed in UK format - 'DD/MM/YYYY'.
However, when specifying a date criteria for a DLOOKUP/DCOUNT, I always have to format the date to US format. I've often used a simple function to swap the digits into the correct order for the criteria:
Function HashDate(dD As Date) As String
HashDate = "#" & Format$(dD, "MM/DD/YYYY") & "#"
End Function
In the the Debug Window:
? Date
09/03/2018
? HashDate(Date)
#03/09/2018#
I am creating a query and have the following SQL line
WHERE (((TblBreaksGiven.FinancialYear)='" & sql_FinancialYear & "')
AND
((TblBreaksGiven.Quarter) IN (" & sql_Quarters & "))); "
sql_quarters is based on a list box on a form, if I select one or more quarters the query works, however, if no quarter is selected I want the default to be all quarters. I don't want to hard code set sql_quarters to something like 'Q1,Q2,Q3,Q4' and have that used if the count of items in the list box is 0. Is there almost like a wildcard I can use with IN I tried something basic like IN (*) to no avail.
You can try this :
WHERE ((((TblBreaksGiven.FinancialYear)='" & sql_FinancialYear & "') AND
(((TblBreaksGiven.Quarter) IN (" & sql_Quarters & ")))
OR (TblBreaksGiven.Quarter) LIKE '*" & sql_Quarters & "*' )); "