+ Reply to Thread
Results 1 to 7 of 7

Syntax error due to an apostrophe in string ?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Syntax error due to an apostrophe in string ?

    Hello everyone. I am currrently working on a project which has me mostly debugging code. I have a user-form that allows
    a user to select from a drop down list a "terminal" (which by-the-way is a list of names of various physical plant facilities:
    e.g. St. John's). After selecting from the drop-down list and after entering other data the user will then press a button called "Save Volumes".
    There is a problem however when a user selects a terminal name that has an apostrophe in it, for instance St. John's, it causes the following error:

    "Syntax error (missing operator) in query expression ...."

    Now just so you know the actual list of terminal name is stored in a Access database, the 1997 version. Below is the code in question
    with an error pointing to the error. Any help is appreciated.

    The "Save Volumes" button code is below:

    Private Sub btnSaveVolume_Click()
        Dim custName As String
        Dim strVol As String
        Dim SalesOrgToUseForSave As String
        custName = Customer.Text & txtCustomer.Text
        VolSaved = -1
        If ValidateVolume Then ' validate the Volume entries
            'if Valid, then add this set of entries to the Opportunity Details grid
            SalesOrgToUseForSave = Sheets(VALIDATIONS).Range("CurrentSalesOrg")
            If Terminal.Text = "*ALL TERMINALS" Or Terminal.Text = "*OTHER" Then SalesOrgToUseForSave = "0000"
            If NewVol Then
                If volCode = 0 Then
                    Dim rs As New ADODB.Recordset
                    rs.Open "Select Max(VolCode) + 1 as VCode from VOL", VolConn
                    If rs!VCode & "" = "" Then
                        volCode = 1
                    Else
                        volCode = rs!VCode
                        Set rs = Nothing
                    End If
                End If
                VolAdded = True
                strVol = "INSERT INTO VOL (VolCode, OpCode, Terminal, Product, PartialVolume, SALES_ORG) Values (" & _
                    volCode & "," & lOpCode & ",'" & Terminal.Text & "','" & Replace(Product.Text, "'", "''") & "'," & _
                    PartialVolume.Text & ",'" & SalesOrgToUseForSave & "')"
                addedVol.Add volCode
            Else
                VolAdded = False
                strVol = "UPDATE VOL Set Terminal = '" & Terminal.Text _
                    & "', SALES_ORG = '" & SalesOrgToUseForSave _
                    & "', Product = '" & Replace(Product.Text, "'", "''") & "'," _
                    & "PartialVolume = " & PartialVolume.Text _
                    & " where VolCode = " & volCode
            End If
            
            VolConn.Execute strVol <------ Error
            FillOppDetails
            ClearVolDetails
    
            VolSaved = 1
            btnAddVolume.Visible = True
            btnAddVolume.Caption = "Add New Volume"
            btnDeleteVolume.Visible = False
            btnSaveVolume.Visible = False
            SetVolFields False
            addedVol.Add volCode
            ResetFrames
            lblInstruction.Visible = False
        End If
        volCode = 0
    End Sub
    Last edited by AnthonyWB; 10-22-2010 at 11:29 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Syntax error due to an apostrophe in string ?

    I suspect you need to replace the ' with a ''

    Replace(Terminal.Text,"'","''")
    edit: seems you have done this already re: Product so the above might be moot.

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Syntax error due to an apostrophe in string ?

    I can't realy follow you. I see the code piece you are talking about. Are you sugestig the following correction:

    Replace(Product.Text, " ' ", " ' ' ") corrected to Replace(Product.Text, " ' "," ' ").

    And why will that work ?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Syntax error due to an apostrophe in string ?

    No, I was suggesting you add what I then realised you already had in place...

    Replace(Product.Text,"'","''")
    is correct

    If however we're referring here to Terminal rather than Product then you should ape the above for Terminal also.

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Syntax error due to an apostrophe in string ?

    I got you, I think. You are suggesting:

     strVol = "INSERT INTO VOL (VolCode, OpCode, Terminal, Product, PartialVolume, SALES_ORG) Values (" & _
                    volCode & "," & lOpCode & ",'" & Terminal.Text & "','" & Replace(Terminal.Text, "'", "''") & "'," & _
                    PartialVolume.Text & ",'" & SalesOrgToUseForSave & "')"
    instead of,

     strVol = "INSERT INTO VOL (VolCode, OpCode, Terminal, Product, PartialVolume, SALES_ORG) Values (" & _
                    volCode & "," & lOpCode & ",'" & Terminal.Text & "','" & Replace(Product.Text, "'", "''") & "'," & _
                    PartialVolume.Text & ",'" & SalesOrgToUseForSave & "')"

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Syntax error due to an apostrophe in string ?

    No, I am suggesting:

     strVol = "INSERT INTO VOL (VolCode, OpCode, Terminal, Product, PartialVolume, SALES_ORG) Values (" & _
                    volCode & "," & lOpCode & ",'" & Replace(Terminal.Text,"'","''") & "','" & Replace(Product.Text, "'", "''") & "'," & _
                    PartialVolume.Text & ",'" & SalesOrgToUseForSave & "')"
    ie encase both Terminal & Product within a Replace [and maybe even SalesOrgToUseForSave if that too can contain apostrophe's]

    You would need to do likewise in your UPDATE statement.

    edit: the point is I am assuming Terminal.Text contains St.John's ... you've not specified that as yet (as far as I can tell)
    Last edited by DonkeyOte; 10-21-2010 at 02:59 PM. Reason: added edit

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Syntax error due to an apostrophe in string ?

    Looks like we got it. Thank you !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1