query string when fieldname includes hyphen

M

Mike Williams

I have a vba query string that errors when I include a field with a name
that includes a hyphen, like so:

ActiveDocument.MailMerge.DataSource.QueryString = _
"SELECT * FROM `" & sourcepage & "$` WHERE " _
& "(`Student` IS NOT NULL)" _
& "AND (`Student` <> 0) " _
& "AND (`T-Raw` > 1) " _
& "AND (`email` IS NOT NULL)"

If I include the line with the field T-Raw, it errors, even though that is
indeed a valid field. If I delete that line, it runs fine.

How can I construct this string without changing the datasource field name?

THANKS.
 
J

Jezebel

Put square brackets around it. ... AND ([T-Raw] > 1) ...
This method is also used if the field name (or table name, for that matter)
is a reserved word (like Field or Date) or contains spaces. It does no harm
*always* to insert square brackets, and adds a measure of robustness to your
code.

Table and field names in SQL statements don't normally have quotes around
them.
 
W

willnot

Great. I'll try it. The quotes I thought were standard, in fact a
peculiar kind of quote created easiest by means of the macro recorder.
Didn't know about the brackets.
 
M

Mike Williams

Tried it. Didn't work. The code below produced the same error using
brackets as it did using the quotes. Any other thoughts? Something I
missed?

ActiveDocument.MailMerge.DataSource.QueryString = _
"SELECT * FROM `" & sourcepage & "$` WHERE " _
& "(`Student` IS NOT NULL)" _
& "AND (`Student` <> 0) " _
& "AND ([T-Raw] > 1)" _
& "AND (`No` > 100)" _
& "AND (`email` IS NOT NULL)"

Mike Williams
wrote in
 
M

Mike Williams

Surprisingly enough, I discovered that by just leaving out the hyphen, it
works fine--like so:
& "AND (`TRaw` > 1) " _
Go figure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top