Has CRecordset m_strFilter changed with Access 2007?

  • Thread starter Thread starter cte67
  • Start date Start date
C

cte67

My code for other versions of Access has the following code:

m_strFilter = "City = Marshalltown"

This does not work with Access 2007. I found through trial and error I must
use:

m_strFilter = "City = 'Marshalltown'"

Is there a workaround so I can eliminate single apostrophes?
 
cte67 said:
My code for other versions of Access has the following code:

m_strFilter = "City = Marshalltown"

This does not work with Access 2007. I found through trial and error I
must
use:

m_strFilter = "City = 'Marshalltown'"

Is there a workaround so I can eliminate single apostrophes?

I can't imagine how your original code (without the quotes) could ever have
worked in any version of Access. A text literal in SQL must be enclosed in
quotes. The only exception I can think of would be a numeric value that
could be implicitly converted to text by the SQL parser. I believe you are
mistaken in thinking that the unquoted literal worked in previous versions.
 
A literal value (like Marshalltown) inside a string must have quote marks
around it if applied against a field of type Text (like City.)

That's the correct way to do it in any version of Access. It should not have
worked in previous version of Access without the quotes, and even if it did,
that's the kind of "mistake" that easily breaks when you change versions
(because you were relying on something faulty.)

Personally, I prefer to use double-quotes, because that doesn't foul up with
names that contain an apostropy. More on how to do that here:
Quotation marks within quotes
at:
http://allenbrowne.com/casu-17.html
 
On Sun, 27 Jan 2008 18:03:01 -0800, cte67

It's very strange the original code would have worked. Perhaps your
driver was doing something special. Your new code follows the official
syntax, so better get used to it: wrap text values in single quotes.

-Tom.
 
My code for other versions of Access has the following code:

m_strFilter = "City = Marshalltown"

This does not work with Access 2007. I found through trial and error I must
use:

m_strFilter = "City = 'Marshalltown'"

Is there a workaround so I can eliminate single apostrophes?

