Solved CurrentDB.Execute Syntax (2 Viewers)

evictme

Registered User.
Local time
Yesterday, 20:18
Joined
May 18, 2011
Messages
168
Hey all,

Im trying to include the following code in a _Onclick event

SQL:
CurrentDb.Execute ("UPDATE Client SET Client.CaseMgrFax = " & Me.Fax_Number & " WHERE Client.CCID = '" & Me.ID & "'")

The event is for Form that includes information that should be updated in another table whenever there is an update/change. Not sure if the .Execute is the best way to do this...I was trying to use a rs.update but that does not do ALL the records with the matching ID (See below). Any advice or suggestions for best doing this would be appreciated! Thank you.

SQL:
 DoCmd.SetWarnings False
          
           Set rs = CurrentDb.OpenRecordset("SELECT * FROM Client")
            
            rs.FindFirst ("CCID = " & Me.ID)
              If Not rs.NoMatch Then
                rs.Edit
                rs.[CaseManager] = Me.Case_Coordinator
                rs.[CCID] = Me.ID
                rs.[ServiceArea] = Me.Region
                rs.[CMPhone] = Me.Phone_Number
                rs.[CaseMgrFax] = Me.Fax_Number
                rs.[CaseCoordEmail] = Me.Email
                rs.Update
                rs.Close
              
              End If
              
            Set rs = Nothing
            
       DoCmd.SetWarnings True
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:18
Joined
Mar 14, 2017
Messages
8,777
I think Execute seems like a fine way to do this. Are you having any particular problem with it? Is CCID column numeric? If so you need to drop those single quotes surrounding it.

You can leave the parenthesis off of Execute
 

evictme

Registered User.
Local time
Yesterday, 20:18
Joined
May 18, 2011
Messages
168
I think Execute seems like a fine way to do this. Are you having any particular problem with it? Is CCID column numeric? If so you need to drop those single quotes surrounding it.

You can leave the parenthesis off of Execute
🤦‍♂️ the whole reason I came here...Yes. The CCID is numeric. It was erroring because of the extra single quotes...Thank you Isaac!
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:18
Joined
Mar 14, 2017
Messages
8,777
🤦‍♂️ the whole reason I came here...Yes. The CCID is numeric. It was erroring because of the extra single quotes...Thank you Isaac!
Sounds like you got it working - glad to hear! 🍺
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 20:18
Joined
Feb 28, 2001
Messages
27,167
.I was trying to use a rs.update but that does not do ALL the records with the matching ID

The reason is that when you do something via SQL and an UPDATE query, you do all matching members of the set at once (in effect if not in fact). When you do a recordset you are attempting to do one at a time. Your code MIGHT have worked the way you wanted if you had built a loop into the logic of using the recordset. BUT the SQL is faster and (to my way of thinking) easier.
 

evictme

Registered User.
Local time
Yesterday, 20:18
Joined
May 18, 2011
Messages
168
I think Execute seems like a fine way to do this. Are you having any particular problem with it? Is CCID column numeric? If so you need to drop those single quotes surrounding it.

You can leave the parenthesis off of Execute
Im trying to do multiple fields but I keep getting a Syntax error. Am I missing an operator/single quotes?

SQL:
CurrentDb.Execute "UPDATE Client SET Client.CaseManger = " & Me.Case_Coordinator & " and Client.CCID = " & Me.ID & " and Client.CMPhone = " & Me.Phone_Number & " and Client.ServiceArea = " & Me.Region & " and  Client.CaseMgrFax = " & Me.Fax_Number & " and  Client.CaseCoordEmail = " & Me.Email & " WHERE Client.CCID = " & Me.ID & ""
 

evictme

Registered User.
Local time
Yesterday, 20:18
Joined
May 18, 2011
Messages
168
The reason is that when you do something via SQL and an UPDATE query, you do all matching members of the set at once (in effect if not in fact). When you do a recordset you are attempting to do one at a time. Your code MIGHT have worked the way you wanted if you had built a loop into the logic of using the recordset. BUT the SQL is faster and (to my way of thinking) easier.
Yea, I realized the loop part just after I made the execute code. I think the execute might work better for me, if I can figure out the syntax
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:18
Joined
Mar 14, 2017
Messages
8,777
What column type is Client.CaseManger? Is it spelled right?

