Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28
  1. #16
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I just ran a test..



    On a form, I added two text boxes (Text4 & Text6) and a button. The code in the Click event of the button is:
    Code:
    Private Sub Command3_Click()
       
       Me.Text6 = ConvertQuotes(Text4)
    
    End Sub
    The code in the function is:
    Code:
    Function ConvertQuotes(InputVal)
       ConvertQuotes = Replace(InputVal, """", "")
    End Function
    (Expanded: ConvertQuotes = Replace(InputVal, " " " ", "") )



    If I type Kay" in Text4 and click the button, Kay is displayed in Text6

    If I type "Kay" in Text4, Kay is displayed in Text6.

    If I change the button code to:
    Code:
    Private Sub Command3_Click()
       Dim sSQL As String
       
       Me.Text6 = "INSERT INTO Table1(Field1) Values('" & ConvertQuotes(Text4) & "');"
    End Sub
    and enter "Kay" in Text4, I get:

    Code:
    INSERT INTO Table1(Field1) Values('kay');
    It appears to correctly formed..??
    Is this what the OP wants?

  2. #17
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Following Steve's scenario, I created a textbox and input value Kay" then in the Immediate window ran:

    ?Replace(Forms!form1.Text67,"""",Chr(96))

    That works.

    Whereas this fails:
    ?Replace("Kay"","""",Chr(96))

    But this works:
    x = "Kay" & """
    ?x
    Kay"
    ?Replace(x,"""",Chr(96))
    Kay`
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #18
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It seems to work in code. Since Kay was getting the error on the line

    db.Execute strsql, dbFailOnError

    I tried changing the button code to this:
    Code:
    Private Sub Command3_Click()
       Dim sSQL As String
       
       sSQL = "INSERT INTO Table1(Field1) Values('" & ConvertQuotes(Text4) & "');"
       MsgBox sSQL
    End Sub
    I entered "Kay", "Kay"" and ""Kay"" in the text box (not at the same time ).

    Every time I got back
    INSERT INTO Table1(Field1) Values('kay');

    Is this what you are looking for Kay?

  4. #19
    Kay is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2012
    Posts
    10
    it doesn't seem to have problem with single quote

  5. #20
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Right, Single Quote (apostrophe) not a problem in Replace.

    It is the Double Quote (quote mark) that is tricky.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #21
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    OK, lets take a step back.
    What are you trying to do? I looks like you are trying to generate an SQL string to insert values into SQL Server.

    If so, could you post the code to generate the SQL string?

  7. #22
    Kay is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2012
    Posts
    10
    Yes I was trying to generate SQL string. Here goes my code

    Dim strColumn1 As String
    Dim stra As String
    Dim strb As String
    Dim str1A As String
    Dim str2B As String
    stra = "'"
    strb = """"
    str1A = "`"
    str2B = "`"
    strColumn1 = [Forms]![Test]![Column1] & ""
    strColumn1 = Replace(strColumn1, stra, str1A)
    strColumn1 = Replace(strComment, strb, str2B)

    Dim db As Database
    Set db = CurrentDb()
    strsql = "INSERT INTO Table1(Field1,Field2,Field3,"
    strsql = strsql & "Field4,Field5,Field6) "
    strsql = strsql & "VALUES (""" & Now() & ""","""
    strsql = strsql & [Forms]![Test]![Column1] & ""","""
    strsql = strsql & [Forms]![Test]![Column2] & ""","""
    strsql = strsql & [Forms]![Test]![Column3] & ""","""
    strsql = strsql & [Forms]![Test]![Column4] & ""","""
    strsql = strsql & [Forms]![Test]![Column5] & """)"
    db.Execute strsql, dbFailOnError
    'DoCmd.OpenQuery "QryTest1A", acNormal, acEdit

    DoCmd.OpenQuery "QryTest2A", acNormal, acAdd

    End If


    Thansk

  8. #23
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    strsql = "INSERT INTO Table1(Field1,Field2,Field3,"
    strsql = strsql & "Field4,Field5,Field6) "
    Field1 is a date field
    Fields 2-6 are text fields?

  9. #24
    Kay is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2012
    Posts
    10
    Yep! That is correct.

  10. #25
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    In VBA, criteria for data/time field uses # as delimiter, not apostrophe or paired quotes.

    (#" & Now() & "#,"""

    You declare and manipulate variable strColumn1 but then don't use it in the SQL construct.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #26
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    This is an example of how I would generate the insert string:



    Code:
    Option Compare Database
    Option Explicit
    'The above lines should always be at the top of the code page
    
    
    'Private Sub Something_Click()
       Dim strSQL As String
       Dim db As DAO.Database
    
       Set db = CurrentDb()
    
       '   strSQL = "INSERT INTO Table1(Field1,Field2,Field3,Field4,Field5,Field6)"
       strSQL = strSQL & " VALUES (#" & Now() & "#, '"
       strSQL = strSQL & ConvertDbl(ConvertSngl([Forms]![Test]![Column1])) & "', '"
       strSQL = strSQL & ConvertDbl(ConvertSngl([Forms]![Test]![Column2])) & "', '"
       strSQL = strSQL & ConvertDbl(ConvertSngl([Forms]![Test]![Column3])) & "', '"
       strSQL = strSQL & ConvertDbl(ConvertSngl([Forms]![Test]![Column4])) & "', '"
       strSQL = strSQL & ConvertDbl(ConvertSngl([Forms]![Test]![Column5])) & "');"
    
       'if this is code behind a form, I would use the following
       '      strSQL = "INSERT INTO Table1(Field1,Field2,Field3,Field4,Field5,Field6)"
       '      strSQL = strSQL & " VALUES (#" & Now() & "#, '"
       '      strSQL = strSQL & ConvertDbl(ConvertSngl(Me.[Column1])) & "', '"
       '      strSQL = strSQL & ConvertDbl(ConvertSngl(Me.[Column2])) & "', '"
       '      strSQL = strSQL & ConvertDbl(ConvertSngl(Me.[Column3])) & "', '"
       '      strSQL = strSQL & ConvertDbl(ConvertSngl(Me.[Column4])) & "', '"
       '      strSQL = strSQL & ConvertDbl(ConvertSngl(Me.[Column5])) & "');"
    
    'for debugging purposes, comment out later
       Debug.Print strSQL
    
       db.Execute strSQL, dbFailOnError
    
       'DoCmd.OpenQuery "QryTest1A", acNormal, acEdit
       DoCmd.OpenQuery "QryTest2A", acNormal, acAdd
    
    End Sub

    'these two subs can be in a standard module or in the same code page as the code above
    Code:
    Function ConvertDbl(InputVal)
    'removes double quotes
       ConvertDbl = Replace(InputVal, """", "")
    End Function
    
    
    Function ConvertSngl(InputVal)
    'replaced a single apostrophe with double apostrophes
       ConvertSngl = Replace(InputVal, "'", "''")
    End Function

  12. #27
    Kay is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2012
    Posts
    10
    thanks a lot! I am getting complie error. will update you later.

  13. #28
    Kay is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2012
    Posts
    10
    Thank you !! It works now.

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Getting the Quotes right
    By aytee111 in forum Programming
    Replies: 2
    Last Post: 10-11-2012, 08:04 PM
  2. New Database on Customer Quotes
    By rkalapura in forum Access
    Replies: 2
    Last Post: 07-13-2012, 07:15 PM
  3. Replies: 3
    Last Post: 06-07-2012, 07:05 AM
  4. Database to produce quotes
    By rossp0203 in forum Database Design
    Replies: 2
    Last Post: 05-26-2011, 10:15 AM
  5. Remove Quotes within Data Values
    By kfschaefer in forum Programming
    Replies: 0
    Last Post: 02-26-2009, 01:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums