PDA

View Full Version : sql Update



Shas
09-18-2017, 02:06 AM
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

jonh
09-18-2017, 03:36 AM
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