Remember, you DO need single quotes for all the text fields. Do those form control values (like Me.Region, Me.Fax_Number) already have the single quotes? (probably not)
 

cheekybuddha

AWF VIP
Local time
Today, 02:18
Joined
Jul 21, 2014
Messages
2,274
The SET arguments should be separated by commas. Also, you can't qualify the fields in an UPDATE statement:
Code:
CurrentDb.Execute "UPDATE Client " & _
                  "SET CaseManger = '" & Me.Case_Coordinator & "', " & _
                  "    CMPhone = '" & Me.Phone_Number & "', " & _
                  "    ServiceArea = '" & Me.Region & "', " & _
                  "    CaseMgrFax = '" & Me.Fax_Number & "', " & _
                  "    CaseCoordEmail = '" & Me.Email & "' " & _
                  "WHERE CCID = " & Me.ID & ";", _
                  dbFailOnError
I've assumed all the fields are string fields. If not, remove the single quotes from around the appropriate value.

The dbFailOnError argument will give you a trappable error if anything goes wrong.

hth,

d
 

evictme

Registered User.
Local time
Yesterday, 20:18
Joined
May 18, 2011
Messages
168
What column type is Client.CaseManger? Is it spelled right?

Remember, you DO need single quotes for all the text fields. Do those form control values (like Me.Region, Me.Fax_Number) already have the single quotes? (probably not)
I corrected the misspelling. Working on the single quotes for the fields...
 

evictme

Registered User.
Local time
Yesterday, 20:18
Joined
May 18, 2011
Messages
168
The SET arguments should be separated by commas. Also, you can't qualify the fields in an UPDATE statement:
Code:
CurrentDb.Execute "UPDATE Client " & _
                  "SET CaseManger = '" & Me.Case_Coordinator & "', " & _
                  "    CMPhone = '" & Me.Phone_Number & "', " & _
                  "    ServiceArea = '" & Me.Region & "', " & _
                  "    CaseMgrFax = '" & Me.Fax_Number & "', " & _
                  "    CaseCoordEmail = '" & Me.Email & "' " & _
                  "WHERE CCID = " & Me.ID & ";", _
                  dbFailOnError
I've assumed all the fields are string fields. If not, remove the single quotes from around the appropriate value.

The dbFailOnError argument will give you a trappable error if anything goes wrong.

hth,

d
Thank you. This works perfect (after the spelling correction). Clearly needed some outside perspective, was working on this all morning
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:18
Joined
Mar 14, 2017
Messages
8,777
Thank you. This works perfect (after the spelling correction). Clearly needed some outside perspective, was working on this all morning
Happens to everyone (y)
 

cheekybuddha

AWF VIP
Local time
Today, 02:18
Joined
Jul 21, 2014
Messages
2,274
Thank you. This works perfect (after the spelling correction).
Glad you got it working! I'm pleased we all could help. (y)

It's often useful to build the SQL into a string variable first, which will give you the opportunity to inspect it if necessary.
eg
Code:
Dim strSQL As String

strSQL = "UPDATE Client " & _
         "SET CaseManager = '" & Me.Case_Coordinator & "', " & _
         "    CMPhone = '" & Me.Phone_Number & "', " & _
         "    ServiceArea = '" & Me.Region & "', " & _
         "    CaseMgrFax = '" & Me.Fax_Number & "', " & _
         "    CaseCoordEmail = '" & Me.Email & "' " & _
         "WHERE CCID = " & Me.ID & ";"
       
Debug.Print strSQL  ' Remove or comment out after development when you are happy with the SQL

CurrentDb.Execute strSQL, dbFailOnError

hth,

d
 

Users who are viewing this thread

Top Bottom