Avatar of thandel
thandel
 asked on

MS Access SQL Syntax

Hi I am trying to use SQL to update a table  (tEmailRx) with information from a form.  The table is laid out as:

CreateDate  - Date
MR - Number (long Int)
RxType - Text
Office - Text
Ws - Text

I am trying to use the following code:
    sSQL = "UPDATE tEmailRx tEmailRx.CreateDate = #" & Date & "#, tEmailRx.MR = " & Me.ID & ", tEmailRx.RxType='CL', tEmailRx.Office='" & sOffice & ",tEmailRx.Ws = Environ$(""computername"")"
    DoCmd.RunSQL sSQL

Open in new window

Which results in:

UPDATE tEmailRx tEmailRx.Date = #11-15-2017#, tEmailRx.MR = 126011, tEmailRx.RxType='CL', tEmailRx.Office='WB,tEmailRx.Ws = Environ$("computername")

But I am getting an error.  What am I doing wrong with my syntax?  SQL is a very weak link for me - Thanks!
Microsoft OfficeMicrosoft AccessSQL

Avatar of undefined
Last Comment
thandel

8/22/2022 - Mon
Norie

You need to use SET.
sSQL = " UPDATE tEmailRx SET tEmailRx.CreateDate = #" & Date & "#, tEmailRx.MR = " & Me.ID & ", tEmailRx.RxType='CL', tEmailRx.Office='" & sOffice & ",tEmailRx.Ws = '" & Environ$("computername") & "'"
    
DoCmd.RunSQL sSQL

Open in new window

thandel

ASKER
Thank you, so silly but there must be a small error as now the error is syntax error missing operator in query expression "WB,tEmail.Rx.Ws = 'Jupiter"
Norie

Strange, in the code you have tEmailRx.Ws but the error message says you have tEmail.Rx.Ws.

What happens if you use the field names without the table name in the SET part.
sSQL = " UPDATE tEmailRx SET CreateDate = #" & Date & "#, MR = " & Me.ID & ", RxType='CL', Office='" & sOffice & ",Ws = '" & Environ$("computername") & "'"
    
DoCmd.RunSQL sSQL

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
thandel

ASKER
I modified to this

sSQL = " UPDATE tEmailRx SET tEmailRx.CreateDate = #" & Date & "#, tEmailRx.MR = " & Me.ID & ", tEmailRx.RxType='CL', tEmailRx.Office='" & sOffice & "',tEmailRx.Ws = '" & Environ$("computername") & "'"code]

Open in new window

Not getting any error but nothing is happening to the table.
thandel

ASKER
I tried your code similar error
thandel

ASKER
oh wait slightly different.... "Wb,Ws = 'Jupiter".
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Norie

I think there's a missing ' in what I posted.

Try this, I think I fixed it.
sSQL = " UPDATE tEmailRx SET CreateDate = #" & Date & "#, MR = " & "Me.ID" & ", RxType='CL', Office='" & "sOffice" & "',Ws = '" & Environ$("computername") & "'"

    
DoCmd.RunSQL sSQL

Open in new window

thandel

ASKER
Well now its prompting me for a value of Me.Id
ASKER CERTIFIED SOLUTION
Norie

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
thandel

ASKER
Is Update correct I want to keep adding a row to the table with this... I can't remember if update is what I need to do that.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
thandel

ASKER
Ah bingo that worked thanks... but it does seem to only  update the first row... I would like to add to the table with each execution
Norie

UPDATE is for updating a table not adding to it, If you want to add to it then you need INSERT.

In fact if your UPDATE query had run successfully it might update every record in the table.
thandel

ASKER
Oh I see, to make a insert would I just change Update to Insert?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Norie

Not quite, the syntax is a bit different and there are different types of INSERT.

If you wanted to add a record with the values in your UPDATE query it might look something like this.
sSQL = " INSERT INTO tEmailRx (CreateDate, MR, RxType, Office, Ws)
sSQL = sSQL & " VALUES (#" & Date & "#, " & Me.ID & ",'CL', '" & sOffice & "','" & Environ$("computername") & "')"

Open in new window

thandel

ASKER
OK thanks but trying the code no errors but no updates to the table either
Norie

Can you post the rest of your code?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
thandel

ASKER
yes but not sure what part you need?  Its really press the button and the SQL runs.
thandel

ASKER
Private Sub RxOpEmail_Click()
    sSQL = "INSERT INTO tEmailRx (CreateDate, MR, RxType, Office, Ws)"
    sSQL = sSQL & " VALUES (#" & Date & "#, " & Me.ID & ",'CL', '" & sOffice & "','" & Environ$("computername") & "')"
    DoCmd.RunSQL sSQL
End Sub

Open in new window

thandel

ASKER
I'm sorry it is working!  I had the table open and assume it would refresh... even if I clicked on another row but it didn't.  When I closed and reopened the table it was visible.  I apologize for the over sight.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.