If your city name contains an apostrophe (St. Mary's Harbour say) you'll need
doubledoublequotes instead:

"[City] = """St. Mary's Harbour"""

John W. Vinson [MVP]
 
On Sun, 27 Jan 2008 19:40:24 -0700, John W. Vinson

Or double-up on the single-quotes:
"[City] = "St. Mary''s Harbour"

-Tom.

My code for other versions of Access has the following code:

m_strFilter = "City = Marshalltown"

This does not work with Access 2007. I found through trial and error I must
use:

m_strFilter = "City = 'Marshalltown'"

Is there a workaround so I can eliminate single apostrophes?

If your city name contains an apostrophe (St. Mary's Harbour say) you'll need
doubledoublequotes instead:

"[City] = """St. Mary's Harbour"""

John W. Vinson [MVP]
 
On Sun, 27 Jan 2008 19:40:24 -0700, John W. Vinson

Or double-up on the single-quotes:
"[City] = "St. Mary''s Harbour"

A double singlequote - a double doublequote - a single singlequote...

Access can involve a lot of doubletalk! <g>


John W. Vinson [MVP]
 
Thanks for the comments.

Interestingly, the code has been working for well over a decade with no
single quotes ... was using Access 97. Never had a single complaint, even
though I have multiple tables.

I tried using double quotes: m_strFilter = "City = ""Marshalltown""" ...
generates an error.
 
cte67 said:
Thanks for the comments.

Interestingly, the code has been working for well over a decade with no
single quotes ... was using Access 97. Never had a single complaint, even
though I have multiple tables.

I tried using double quotes: m_strFilter = "City = ""Marshalltown""" ...
generates an error.


There's something missing from the story. Maybe if you show all the
relevant code and and tell us exactly what error code and message you are
getting, we can figure it out.
 
The error msg is, "Too few parameters. Expected 1."

The code is:

//------------------------------
int OdbcRequeryFilterSort(CRecordset* pRs, CString Filter, CString Sort)
{
//Returns 0 = ok
// -1 = error
if(!OdbcIsTableOpen(pRs))
{
AfxMessageBox("OdbcRequeryFilterSort, The table is not open!");
return -1;
}
pRs->m_strSort = Sort;
pRs->m_strFilter = Filter;
try
{
pRs->Requery();
}
catch(CException* pe)
{

MessageBeep(MB_ICONEXCLAMATION);
pe->ReportError();
pe->Delete();
CString Str = _T("OdbcRequeryFilterSort--CException: ");

AfxMessageBox(Str);

return -1;
}

return 0;
}//OdbcRequeryFilterSort
//------------------------------
 
cte67 said:
The error msg is, "Too few parameters. Expected 1."

The code is:

//------------------------------
int OdbcRequeryFilterSort(CRecordset* pRs, CString Filter, CString Sort)
{
//Returns 0 = ok
// -1 = error
if(!OdbcIsTableOpen(pRs))
{
AfxMessageBox("OdbcRequeryFilterSort, The table is not open!");
return -1;
}
pRs->m_strSort = Sort;
pRs->m_strFilter = Filter;
try
{
pRs->Requery();
}
catch(CException* pe)
{

MessageBeep(MB_ICONEXCLAMATION);
pe->ReportError();
pe->Delete();
CString Str = _T("OdbcRequeryFilterSort--CException: ");

AfxMessageBox(Str);

return -1;
}

return 0;
}//OdbcRequeryFilterSort
//------------------------------


Okay, so this isn't really an Access question, but a .Net/ODBC/(maybe)Jet
question. But we can still probably figure out what's wrong.

Your code refers to ODBC; are you in fact querying a Jet database by way of
ODBC? Is the database in question an .mdb file or an .accdb file?

The code doesn't show the call to the function, so I can't see what you're
passing in the arguments "Filter" and "Sort". You should also post the code
that constructs those arguments and calls the function. When you step
through the code in debug mode, what values do you see for them?

The error message is one that Jet gives when you it doesn't recognize a name
in the query. We commonly see it when the name of a field is misspelled or
a text literal is not properly quoted. At the moment, I think that's the
most likely explanation, so please double-check that you haven't misspelled
the name of the field, and that the text literal is properly quoted.
 
The code was written about ten years ago using the ODBC Microsoft Access
Driver to access a .mdb file (Access 97). The code was thoroughly debugged
back then so I have a high level of confidence all parameters passed are
correct (no typos, but apparently an issue with single quote marks). The
code has worked flawlessly for a decade. However, I did step through the
code to verify the params I'm passing are still the correct ... they are.
Nothing is misspelled and the text is properly quoted.

The database has a half dozen tables and reports that contain things such as
phone numbers, mailing addresses, and mailing labels.

When I converted the .mdb to a .accdb file using Access 2007, I was not
positive my program would still work ... it did. The few queries I tried
worked fine.

A few days ago my wife opened the database to print some mailing labels.
The first line in each address label contained the word Error. After some
debugging I discovered the word "Name" is no longer allowed for a field. I
changed the field to "Names" and modified the code for the new field name.
(It's unfortunate this is a new requirement for Access 2007 ... I have no
idea how many other programs / databases of mine use the word Name.)

However, this led me to check out other table queries and I discovered one
that no longer works.

Note that the using single quote marks works fine ... which confirms there
are no typos. I was hoping that I could use double quote marks instead to
make it easy to do searches that contain single quote marks. Any suggestions
are appreciated.
 
cte67 said:
The code was written about ten years ago using the ODBC Microsoft Access
Driver to access a .mdb file (Access 97). The code was thoroughly
debugged
back then so I have a high level of confidence all parameters passed are
correct (no typos, but apparently an issue with single quote marks). The
code has worked flawlessly for a decade. However, I did step through the
code to verify the params I'm passing are still the correct ... they are.
Nothing is misspelled and the text is properly quoted.

The database has a half dozen tables and reports that contain things such
as
phone numbers, mailing addresses, and mailing labels.

When I converted the .mdb to a .accdb file using Access 2007, I was not
positive my program would still work ... it did. The few queries I tried
worked fine.

A few days ago my wife opened the database to print some mailing labels.
The first line in each address label contained the word Error. After some
debugging I discovered the word "Name" is no longer allowed for a field.
I
changed the field to "Names" and modified the code for the new field name.
(It's unfortunate this is a new requirement for Access 2007 ... I have no
idea how many other programs / databases of mine use the word Name.)

However, this led me to check out other table queries and I discovered one
that no longer works.

Note that the using single quote marks works fine ... which confirms there
are no typos. I was hoping that I could use double quote marks instead to
make it easy to do searches that contain single quote marks. Any
suggestions
are appreciated.


The only thing I can think of is that you should look for an updated driver
for the Access 2007 file format. Maybe one of the connection strings posted
at these links will work:

http://connectionstrings.com/?carrier=access

http://connectionstrings.com/?carrier=access2007
 
Use single quotes as the delimiter, and wrap the Replace function around the
value being passed to change any single quote in the expression into two
consecutive single quotes.

See my May, 2004 "Access Answers" column in Pinnacle Publication's "Smart
Access" for details. You can download the column (and sample database) for
free at http://www.accessmvp.com/DJSteele/SmartAccess.html
 
I'm using the ODBC driver provided by WinXP SP2 ... not sure I have a choice
in a different driver as it is part of the ODBC that comes with WinXP.

Changing single quotes into two single quotes should work fine for me.

Thanks to all who responded.
 
Mystery solved. This whole thing really bugged me ... so I did more
investigation. The problem is in a library where I mixed up Access database
code with some CTree database code. My old program has always used the
single quotes ... recompiling it is what caused the problems.

Again, thanks to all.
 
Back
Top