Results 1 to 22 of 22

Thread: Should be easy, but I'm missing it

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Calgary Alberta
    Posts
    359

    Post

    Can you post the entire line of code you use to do this? I'm not sure exactly what you are using (recordset etc) although I think I might know what's wrong

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    Code:
                 
    sSQL = "INSERT INTO [MainRep] ([Service Description]) " _
    & "VALUES (" & """" & "CAN'T" & """" & ");"
    cnn.Execute sSQL
    Thanks,
    Wade

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Change it to 'CAN''T'

    That's two single quotes between the N and the T.

    ------------------
    Marty
    Why is it called lipstick if you can still move your lips?

  4. #4
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    What if CAN'T is in the middle of a long string of comments from a user?

    Thanks,
    Wade


    [This message has been edited by WadeD (edited 01-26-2000).]

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Use the VB6 Replace function, or write a routine that parses the string replacing all single quotes with pairs of single quotes.

    ------------------
    Marty
    Why is it called lipstick if you can still move your lips?

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    I'm writing back a number of records in a loop already. If I have to check each field that could have an apostrophe, it'll really start to drag. Do you know why VB is choking on the quotation marks when I can do it correctly directly from Access using the same SQL?

    Thanks,
    Wade

  7. #7
    Member
    Join Date
    Jan 1999
    Location
    Garden Grove, CA, Orange
    Posts
    55

    Post

    Code

    Public Function RemQMark(strTest As String) As String
    'Test for illegal SQL characters (' or ") in company name
    Dim intI As Integer, intL As Integer, intE As Integer
    Dim strMsg As String

    intL = Len(strTest)
    intI = 0: RemQMark = ""

    Do While intI <= intL
    intI = intI + 1
    If Mid(strTest, intI, 1) = """" Then
    strTest = Left(strTest, intI) & """" & Right(strTest, intL - intI)
    ElseIf Mid(strTest, intI, 1) = "'" Then
    strTest = Left(strTest, intI) & "'" & Right(strTest, intL - intI)

    End If
    intE = Len(strTest)
    If intE > intL Then
    intL = intL + 1
    intI = intI + 1
    End If
    Loop

    RemQMark = strTest

    End Function
    ============================

    Usage

    sSQL = "INSERT INTO [MainRep] ([Service Description]) VALUES ('" & RemQMark(YourVariable) & "')"
    cnn.Execute sSQL


    Joon

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Calgary Alberta
    Posts
    359

    Post

    try this:

    strString = "Can't"

    sSQL = "INSERT INTO [MainRep] ([Service Description]) " _
    & "VALUES ('" & strString & "');"

    cnn.Execute sSQL

    That's usually what I do when I'm messing with things like this.. which is most of the time.

    NOTE: that's ( SINGLE ' followed by DOUBLE " then DOUBLE ", SINGLE ' and )

    [This message has been edited by netSurfer (edited 01-26-2000).]

  9. #9
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    I appreciate all of the responses, but I don't want to add overhead by checking the string on each textbox that could have an apostrophe.
    Netsurfer,
    Code:
    strString = "Can't print on all computers"
    sSQL = "INSERT INTO [MainRep] ([Service Description]) " _
    & "VALUES (" & """" & strString & """" & ");"
    txtTestSQL = sSQL
    If I copy the text from txtTestSQL to Access, the SQL works. I know in DAO, you could pass quotations. RDO or ADO should allow the same, shouldn't they?

    Thanks again,
    Wade

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Calgary Alberta
    Posts
    359

    Post

    Did you try my last example? I use it and know that it will post ' inside strings. I use DAO though but I don't know if it makes a difference.

  11. #11
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    I tried it, but it gives me a not enough parameters error. The example 2 above uses what you mentioned. I added """" around each side of the string to make it pass quotations (so it passes "can't print on all computers"). Any other ideas?

    Thanks again,
    Wade

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Calgary Alberta
    Posts
    359

    Post

    I'm sorry , I just can't see anything wrong. It's probably something simple that we'll kick ourselves for but I just can't see it. Good luck

  13. #13
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    Thanks for trying. I'll let you know if I find it.

    Wade

  14. #14
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    If "can't" is causing so many problems, why not just send "cannot"???

  15. #15
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    Chris,

    I appreciate the reply, but can't was only an example of text with an apostrophe that a user might enter.

    Aaron gave me an example of Replace which works.

    Thanks,
    Wade

  16. #16
    Addicted Member
    Join Date
    Oct 1999
    Location
    Brossard, Québec, Canada
    Posts
    241

    Post

    Hi WadeD,

    Hope you are still looking at this topic because I tried exacly what you want to know several times... and found out a way to work around it...

    instead of using a SQL statement, Just use a record set with the AddNew method...

    The thing is that in SQL, string values are quoted between apostophes as so: 'YOURSTRING'
    now if you put another apostrophe in that string value, it's like you are saying that the string end right there.... (Still folowing me? )

    Using recordsets is not really slower than SQL statements (Haven't actualy timed it but never had problems)

    so you can try something like this:

    Dim DB As Database
    Dim rst As RecordSet

    Set DB = OpenDatabase(PATH_OF_YOUR_DATABASE)
    Set rst = DB.OpenRecordSet("SELECT * FROM MainRep")

    rst.AddNew
    rst![Service Description] = "Can't"
    rst.update

    DB.Close
    rst.Close
    Set DB = Nothing
    Set rst = Nothing


    Now using that in a loop won't be slower than using a SQL statement in a loop..

    Hope it helps!

  17. #17
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    I'm sending an INSERT SQL statement to add a record:
    Code:
    'Error --> Too Few Parameters:
    INSERT INTO [MainRep] ([Service Description]) VALUES ("CAN'T");
    I used the quotations around CAN'T because of the apostrophe, and this works great in Access (I copied and pasted to make sure), but I get an error in VB. Where am I going wrong??

    Thanks in advance,
    Wade

    [This message has been edited by WadeD (edited 01-26-2000).]

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Calgary Alberta
    Posts
    359

    Post

    It thinks you're trying to insert into 2 fields 1 variable. What is can't?

  19. #19
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    The first name shown is the table name, not a field. This works directly from Access, but not in vb.

    I was using a variable but it was choking on the word can't so I put can't in there while troubleshooting.

    Thanks,
    Wade

    [This message has been edited by WadeD (edited 01-26-2000).]

  20. #20
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    David,

    I know you can use AddNew with DAO, which I've done before, but I've migrated to ADO which I don't believe supports the AddNew method. When I've tried it previously, I get an error message. Thanks for the reply.

    Wade

  21. #21
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Post

    An ADO Recordset do have the AddNew method.

    ------------------
    Joacim Andersson
    [email protected]
    [email protected]
    www.YellowBlazer.com



  22. #22
    Guest

    Post

    Uhhh guys.. if your saying what I think your saying, your haveing a hard time with the Quotes and the Apostrophe in the word "can't" ???

    Why not use a CHR ?

    Example:
    "can" + chr(39) + "t"

    Or am I missing the point here ?


    ------------------
    John T. Mieske
    Star Trek LCARS programmer

    Knight Vision Enterprises
    [email protected]


    [This message has been edited by Knight_Vision (edited 01-28-2000).]

Posting Permissions

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



Click Here to Expand Forum to Full Width