Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Adding or Subtracting a % from a list of values in a continous form in Access

I have a continuous form with 2 fields:
[Last_Quarter_Forecast] and [Last_Quarter_Actual]

I would like to add the following 2 buttons on in the header of the form:
[-5%] and [+5%]
The 2 button should increase (or decrease) all the values displayed in [Last_Quarter_Actual] by 5% on every click

What the VBA code of the buttons should be?

Thank you as always for your support
0
fitaliano
Asked:
fitaliano
  • 5
  • 2
1 Solution
 
Jeffrey CoachmanCommented:
Code to Add 5% to [Last_Quarter_Forecast].

I am sure you can figure out the rest on your own...

;-)

JeffCoachman

Private Sub cmdAdd5_Click()
    CurrentDb.Execute "UPDATE Orders SET [Last_Quarter_Forecast] = ([Last_Quarter_Forecast]*0.05)+[Last_Quarter_Forecast];"
    Me.Requery
End Sub
0
 
Jeffrey CoachmanCommented:
Or a little safer:

Private Sub cmdAdd5_Click()
    CurrentDb.Execute "UPDATE YourTable SET [Last_Quarter_Forecast] = ([Last_Quarter_Forecast]*0.05)+[Last_Quarter_Forecast];",dbfailonerror
    Me.Requery
End Sub

0
 
fitalianoAuthor Commented:
Thank you boag2000 but I actually don't want to update the entire table but only the records displayed by the form.  Sorry for not specifying this in my initial quetion.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jeffrey CoachmanCommented:
Then will youi be telling me what the filter is?
;-)

0
 
Jeffrey CoachmanCommented:
Actually you might do better with this for my first solution...:
Private Sub cmdAdd5_Click()
    CurrentDb.Execute "UPDATE YourTable SET [Last_Quarter_Forecast] = [Last_Quarter_Forecast]+([Last_Quarter_Forecast]*0.05);",dbfailonerror
    Me.Requery
End Sub
0
 
fitalianoAuthor Commented:
The filter is another form with a query on the table.

0
 
Jeffrey CoachmanCommented:
Until you tell me what the filter is and how, exactly, it is being applied we are at an impasse here...

How about you post a sample DB:
Sample database notes:
1. Back up your database(s).
2. Combine the front and back ends into one database file.
3. Remove any startup options, unless they are relevant to the issue.
4. Delete any objects that do not relate directly to the issue.
5. Remove any references to any "linked" files (files outside of the database, Images, OLE Files, ...etc)
6. Remove any references to any third party Active-x Controls (unless they are relevant to the issue)
7. Remove, obfuscate, encrypt, or otherwise disguise, any sensitive data.
8. Compile the code. (From the database window, click: Debug-->Compile)
9. Run the compact/Repair utility.
10. Remove any Passwords and/or security.
11. Post explicit steps to replicate the issue.
12. Test the database before posting.

In other words, ...post a database that we can easily open and immediately see the issue.

JeffCoachman
0
 
DockieBoyCommented:
Maybe you could loop through the record source for your form and make the changes, then requery your form?

Private Sub MyButton_Click()
 Dim db as DAO.Database
 Dim rs as DAO.Recordset
 Dim ssl as string
  Set db = CurrentDb
  ssl = "select * from MyRecordSource where UseFileterHere;"
  Set rs = db.OpenrecordSource(ssl)
 If rs.RecordCount = 0 then
  Set db = Nothing
  Set rs = Nothing
  Exit Sub
 Else
  With rs
   .MoveFirst
   Do While Not .EOF
   'Make your changes here'
   .Update
   .MoveNext
  Loop
 .Close
 End With
 End If
  Set db = Nothing
  Set rs = Nothing
 Me.Requery
End Sub
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now