Consulting

Results 1 to 2 of 2

Thread: sql Update

  1. #1
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    1
    Location

    sql Update

    Hello everyone! First time on this forum so be nice ;-) I'm not very god at Access but mostly I solved the problem myself, it's just a matter of time. But this time I have tried and tried with no luck. So therfore I'm trying this forum

    Here's the code...

    Private Sub cmdKopplaTillArbetsordernsFaktureringsadressEllerFT_Click()

    Dim dbs, SqlSkrivTillDatabas As String
    Dim stDocName, stLinkCriteria, Namn As String


    stDocName = "frmOrder_Registrering"
    DoCmd.OpenForm stDocName, , , stLinkCriteria



    txtId = Forms!frmOrder_KundRegisterSökAnnan!txtIDhjälp
    txtKundId = Forms!frmOrder_KundRegisterSökAnnan!txtKundId
    Namn = DLookup("[Namn]", "tblOrder_Kundregister", "[Kund-Id]='" & txtKundId & "'")

    MsgBox "txtId= " & txtId 'write 12174
    MsgBox "txtKundId= " & txtKundId 'write 15019837
    MsgBox "Namn= " & Namn 'write Andersson Lars-Ola

    'So far so god...

    'I like to bring the variable namn from my cutomer tabel and write this value in another tabel, but if I use letters it don't work
    'run-time error '3075' Syntax error (missing operator) in query expression 'Andersson Lars-Ola'
    'Körfel nr '3075' syntaxfel (operator saknas) i frågeuttrycket 'Andersson Lars-Ola' (swedish text)

    SqlSkrivTillDatabas = "Update [Indata arbetsorder] Set [Arbetsplatsens namn] = " & Namn & " WHERE ID = " & txtId & "."
    Set dbs = CurrentDb
    dbs.Execute SqlSkrivTillDatabas


    'If I Name Namn to a numeric string then it works

    'Namn = 123456
    'SqlSkrivTillDatabas = "Update [Indata arbetsorder] Set [Arbetsplatsens namn] = " & Namn & " WHERE ID = " & txtId & "."
    'Set dbs = CurrentDb
    'dbs.Execute SqlSkrivTillDatabas


    'Or if I called Namn Andersson Lars-Ola directly then it works to

    'Namn = "'Andersson Lars-Ola'"
    'SqlSkrivTillDatabas = "Update [Indata arbetsorder] Set [Arbetsplatsens namn] = " & Namn & " WHERE ID = " & txtId & "."
    'Set dbs = CurrentDb
    'dbs.Execute SqlSkrivTillDatabas

    End Sub

    Perhaps someone can se what I have done wrong...

    Thank you in advance

    Shas

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Hi.


    String datatypes reqire quotes around values.
    Since names can have apostrophies you either need to find and replace them or use double double quotes.


    I've never seen SQL terminated by a period. It's usually a ; but you don't need it anyway.

    Namn = "o'reilly"
    SqlSkrivTillDatabas = "Update [Indata arbetsorder] Set [Arbetsplatsens namn] = """ & Namn & """ WHERE ID = " & txtId

Posting Permissions

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