The single quotes are required because tbl_Perm_ServNameSubDay.Day is
obviously a text field or you wouldn't be trying to pass it Mon, Tue, etc.
In actual fact, though, I mislead you: the Format statement shouldn't be in
quotes, so the double quotes aren't required in this case. You're also
missing the initial single quote. (The explanation of why everything is
turning green is that your single quote isn't inside double quotes, so
Access sees it as a comment delimiter)
As well, you should rename the field: Day is a reserved word, and you should
use reserved words for your own purpose. (For a comprehensive list of names
to avoid, see what Allen Browne has at
http://www.allenbrowne.com/AppIssueBadWord.html) If you cannot (or will not)
rename the field, at least put square brackets around it:
strSQL = "SELECT [qry_(1)ServersAndSubBackups].Client, " & _
"[qry_(1)ServersAndSubBackups].ServName, " & _
"tbl_Perm_ServNameSubDay.[Backup Name], " & _
"tbl_Perm_ServNameSubDay.[Day] " & _
"FROM [qry_(1)ServersAndSubBackups] " & _
"LEFT JOIN tbl_Perm_ServNameSubDay ON " & _
"[qry_(1)ServersAndSubBackups].ServSubID =
tbl_Perm_ServNameSubDay.ServSubID " & _
"WHERE tbl_Perm_ServNameSubDay.[Day] = '" & _
Format(VBA.Date, "ddd") & "' " & _
"ORDER BY [qry_(1)ServersAndSubBackups].Client, " & _
"[qry_(1)ServersAndSubBackups].ServName, " & _
"tbl_Perm_ServNameSubDay.[Backup Name];"
Exagerated for clarity, that's
"WHERE tbl_Perm_ServNameSubDay.[Day] = ' " & _
Format(VBA.Date, "ddd") & " ' " & _
Note that you need the space after the second single quote (or else one
before ORDER BY)
--
Doug Steele, Microsoft Access MVP
(no private e-mails, please)
GLT said:
Hi Douglas,
Thanks for your reply - I tried this as follows:
strSQL = "SELECT [qry_(1)ServersAndSubBackups].Client, " & _
"[qry_(1)ServersAndSubBackups].ServName, " & _
"tbl_Perm_ServNameSubDay.[Backup Name], " & _
"tbl_Perm_ServNameSubDay.Day " & _
"FROM [qry_(1)ServersAndSubBackups] " & _
"LEFT JOIN tbl_Perm_ServNameSubDay ON " & _
"[qry_(1)ServersAndSubBackups].ServSubID =
tbl_Perm_ServNameSubDay.ServSubID " & _
"WHERE tbl_Perm_ServNameSubDay.Day = " & _
"Format(VBA.Date, ""ddd"") & "'" & _
"ORDER BY [qry_(1)ServersAndSubBackups].Client, " & _
"[qry_(1)ServersAndSubBackups].ServName, " & _
"tbl_Perm_ServNameSubDay.[Backup Name];"
Now everything turns green after the single quote - what is the purpose of
the single quote in the first place? If i remove the single quote I get a
run time error...
Thanks,
GLT.
:
In order to include quotes inside of quotes, you need to double them up:
"Format(VBA.Date, ""ddd"") & "'" & _
(note, too, the other correction I made to your quotes after the Format
statement)
--
Doug Steele, Microsoft Access MVP
(no private e-mails, please)
Hi Ken,
Thanks to both yourself and Daniel's responses,
I have edited my sql statement and it now looks like this:
strSQL = "SELECT [qry_(1)ServersAndSubBackups].Client, " & _
"[qry_(1)ServersAndSubBackups].ServName, " & _
"tbl_Perm_ServNameSubDay.[Backup Name], " & _
"tbl_Perm_ServNameSubDay.Day " & _
"FROM [qry_(1)ServersAndSubBackups] " & _
"LEFT JOIN tbl_Perm_ServNameSubDay ON " & _
"[qry_(1)ServersAndSubBackups].ServSubID =
tbl_Perm_ServNameSubDay.ServSubID " & _
"WHERE tbl_Perm_ServNameSubDay.Day = """ & _
"Format(VBA.Date, "ddd") & "" ' " & _
"ORDER BY [qry_(1)ServersAndSubBackups].Client, " & _
"[qry_(1)ServersAndSubBackups].ServName, " & _
"tbl_Perm_ServNameSubDay.[Backup Name];"
It still says I have a syntax error - I do understand using the
Format(VBA.Date,"ddd") to extract the current day (its what i used in
the
strDay This works for Tue - Fri, but on Mondays (ie. strDay = mon) then
I
was
hoping to have strDay= "sun" or "sat" or "fri" in the above sql as
well.
These quotes in SQL drive me up the wall...
:
As Daniel has said you need to delimit the value with quotes
characters,
but
if the value is always the current day then you don't need the
variable
at
all. You can use:
"WHERE tbl_Perm_ServNameSubDay.Day = """ & _
Format(VBA.Date,"ddd") & ""' " & _
Note the use of the pair of contiguous quotes characters within the
strings
to denote a literal quotes character. The alternative is to use a
single
quotes character as Daniel did, which will be fine in this context,
but
would
be a problem with strings containing apostrophes, e.g. Irish names
like
O'Siridean (which just happens to be mine in its un-anglicized form,
though
strictly its Ó Siridean). If values can contain both single and
double
quotes characters then you have to resort to a more elaborate solution
by
using the Replace function to substitute another character for each
when
comparing values. A High ASCII character like the tilde is often
used,
e.g.
strName = "Cináed ""Born of Fire"" O'Siridean"
strFind = "O'Siridean"
strName = Replace(strName,"""","~")
strName = Replace(strName,"'","~")
strFind = Replace(strName,"""","~")
strFind = Replace(strName,"'","~")
? strName Like "*" & strFind & "*"
True
Another thing about your code is that you are not including a space at
the
end of each line, so the expression will run things together e.g.
"...tbl_Perm_ServNameSubDay.Day" & _
"FROM [qry_(1)ServersAndSubBackups]..."
would evaluate as
...tbl_Perm_ServNameSubDay.DayFROM [qry_(1)ServersAndSubBackups]...
Sometimes the lack of a space won't matter, but often it will produce
an
error.
Ken Sheridan
Stafford, England
:
Hi,
would anyone be able to help with the following SQL:
strSQL = "SELECT [qry_(1)ServersAndSubBackups].Client,
[qry_(1)ServersAndSubBackups].ServName,
tbl_Perm_ServNameSubDay.[Backup
Name], tbl_Perm_ServNameSubDay.Day" & _
"FROM [qry_(1)ServersAndSubBackups] LEFT JOIN
tbl_Perm_ServNameSubDay
ON
[qry_(1)ServersAndSubBackups].ServSubID =
tbl_Perm_ServNameSubDay.ServSubID"
& _
"WHERE (((tbl_Perm_ServNameSubDay.Day) = " & strDay & "]))" & _
"ORDER BY [qry_(1)ServersAndSubBackups].Client,
[qry_(1)ServersAndSubBackups].ServName,
tbl_Perm_ServNameSubDay.[Backup
Name];"
I am trying to use the string called strDay (which contains the
current
day
ie. mon = monday, tue = tuesday etc).
I would also like to put a condition when strDay = "mon", the where
extracts
"sun" or "sat" or "sun"...
Is this possible or will it make the SQL to complicated?
Any assistance would be greatly appreciated.
Cheers,
GLT.