473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Docmd Update where criteria is Numeric

Hi,
I'm new to access. I'm trying to update a tables. Below is the VBA Code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdUpdateSales_Click()
  2.  
  3.  DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed'" & _
  4.  "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID.Value & ""
  5.  
  6. End Sub
But nothing is working. Can somebody help me to write the code.

Please note that there are two tables. One is CUSTOMERS and the other is SALES. When I click on the button, I want the field CustomersStatus be updated to "Closed" where CustomersStatus = 'Invoiced' the table Customers and where CustomerID in table Customers = the field Invoice_ID in table Sales. I know that this must be easy. Its just that I'm new and don't know how to proceed.
May 30 '13 #1

✓ answered by Seth Schrock

Please use code tags when posting code (the [CODE/] button).

One mistake that I can see is that once you concatenate the first two parts together you end up with
Expand|Select|Wrap|Line Numbers
  1. ...'Closed'Where...
You need a space in there for it to work. I tend to put the space at the end of the line, so I would do
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed' " & _
  2. "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID.Value & ""
Notice the space between the single quote and the double quote at the end of the first line.

Also, is Me.Invoice_ID bound to a text field or a number field? You have an empty set of double quotes at the end which makes me wonder if you meant to include a single quote between them. If it is a number field, then you don't need the ending single quote (which means you can also remove the double quotes that you added at the end, although this won't affect the execution of the code). If it is a text field, then you need to add a single quote after the equals sign and in the middle of the two double quotes at the end. Here is an example for a number field:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed'" & _
  2. "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID.Value
and for a text field
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed'" & _
  2. "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = '" & Me.Invoice_ID.Value & "'"

12 2969
Seth Schrock
2,965 Expert 2GB
Please use code tags when posting code (the [CODE/] button).

One mistake that I can see is that once you concatenate the first two parts together you end up with
Expand|Select|Wrap|Line Numbers
  1. ...'Closed'Where...
You need a space in there for it to work. I tend to put the space at the end of the line, so I would do
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed' " & _
  2. "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID.Value & ""
Notice the space between the single quote and the double quote at the end of the first line.

Also, is Me.Invoice_ID bound to a text field or a number field? You have an empty set of double quotes at the end which makes me wonder if you meant to include a single quote between them. If it is a number field, then you don't need the ending single quote (which means you can also remove the double quotes that you added at the end, although this won't affect the execution of the code). If it is a text field, then you need to add a single quote after the equals sign and in the middle of the two double quotes at the end. Here is an example for a number field:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed'" & _
  2. "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID.Value
and for a text field
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed'" & _
  2. "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = '" & Me.Invoice_ID.Value & "'"
May 30 '13 #2
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed'" & _
  2.     "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID
I got this error " Compile error Method or Data Member not found"

The CustomerID is an AutoNumber Field found in the Customer Table and the Invoice_Id is a number Field found in the Sales Table. But it didn't work even without the quote.
May 30 '13 #3
Seth Schrock
2,965 Expert 2GB
Please use code tags when posting code. This is very important!

Do you have Option Explicit set? It would be at the very top of your module, the second line. I also noticed that you still don't have the space that you need to separate the WHERE clause from the code before it.

There is another way to run queries from code that I like to use as it tells you if there is a problem with the SQL code itself. Try copying and pasting the following and see if it works:
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim strQuery As String
  3.  
  4. Set db = CurrentDb
  5. strQuery = "UPDATE Customers SET Customers.CustomersStatus = 'Closed' " & _
  6. "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID
  7.  
  8. DoCmd.SetWarnings False
  9. db.Execute strQuery, dbFailOnError
  10. DoCmd.SetWarnings True
May 30 '13 #4
zmbd
5,501 Expert Mod 4TB
jazee007:

Hello and Welcome!

First:
Please start off with following the first few parts in the following: > Before Posting (VBA or SQL) Code Seth has started you off on this path; however, the link provides a few more pointers.

