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 & "#"
Related
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 have a query in Access named 'Laufzettel' and it looks in this way. The dates are in dd.mm.yyyy format
Antragsnummer Eingang esigniert Ausgang Anlage Policierung
111 2.10.2016 2.10.2016 3.10.2016 3.10.2016
222 3.10.2016 3.10.2016 3.10.2016 4.10.2016
333 5.10.2016 6.10.2016 7.10.2016 7.10.2016
I am creating a form named 'overview' with two Textboxes named StartDate and EndDate and search button. What I need is When I click the search button by giving in two dates it should retrieve me the related records from the above query with all the fields in form of a report.
The StartDate and EndDate are related to the Eingang field of the query. SO when I enter 2.10.2016 as StartDate and 5.10.2016 as EndDate it must produce the above query. To achieve this I have started to create a query using the form fields which in turn can produce me the required report when I click the search button. So I am trying with the code
SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung
FROM Laufzettel
WHERE Lauzettel.Eingang BETWEEN [Forms]![overview]![StartDate] and [Forms]![overview]![EndDate];
For which I get an error Access cannot recognise [Forms]![overview]![StartDate] and [Forms]![overview]![EndDate] as a valid field name or expression
How can I achieve the above? Can someone help me?
EDIT : Here is my working Query now. Although It doesn't give me the records from EndDate values.
PARAMETERS [Forms]![overview]![start] DateTime, [Forms]![overview]![end] DateTime;
SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.AEingangDatenstromZWorkflow, Laufzettel.BEingangesigniertDokumentZWorkflow, Laufzettel.CAusgangDatenstromZWorkflow, Laufzettel.DAnlageSchwebeVSL, Laufzettel.EPolicierungVSL
FROM Laufzettel
WHERE (((Laufzettel.AEingangDatenstromZWorkflow) Between [Forms]! [overview]![start] And [Forms]![overview]![end]));
Can someone tell me where am I going Wrong?
If build in code (VBA), try with:
SQL = "SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung " & _
"FROM Laufzettel " & _
"WHERE Lauzettel.Eingang BETWEEN #" & Format([Forms]![overview]![StartDate], "yyyy\/mm\/dd") & "# AND #" & Format([Forms]![overview]![EndDate], "yyyy\/mm\/dd") & "#"
If the "date" field really is text, apply DateValue:
SQL = "SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung " & _
"FROM Laufzettel " & _
"WHERE DateValue(Lauzettel.Eingang) BETWEEN #" & Format([Forms]![overview]![StartDate], "yyyy\/mm\/dd") & "# AND #" & Format([Forms]![overview]![EndDate], "yyyy\/mm\/dd") & "#"
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)]
I am trying to save individual records in a report to PDF files. using Access 2010. I got this to work, but I need to put a start date and end date in, and I can't seem to figure out the syntax. Here is what I have so far:
DoCmd.OpenReport "Rpt Form Responses", acViewReport, , "[Facility Number]=" & temp & _ And [Service Date] Between & begindate And enddate;
The top line works fine but when I add the second I can't get it to work. I have tried all the qualifiers', ", and # and many different variations of syntax with no luck.
begindate and enddate are strings that I capture from an input box. Should I change these to a date? I have done a lot of reading on here and based on suggestions I thought about putting a text box on the form for begin and end but I think I would rather do the inputbox.
Are you sure the syntax you posted above is accurate, because it is not correct.
I am assuming 'temp' is numeric, else you need to enclose in single-quotes;
Tthe continuation line is missing the leading " followed by a space;
Missing quotes, spaces around your BETWEEN and AND
You can get away with strings for the dates as long as you did something like:
begindate = #1/1/2005#
enddate = #1/1/2012#
Here is what I believe your syntax should look like (I tested and it works):
DoCmd.OpenReport "Rpt Form Responses", acViewReport, , "[Facility Number]=" & temp & _
" And [Service Date] Between #" & begindate & "# And #" & enddate & "#"
Below query is not returning any rows into the listbox.There is no error message:
lstDiff.RowSource = "select TestScenario,TestId from tblTesting where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'"
Could anyone help?
Your values in the field are numeric, so the extra single quotes aren't needed. Code should look like the following:
Me.lstDiff.RowSource = "select TestScenario,TestId from tblTesting where empid= " & Me.txtEmpId & " and testid= " & Me.txtAutoNumber & ";"
I've also dropped .Value from the field references, they're not harmful, but also aren't necessary.
And I've added a semi-colon to the end of your statement.
Depending on when/where you insert this code, you may need to add the following statement as well:
Me.lstDiff.Requery
You keep posting questions about the exact same WHERE clause with exactly the same apparent error in each one. SO users dutifully point out your error and then a few days later, you show up with a related question utilizing the same faulty WHERE clause.
DLookup Problem:
txtContSunStart1.Value = DLookup("Start1", "tblContract", _
"TestId = " & _
lstResults.Value & _
"" And "Day = 'Sunday'")
VBA Update Query:
DoCmd.RunSQL (" Update tbltesting set IsDiff ='Yes' where empid= " & Me.txtEmpId.Value & " and testid= " & Me.txtAutoNumber.Value & ";")
VBA SQL Problem
DoCmd.RunSQL ("insert into tbltesting (IsDiff)values ('Yes') where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'")
And then in the current question:
lstDiff.RowSource = "select TestScenario,TestId from tblTesting where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'"
You are having difficulties with exactly the same set of problems repeatedly.
Here are the rules for concatenating SQL strings with the correct delimiters in Access:
numeric values do not need delimiters:
"... AND testid= " & Me!txtAutoNumber
text values need quote delimiters. In Access, it's general practice to use double quotes, but much easier to use single quotes since it's a pain to type double quotes in a form that will work (typing """ or """" depending on context is counterintuitive and silly to me, so I always define a global constant that holds the double quote symbol and concatenate with that).
"... AND textfield=" & Chr(34) & Me!MyTextField & Chr(34)
date values use the # delimiter:
"... AND datefield=#" & Me!MyDateField & "#"
Boolean fields require no quotes and it works best to use True and False:
"... AND IsDiff=True"
These rules apply both to WHERE clause criteria and to SET statements in UPDATE queries. The rules apply in writing a SQL string that you pass to DoCmd.RunSQL or CurrentDB.Execute, as well as to writing SQL strings to be used as the recordsource of a form or report or as the rowsource of a combo box or listbox.
Personally, whenever I use SQL statements in code, I prefer to store the statement in a variable. While testing, on the line after you assign your statement to a variable, you can use Debug.Print to see what your SQL statement looks like after parsing your txtempid and txtautonumber. It would look something like this.
Dim sSQL as String
sSQL = "select TestScenario,TestId from tblTesting where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'"
Debug.Print sSQL
lstDiff.RowSource = sSQL
Then as long as your immediate window is visible (Ctrl-G), you can see what your SQL statement really is. If it looks right in the immediate window, copy and paste it into the query builder and run it there.
Try running the query in your SQL Management Studio. Do you get any row(s) back?
Edit: Just noticed the access-tag. Are you sure your table contains at least one post with supplied ids?
My Access is a bit rusty, but if all else fails try using a recordset to capture the data from the SQL and loop through it adding the values to the list box. Example Code