Results 1 to 37 of 37

Thread: [RESOLVED] Update Syntax in Access not the same as in vb 6 code why?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] Update Syntax in Access not the same as in vb 6 code why?

    hey
    i have a code that update a certain field if it contains this mark [']
    when i run it in the access table it works fine
    when i run it in the vb 6 code i get a error why?
    this is the code
    in access
    Code:
    UPDATE Customers SET FullName = Replace(FullName,"'","")
    in vb 6
    Code:
    CN.Execute "UPDATE Customers SET FullName = Replace(FullName," ' ","")"
    tnx for any help
    salsa

  2. #2
    Fanatic Member
    Join Date
    Jan 2015
    Posts
    643

    Re: Update Syntax in Access not the same as in vb 6 code why?

    > CN.Execute "UPDATE Customers SET FullName = Replace(FullName," ' ","")"

    You must double the double quote

    CN.Execute "UPDATE Customers SET FullName = Replace(FullName,""'"","""")"

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Name:  untitled.JPG
Views: 314
Size:  12.0 KB

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Jet SQL Expression Service VBA does not include a Replace$() function.

    MS Access uses a private API embedded in the Jet 4.0 and ACE engines to inject its own VBA engine as a substitute expression processor. This is also how MS Access allows "user defined functions" written in the Access VBA macro language to be called within Jet/ACE SQL expressions.

    Some of this is historical: Originally MS Access used another language ("Access Basic") and needed a way to inject it. Jet SQL VBA goes back to the VB5 era which has no Replace$() function.

    When VB6 expanded the range of intrinsic functions in its VBA namespace Microsoft didn't expend the Jet SQL Expression Service's VBA namespace. This hasn't happened in the later (renamed) ACE engines either.


    It can get confusing because "VBA" means different things in different contexts. In VB6 it is just the namespace of a default global object, in Jet SQL it refers to an expression syntax wrapped around its own global object, and in MS Access it is a macro language wrapped around a global object and a procedural processor.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Update Syntax in Access not the same as in vb 6 code why?

    The article How to configure Jet 4.0 to prevent unsafe functions from running in Access 2003 contains a list of the functions accepted by the Jet SQL Expression parser.

  6. #6

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    so you are saying sir it is not possible to run this code from vb ?

  7. #7
    gibra
    Guest

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by salsa31 View Post
    so you are saying sir it is not possible to run this code from vb ?
    But, why you want to replace in your table all single quote with double quote ?
    Isn't a good idea (by me is totally wrong).

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by gibra View Post
    But, why you want to replace in your table all single quote with double quote ?
    Isn't a good idea (by me is totally wrong).
    i dont want to replace it with double quote
    i just want to check if there is a quote then remove it thats all

    this is how i run it in access
    Code:
    UPDATE Customers SET FullName = Replace(FullName,"'","")
    i want to do the same thing with vb 6

  9. #9
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Sorry. I misunderstood the question. Deleting my answer.

    Okay. The best way I can think of is two step.
    1) Use your existing method inside access to clean that field of ")" in the FullName field.
    2) Create a filter in the tool that adds ")" to the text that removes it before the text is added to the database.
    Last edited by Gruff; Apr 11th, 2016 at 10:37 AM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  10. #10

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by Gruff View Post
    Sorry. I misunderstood the question. Deleting my answer.

    Okay. The best way I can think of is two step.
    1) Use your existing method inside access to clean that field of ")" in the FullName field.
    2) Create a filter in the tool that adds ")" to the text that removes it before the text is added to the database.
    can you give me an example please?

  11. #11
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Update Syntax in Access not the same as in vb 6 code why?

    You already know how to do #1
    Regarding #2: Since I do not know how you input your text into the field in the first place I cannot say
    Last edited by Gruff; Apr 11th, 2016 at 11:33 AM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Og for the love of ENIAC....
    CN.Execute "UPDATE Customers SET FullName = Replace(FullName,"","", """")"

    first a case of not enough double quoting... then a case of too much in the wrong spot ....
    Replace takes three paramerters, the haysatck, the needle and what to replace it with... this will look in FullName find , in the text and replace it with an empty string...
    We're not building rockets here folks...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Update Syntax in Access not the same as in vb 6 code why?

    So your saying Dil is wrong in post #4 TG?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  14. #14

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by techgnome View Post
    Og for the love of ENIAC....
    CN.Execute "UPDATE Customers SET FullName = Replace(FullName,"","", """")"

    first a case of not enough double quoting... then a case of too much in the wrong spot ....
    Replace takes three paramerters, the haysatck, the needle and what to replace it with... this will look in FullName find , in the text and replace it with an empty string...
    We're not building rockets here folks...

    -tg
    well sir that didnt help
    Code:
    CN.Execute "UPDATE Customers SET FullName = Replace(FullName,"","", """")"
    i still get the same error

    Name:  untitled (1).JPG
Views: 285
Size:  12.0 KB

  15. #15
    gibra
    Guest

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by salsa31 View Post
    i dont want to replace it with double quote
    I'm sorry, I misread.

  16. #16

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by gibra View Post
    I'm sorry, I misread.
    thats ok
    i guess there isnt a way to make this update through vb 6 only through access it self

  17. #17
    gibra
    Guest

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by salsa31 View Post
    thats ok
    i guess there isnt a way to make this update through vb 6 only through access it self
    However, Replace doesn't work outside of MS Access environment.
    Even disabling the SandBoxMode registry key, at least to me it never worked.
    So you'd better open a recordset to change values.

  18. #18

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?


  19. #19
    gibra
    Guest

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by salsa31 View Post
    But it has nothing to do with your problem, that is instead related to SQL in Access database.

  20. #20

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by gibra View Post
    But it has nothing to do with your problem, that is instead related to SQL in Access database.
    LOL

  21. #21
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Left(...), Mid(...) and Instr(...) are supported though, so an (although horrible looking)
    workaround would be possible directly in "plain JET-SQL"...

    Olaf

  22. #22
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Update Syntax in Access not the same as in vb 6 code why?

    the following seems to work(at least on my system):
    Code:
    Private Sub Command1_Click()
       Dim Conn As New ADODB.Connection
       Conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\IkkeEnGij\Documents\TheDatabase.accdb"
       Conn.Open
       Conn.Execute "UPDATE Table1 SET Table1.TheName = Replace(Table1.TheName,chr(39),"""")"
       Conn.Close
    End Sub
    Last edited by IkkeEnGij; Apr 11th, 2016 at 04:15 PM. Reason: writeing error
    do not put off till tomorrow what you can put off forever

  23. #23

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by Schmidt View Post
    Left(...), Mid(...) and Instr(...) are supported though, so an (although horrible looking)
    workaround would be possible directly in "plain JET-SQL"...

    Olaf
    can you give me a example please what do you mean?

  24. #24
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Update Syntax in Access not the same as in vb 6 code why?

    does post#22 not work ?
    do not put off till tomorrow what you can put off forever

  25. #25

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by IkkeEnGij View Post
    does post#22 not work ?
    no my friend still same error
    Code:
    Set CN = New ADODB.Connection
    StrSql = "Provider=Microsoft.Jet.OLEDB.4.0;"
    StrSql = StrSql & "Data Source=" & App.Path & "\HairDesign.mdb;"
    StrSql = StrSql & "Jet OLEDB:Database Password=035924794"
    CN.ConnectionString = StrSql
    CN.Open
    CN.Execute "UPDATE Customers SET Customers.FullName = Replace(Customers.FullName,chr(39),"""")"
    CN.Close

  26. #26
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Update Syntax in Access not the same as in vb 6 code why?

    on my system it works perfectly with ADO and DAO and VB6 sp6
    aceoledb/acedao

    Try with DAO
    if it does not work, post an example database (unprotected)
    do not put off till tomorrow what you can put off forever

  27. #27
    gibra
    Guest

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by IkkeEnGij View Post
    the following seems to work(at least on my system):
    Code:
    Private Sub Command1_Click()
       Dim Conn As New ADODB.Connection
       Conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\IkkeEnGij\Documents\TheDatabase.accdb"
       Conn.Open
       Conn.Execute "UPDATE Table1 SET Table1.TheName = Replace(Table1.TheName,chr(39),"""")"
       Conn.Close
    End Sub
    This isn't JET provider, this is ACE provider.
    Using JET doesn't work.

  28. #28

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by gibra View Post
    This isn't JET provider, this is ACE provider.
    Using JET doesn't work.
    then how should i do it?

  29. #29
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by salsa31 View Post
    then how should i do it?
    As said, the SQL isn't really beautiful, but (for single Replacements) it should work as shown below:

    Code:
    Option Explicit 'Into a VB-Form in an empty Project
    
    Const JetPrefix = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    
    Private Cnn As Object
    
    Private Sub Form_Load()
      Dim DBName$: DBName = Environ("temp") & "\test1.mdb"
      If CreateObject("Scripting.FileSystemObject").FileExists(DBName) Then Kill DBName
     
      Set Cnn = CreateObject("ADOX.Catalog").Create(JetPrefix & DBName)
          Cnn.Execute "Create Table T1(ID Identity Primary Key, Txt Text(255))"
          Cnn.Execute "Insert Into T1(Txt) Values('O''Brien')"
      
      'show the original Value which sits in the Field
      Debug.Print GetRs("Select Txt From T1")(0)
      
      'do a (single) Replacement on all fields which contain an apostrophe
      Cnn.Execute "Update T1 Set Txt = Left(Txt,Instr(Txt,'''')-1) & Mid(Txt,Instr(Txt,'''')+1) Where Instr(Txt,'''')>0"
      
      'let's see if that worked...
      Debug.Print GetRs("Select Txt From T1")(0)
    End Sub
     
    Function GetRs(SQL As String) As Object
      Set GetRs = CreateObject("ADODB.Recordset")
          GetRs.CursorLocation = 3  'adUseClient
          GetRs.Open SQL, Cnn, 3, 3 'adOpenStatic, adLockOptimistic
    End Function
    Olaf

  30. #30
    gibra
    Guest

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by salsa31 View Post
    then how should i do it?
    As already worte, use a ADODB.Recordset , loop records, and replace single quote. Ex:

    Code:
    rs.Open "SELECT Fullname FROM Customers .....", ...........
    Do While Not rs.Eof
        rs!Fullname = Replace$(rs!Fullname, "'", vbNulString)
        rs.MoveNext
    Loop

  31. #31
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by gibra View Post
    This isn't JET provider, this is ACE provider.
    Using JET doesn't work.
    afaik ACE should work with .mdb to
    there are a lot of possibilitys to try out
    among others using a recordset (as you suggested)
    if it does not work with ADO try with DAO
    if it does not work with JET, try ACE
    so i see at least 8 different methods to try

    sql with ADO/DAO JET/ACE
    recordset with ADO/DAO JET/ACE

    but of cource if one asks a question about databases
    including an example database would be the smart thing to do
    do not put off till tomorrow what you can put off forever

  32. #32
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by IkkeEnGij View Post
    on my system it works perfectly with ADO and DAO and VB6 sp6
    aceoledb/acedao
    And i did try it out
    but is was with an .accdb
    just saying so there is no misunderstanding
    do not put off till tomorrow what you can put off forever

  33. #33
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Perhaps the question that should be asked is "WHY?" why are you manipulating the data like this? What's the purpose of dropping the ' from the data? Other than to piss off a bunch of Irish?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  34. #34
    gibra
    Guest

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by techgnome View Post
    Perhaps the question that should be asked is "WHY?" why are you manipulating the data like this? What's the purpose of dropping the ' from the data? Other than to piss off a bunch of Irish?
    -tg
    I agree 100%.
    Why?

  35. #35

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Quote Originally Posted by gibra View Post
    As already worte, use a ADODB.Recordset , loop records, and replace single quote. Ex:

    Code:
    rs.Open "SELECT Fullname FROM Customers .....", ...........
    Do While Not rs.Eof
        rs!Fullname = Replace$(rs!Fullname, "'", vbNulString)
        rs.MoveNext
    Loop
    Code:
            Dim Rs As New ADODB.Recordset
            Rs.Open "SELECT Fullname FROM Customers", CN
            Do While Not Rs.EOF
                Rs!FullName = Replace$(Rs!FullName, "'", vbNullString)
            Rs.MoveNext
            Loop
            Rs.Close
    Name:  99.JPG
Views: 222
Size:  14.0 KB

  36. #36

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: Update Syntax in Access not the same as in vb 6 code why?

    Tg ANd Gibra
    this is a old database AMIGOS

  37. #37
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Update Syntax in Access not the same as in vb 6 code why?

    i just tested with the .mdb Olaf put here
    works perfectly with ACEDAO
    Code:
    Private Sub Command1_Click()
        Dim db As DAO.Database
        Set db = OpenDatabase("C:\0test\test1.mdb")
        db.Execute "UPDATE T1 SET T1.Txt = Replace(T1.Txt,""'"","""")"
        db.Close
        Set db = Nothing
    End Sub
    works perfectly with ACEOLEDB
    Code:
    Private Sub Command1_Click()
       Dim Conn As New ADODB.Connection
       Conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\0test\test1.mdb"
       Conn.Open
       Conn.Execute "UPDATE T1 SET T1.Txt = Replace(T1.Txt,""'"","""")"
       Conn.Close
    End Sub
    do not put off till tomorrow what you can put off forever

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