We help IT Professionals succeed at work.

Afterupdate cbo, Insert and text

pdvsa
pdvsa asked
on
Experts,

I have an error in an afterupdate query on a cbo to populate tblProjects.Buy_CP to the value on Me.cboBuy_CP.  
I think my problem is that Buy_CP is text and the single quote double quote is not correct in the below.  


    CurrentDb.Execute "UPDATE tblProjects SET Buy_CP='" & Me.cboBuy_CP.Column(1) & "' WHERE ID=" & Me.ID

Col 0 is the ID and Col 1 is the text.

Thank you...
Comment
Watch Question

John TsioumprisSoftware & Systems Engineer
BRONZE EXPERT
Distinguished Expert 2019
Commented:

If the value is text you wrap it around single quotes.

 I think the query in structure is correct 

You need that there is a data type match between 

Buy_CP < --> Me.cboBuy_CP.Column(1)

&

 ID <---> Me.ID

Probably

Buy_CP is numeric and   cboBuy_CP.Column(1) is text ...so your update won't work unless you modify   Buy_CP to be text.


SILVER EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:

Your code seems correct, so it is probably the values that cause an error.

Try:


Dim Sql As String


Sql = "UPDATE tblProjects SET Buy_CP='" & Me.cboBuy_CP.Column(1) & "' WHERE ID=" & Me.ID & ""
Debug.Print Sql
CurrentDb.Execute Sql


Then study the printed SQL and try to run that manually (paste it to the SQL view of a new query).

SILVER EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:

Sorry for the garbled code. It's the buggy editor.


Dim Sql As String

Sql = "UPDATE tblProjects SET Buy_CP='" & Me.cboBuy_CP.Column(1) & "' WHERE ID=" & Me.ID & ""
Debug.Print Sql
CurrentDb.Execute Sql

ste5anSenior Developer
BRONZE EXPERT

Commented:
It's always a good idea to create an extra SQL variable, but when you think the error comes from your UPDATE, then you need to look at your data. Cause your way of SQL string concatenation is vulnerable for SQL injection, thus it may fail due to single quotes in your data. Also as your ID seems to be numeric, enforcing this by using CLng() can help you to spot errors in data. And you need error handling with On [Local] Error GoTo LocalError (see attached file).

Dim Sql As String

Sql = "UPDATE tblProjects " & _
  "SET Buy_CP = '" & Replace(Me.cboBuy_CP.Column(1), "'", "''") & "' WHERE ID = " & CLng(Me.ID) & ";"
Debug.Print Sql
CurrentDb.Execute Sql

Open in new window

As ready to use-solution:

a) using a function for escaping quotes or NULL:

Dim Sql As String

Sql = "UPDATE tblProjects " & _
  "SET Buy_CP = " & SqlQuoteNull(Me.cboBuy_CP.Column(1)) & " WHERE ID = " & CLng(Me.ID) & ";"
Debug.Print Sql
CurrentDb.Execute Sql

Open in new window

b) with some error handling

Private Sub YourMethod()

  Const MSG_UPDATE_ERROR As String = "Error during update."
  Const SQL_UPDATE As String = "UPDATE tblProjects SET Buy_CP = {0} WHERE ID = {1};"

  If Not SqlExecuteFmt(SQL_UPDATE, SqlQuoteNull(Me.cboBuy_CP.Column(1)), CLng(Me.ID)) Then
    MsgBox MSG_UPDATE_ERROR, vbWarning
  End If

End Function

Open in new window

sql.vba
pdvsaProject finance

Author

Commented:

Thank you guys.... appreciate it much. I have not been able to get away to test  yet... shouod be able to tomorrow at latest. Enjoy the weekend!

pdvsaProject finance

Author

Commented:

my code was correct...my issue was something else.  Thank you...appreciate the advice and comments. 

ste5anSenior Developer
BRONZE EXPERT

Commented:
In this case.. not really. Then there was "with some error handling" missing to catch the error where it really happened.