473,396 Members | 1,917 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.

How to RunSQL for update a record in ON CLICK event?

I want to run an execute SQL statement which update a field in a table after clicking on "save" button

here the code I use:
Expand|Select|Wrap|Line Numbers
  1. Private Sub save_Click()
  2. On Error GoTo Err_save_Click
  3. Dim v_balance As Double
  4. Dim v_final_balance As Double
  5.  
  6.     DoCmd.RunCommand acCmdSaveRecord
  7.  
  8.     v_balance = DoCmd.OpenQuery("SELECT closing_balance FROM balanceT")
  9.  
  10.     v_final_balance = v_balance - Forms![cash].[cash_out_usd] + Forms![cash].[cash_in_usd]
  11.  
  12.  
here I should Run my update statement which is: "UPDATE balanceT SET closing_balance = v_final_balance "

But How should I do that?

THANKS.
Aug 5 '13 #1
6 1376
Seth Schrock
2,965 Expert 2GB
This is the way that I do it:

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim db as DAO.Database
  3.  
  4. Set db = CurrentDb
  5. strSQL =  "UPDATE balanceT SET closing_balance = " & v_final_balance
  6. 'Note: you will need a WHERE clause or else all the 
  7. 'records in the table will get updated
  8.  
  9. db.Execute strSQL, dbFailOnError
  10.  
  11. Set db = Nothing
If you don't want to get the warning that you are changing x number of records, you can add DoCmd.SetWarnings False right before the db.Execute line, but make sure to set it back to true at the end of the procedure.

Just curious, have you tested line 8 of your code? If it does work, I wasn't aware of the fact that you could do it that way.
Aug 5 '13 #2
zmbd
5,501 Expert Mod 4TB
Seth:
Just curious, have you tested line 8 of your code? If it does work, I wasn't aware of the fact that you could do it that way.
It shouldn't work that way.
If OP will do a debug compile he should receive a "Compile Error: Expected Function or Variable" error and the ".openquery" will be selected.
Aug 5 '13 #3
Seth Schrock
2,965 Expert 2GB
I didn't see how it could work. Perhaps a DLookup() would be the simplest method to get the value the OP is looking for.
Aug 5 '13 #4
zmbd
5,501 Expert Mod 4TB
Seth Schrock
I didn't see how it could work. Perhaps a DLookup() would be the simplest method to get the value the OP is looking for.
New topic new thread };-) ... and yes from what is posted, that would appear to be a good guess.
Aug 5 '13 #5
Oky,
It works now

I used DLookup function as you suggest:
Expand|Select|Wrap|Line Numbers
  1. Private Sub save_Click()
  2. On Error GoTo Err_save_Click
  3. Dim v_balance As Double
  4. Dim v_final_balance As Double
  5.  
  6. Dim strSQL As String
  7. Dim db As DAO.Database
  8.  
  9. Set db = CurrentDb
  10.  
  11. DoCmd.RunCommand acCmdSaveRecord
  12.  
  13. DoCmd.OpenQuery "Q_get_balance"
  14.  
  15.  
  16.  
  17. v_balance = DLookup("colsing_balance", "Q_get_balance")
  18.  
  19.  
  20. v_final_balance = v_balance - Forms![cash].[cash_out_usd] + Forms![cash].[cash_in_usd]
  21.  
  22.  
  23. strSQL = "UPDATE balanceT SET colsing_balance = " & v_final_balance
  24.  
  25.  
  26. db.Execute strSQL, dbFailOnError
  27.  
  28. Set db = Nothing
  29.  
  30.  
  31. Exit_save_Click:
  32.     Exit Sub
  33.  
  34. Err_save_Click:
  35.     MsgBox Err.Description
  36.     Resume Exit_save_Click
  37.  
  38. End Sub
  39.  
  40.  
Thanks for you efforts.
but How can I refresh my form after saving because the text boxes stay holding the values I entered.
I want to empty some text boxes after saving.

THANKS.
Aug 6 '13 #6
zmbd
5,501 Expert Mod 4TB
Please limit to One Question Per Thread.
You can re-link back to this thread in the new one should you need to.
Aug 6 '13 #7

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

Similar topics

4
by: Rotsj | last post by:
Hi, i try to execute an update query from within a form, but i get the message: run time error '3144'. Syntax error on the update statement when i try something like this: DoCmd.RunSQL...
8
by: RC | last post by:
In my Access 2002 form, I have a combo box and on the AfterUpdate event I use DoCmd.RunSQL ("UPDATE .... to update records in a table. When it starts to run I get a message "You are about to...
4
by: James P. | last post by:
Hello there, I have a bound-form using Navigator so that the user can move back and forth to update record in the form. Every time a record was modified and the user clicks the Navigator to...
7
by: MLH | last post by:
I tried the following code to prevent a checkbox from being updated (going from a value of Null to True, from True to False or from False to True). I was surprised it did not work. Can anyone...
1
by: radha | last post by:
hi friend Could somebody please tell me how to update an access record having two primary keys?(vb.net, asp.net, access Also how do we show a page from another page,for eg i am in page1.aspx and...
3
by: Tommy | last post by:
Sometimes, I use "TextBox_TextChanged" event to auto fill in other text box once user tab out from the search box. i.e. connect to database and use the field to search out record and then fill in...
1
by: TriednTested | last post by:
Hello I have a subform and to edit the data I place it on the main form where changes can be made, then a cmd button is used to execute the docmd.runsql update query. However I am constantly getting...
8
by: =?Utf-8?B?UmljaA==?= | last post by:
My from contains a "Move Next" button. When the user clicks on the "Move Next" button - several procedures get invoked and eventually, the dataset underlying the form will display main data from...
2
by: =?Utf-8?B?SGV6YWw=?= | last post by:
Hi, I am trying to add a new record to a table but everytime I click the button, somehow it saves the record twice... I've created a stored procedure to insert records into a table and I called...
4
by: SAL | last post by:
Hello, I'm working, basically my first, AJAX page and am having a few problems. One is that the Click event for a button I have in UpdatePanel1 is not getting called. I've tried with the button...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.