Next:
You've fallen into what I call the MicroSoft programmer's trap. What this is, is creating the parameter string directly in the command (as you've done). This makes it very difficult to troubleshoot when the command fails.

To "fix" this let's modify your First code block a tad:

Expand|Select|Wrap|Line Numbers
  1. 'These are only at the top of each module - ONCE
  2. Option Compare Database
  3. Option Explicit
  4. '
  5.  
  6. Private Sub cmdUpdateSales_Click()
  7.     '
  8.     'a string for your eventual command.
  9.     Dim strSQL As String
  10.     '
  11.     'setup an error trap
  12.     On Error GoTo z_error_trap
  13.     '
  14.     'Define the value for your SQL string
  15.     strSQL = "UPDATE Customers " & _
  16.             "SET Customers.CustomersStatus = 'Closed' " & _
  17.             "WHERE ((Customers.CustomersStatus = 'Invoiced') " & _
  18.                 "AND (Customers.CustomerID = " & Me.Invoice_ID.Value & "))"
  19.     '
  20.     '> for debuging
  21.     '>
  22.     Debug.Print "strSQL = " & strSQL
  23.     Stop
  24.     '> Press <ctrl><G> for the Immediate window - check the resolution of the string.
  25.     '> Now you can either [F8] to step the code, or [F5] to let it run
  26.     '
  27.     DoCmd.RunSQL strSQL
  28. z_cleanup:
  29. Exit Sub
  30. z_error_trap:
  31.     MsgBox "Error Number: " & Err.Number & vbCrLf & _
  32.         "Error Detail: " & Err.Description, vbCritical, "Oh Bother - Error Trap"
  33.     Resume z_cleanup
  34. End Sub
Note the stop, your code will STOP running on this line, please press <ctrl-g>. We need to see the value you get for strSQL in the immediate window - a simple cut and paste from the Immediate to a post on this site.

Please let the code finish running by either [F8] or [F5], report any errors you get. PLEASE the EXACT NUMBER and the EXACT TEXT of the error message. Please, do not shortcut the message nor leave the number out as both are importaint to understanding the issue at hand.
May 30 '13 #5
Thanks a lot for your help. Hope to resolve it soon.

I tried both code from Seth and ZMBD. I still got the Compile Error. I don't understand why. The name of the field in Sales table is "Invoice_ID" with Data Type "Number".

When I run the code, it stop and highlight the following
Expand|Select|Wrap|Line Numbers
  1. .Invoice_ID
then I got a menu "Compile Error - Method or Data Member not found".

Please is it necessary to put the ".value" after the
Expand|Select|Wrap|Line Numbers
  1. .Invoice_ID
. In the beginning of the post I use".value" cause I have thought for number, we should put the ".value"

Moreover in the immediate window there's nothing written.
May 30 '13 #6
zmbd
5,501 Expert Mod 4TB
As you did not provide the line, I can only guess that the point of error is & Me.Invoice_ID.Value as given on line 18 of the code I posted?

Well,
More than likely that field is not part of the recordset or control collection of the form.

That is to say:
If the form is Bound to the table via a query, then the query doesn't have that field as part of it.
If the form is Bound to the table, then the field is not named as you have it named.
If the form is Bound and you somehow have two controls with that same name (wow... I'd like to see that) then Access is choking on the duplicate.
If the form is un-bound then there does not exist a control with the name you've called.
May 30 '13 #7
Seth Schrock
2,965 Expert 2GB
Given that the Me. expression was used instead of Me!, I would say it would have to be something with the control. Possibly the control is named something like Text1 or txtInvoice_ID but is bound to the Invoice_ID field.
May 30 '13 #8
zmbd
5,501 Expert Mod 4TB
I know that there is alot of debate about the "bang" v "dot" and when to use them.

In either case, if the field name was available OP wouldn't get this error. Although the bang will refer to controls in normal usage, V2003 and newer - If the form is bound and one uses a bang to refer to the control and the control is not there by that name; however a record set field has that name then MSA will try to use the field value.

Caused me all sorts of headache.

Could have been do to the bound v unbound of the controls. In one case the Bang works and the other it doesn't.

Besides, it appears that MS is pressing towards all dot with a few exceptions for subforms and the new navigation control.

In this case, I think the bang/dot is a red-herring and we need for OP to take a careful look at what the form is bound to (or not) and what all of the control names are.
-
May 30 '13 #9
ZMBD, you were right. I found the mistake. The form is Bound to the table via a query and the query doesn't have that field in it. I haven't inserted the field caused Other field have been grouped. When the field "Invoice_ID" is inserted, it expands the query causing too many records to view.

The code works now. Thanks.

But on my side I need to rewrite the code. Cause there are too many records to click on. Can you help me to write it?

The idea is as follows. "On Click" the code below will run and do the following action:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE Sales SET Sales.Action = 'On-Order'" & _
  2. "WHERE (Sales.Action)= 'Re-Order' and (Sales.Barcode_Of_Goods)='" & Me.Barcode_Goods & "'"
Now that the field "Action" have been changed to "On-Order" can we add a code like
Expand|Select|Wrap|Line Numbers
  1. look for a record in table Customers 
  2. where [Invoice_Id]=[Customers].[CustomerID] 
  3. Then 
  4. Edit Record 
  5. Set the Field CustomersStatus its value to "Closed"
.
May 31 '13 #10
zmbd
5,501 Expert Mod 4TB
Glad you found the issue.

Your subsequent request for help should be started in a new thread as we prefer to keep to the one-question/problem per thread rule.
-z
May 31 '13 #11
Oralloy
988 Expert 512MB
Hey, jazee007,

As a courtesy for those of us who read posts and learn from them, can you mark the "correct answer", please?

When you do, it helps other developers who are using the site to understand the core problem and ultimate solution, without having to guess.

Cheers,
Oralloy
May 31 '13 #12
Sure... both Seth and ZMBD help to find the mistake. THe Invoice_ID field was missing from the query. Now that I inserted the "Invoice_ID" It works. I used the following code
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdUpdateSales_Click()
  2.  
  3.  DoCmd.RunSQL "UPDATE Customers SET Customers.CustomersStatus = 'Closed' " & _
  4.  "Where (Customers.CustomersStatus) = 'Invoiced' and (Customers.CustomerID) = " & Me.Invoice_ID & ""
  5.  
  6. End Sub
It works nice. Thanks for all and sorry for the delay to reply. I will use another thread for the other issue. Thanks again.
Jun 1 '13 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: mbuna | last post by:
I have the following php code: echo $iyear; $query = "UPDATE users set title='$ititle', lastname='$ilastname', firstname='$ifirstname', middleinit='$imiddleinit',
2
by: Eric J Owens | last post by:
TIA for any help! I have an a2k mdb front end using sql server 2k on backend. my form show router port assignments, when an assignment is deleted, there is a button that is supposed to set the...
5
by: Don Seckler | last post by:
I have an update query that runs when a report closes. I have several reports that will need to run the update query with diferent criteria. I'd like to simply make the criteria change in the...
1
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; ...
1
by: Leo | last post by:
Hello, I have two tables (Access 2000). In the first table I have a column containing postcodes. In the second table I have a list of all postcodes for a particular borough. I'm trying to write...
4
by: EJO | last post by:
In the query builder, I can create a query that will take a date/time as criteria, and the query finds the records: SELECT SRSrvcsEquip.Stock, SRSrvcsEquip.Activity, SRSrvcsEquip.EquipOwned,...
2
by: Neville MADDEN | last post by:
Good morning everyone, I'm producing an A97 database of Mining records. One of the tables has the map sheet references as SH/55-14. The other table has the references as SH55-17. I want to...
0
by: redragon | last post by:
This may be a silly question, but what exactly needs to change in an RSS file to make the feed reader re-index the feed? I am hand updating the file. Just changing just pubDate under channel...
4
by: Constantine AI | last post by:
I am working on an automatic Purchase Order Generator where with a click of a button will automatically assign a PONo to certain stock items (based on the SuppNo). The coding i have at present is as...
0
Walt in Decatur
by: Walt in Decatur | last post by:
I have a form which is tied to a table with data for equipment belonging in a particular space (tbl_equpment_data). This form is actually a subform on a main form which also deals with other data...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.