A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Syntax error when using names with apostrophe



 
 
Thread Tools Display Modes
  #1  
Old May 4th, 2010, 04:20 PM posted to microsoft.public.access.queries
inbarik
external usenet poster
 
Posts: 3
Default Syntax error when using names with apostrophe


i'm getting syntax error message for this button that tries to get name (of
building) with apostrophe. Any idea how to solve this? Note that i must use
name with apostrophe e.g. O'neal:

Private Sub Command76_Click()
On Error GoTo Err_Command76_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = ChrW(1506) & ChrW(1491) & ChrW(1499) & ChrW(1503) & ChrW(32)
& ChrW(1508) & ChrW(1512) & ChrW(1496) & ChrW(1497) & ChrW(32) & ChrW(1502) &
ChrW(1489) & ChrW(1504) & ChrW(1492)

stLinkCriteria = "[Building]=" & "'" & Me![Building] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria
  #2  
Old May 4th, 2010, 04:27 PM posted to microsoft.public.access.queries
Stefan Hoffmann
external usenet poster
 
Posts: 991
Default Syntax error when using names with apostrophe

hi,

On 04.05.2010 17:20, inbarik wrote:
i'm getting syntax error message for this button that tries to get name (of
building) with apostrophe. Any idea how to solve this? Note that i must use
name with apostrophe e.g. O'neal:

You need to escape it.

stLinkCriteria = "[Building]="& "'"& Me![Building]& "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria

E.g.:

stLinkCriteria = "[Building]='" & _
Replace(Me![Building], "'", "''") & "'"

Or use a user defined function like

Public Function SqlQuote(AString As String, _
Optional ADelimiter As String = "'") As String

SqlQuote = ADelimiter & _
Replace(AString, ADelimiter, ADelimiter & ADelimiter) & _
ADelimiter

End Function

in a standard module and

stLinkCriteria = "[Building]=" & SqlQuote(Me![Building])


mfG
-- stefan --
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 11:32 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.