473,395 Members | 1,931 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,395 software developers and data experts.

Very basic "Update table via VBA" query

56
I can't believe it but I don't have a clue how to update a table using VBA!

What I'm trying to do is:

1) Do a search on the last 3 periods turnover for one client in particular and check to see if they are all over 60k
2) Go to a table which states rebate rates for each client and change that one clients rate based on whether they are over or under 60k for the 3 months

I'm assuming for part 1, I'd have to set up a 3 record array and have a SQLquery feed the results of a check for the last 3 months turnover into it, and then check each part of the array for the 60k mark.
Then part 2 I would have to update the relevant field. Although I'm not entirely sure how to go and pinpoint that record and update it.

Point out any flaws with my part 1 assumption. Thats how I believe I'd do it.
Nov 8 '07 #1
6 4217
Rabbit
12,516 Expert Mod 8TB
I don't know what "last 3 periods turnover" means. Can you provide some pseudo data?
Nov 8 '07 #2
Widge
56
Sorry. Entries like


Date field Amount Field
2007-05 £40000
2007-06 £45000
2007-07 £55000
2007-08 £60000
Nov 9 '07 #3
Widge
56
I'm having a go at writing the bit where I find the last 3 periods turnover, am I doing this in a totally long winded way??

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub TweakRebates()
  3.  
  4.     Set cnn = CurrentProject.Connection
  5.     Set Tweak = New ADODB.Recordset
  6.     Dim Rec As Record
  7.     Set Rec = New Record
  8.  
  9. frcSQL = "SELECT (Sum([Rebates_CBS Transaction Data].[Base Amount])*-1) AS [SumOfBase Amount]" _
  10.         & " from [Rebates_CBS Transaction Data] GROUP BY [Rebates_CBS Transaction Data].[Base Amount], [Rebates_CBS Transaction Data].Name, [Rebates_CBS Transaction Data].[Rebate Date]" _
  11.         & " HAVING ((([Rebates_CBS Transaction Data].[Rebate Date])='" & Period & "' Or " _
  12.         & " ([Rebates_CBS Transaction Data].[Rebate Date])='" & Period1 & "' Or" _
  13.         & " ([Rebates_CBS Transaction Data].[Rebate Date])='" & Period2 & "')" _
  14.         & " AND (([Rebates_CBS Transaction Data].Name)='Frc Furniture Resource Centre'))" _
  15.  
  16.     With Tweak
  17.         .Open frcSQL, cnn, adOpenForwardOnly, adLockReadOnly
  18.     End With
  19.  
  20.     a = 1
  21.  
  22.     Set Tweak = Rec.GetChildren
  23.  
  24.  
  25.     While Not Tweak.EOF
  26.         Set MonthCheck(a) = Tweak(a)
  27.         Debug.Print MonthCheck(2)
  28.         Tweak.MoveNext
  29.         a = a + 1
  30.     Wend
  31.  
  32.  
  33. Debug.Print frcSQL
  34.  
  35. End Sub
  36.  
  37.  
What I'm doing is running a query and populating a recordset with the 3 periods turnover in question. I can't figure out how to reference them.

What I was going to do was load them into an array and check to see if they were all over 60k.

Now is this totally unnecessary? Because what I want is quite simple, but the process of doing it is getting increasingly more and more difficult. You should be able to see from the above that I'm not entirely used to using Recordsets and generally bumble about using F1 help trying different techniques.
Nov 9 '07 #4
Widge
56
Right, I've figured out the above now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub TweakRebates()
  3.  
  4.     Set cnn = CurrentProject.Connection
  5.     Set Tweak = New ADODB.Recordset
  6.  
  7. frcSQL = "SELECT (Sum([Rebates_CBS Transaction Data].[Base Amount])*-1) AS [SumOfBase Amount]" _
  8.         & " from [Rebates_CBS Transaction Data] GROUP BY [Rebates_CBS Transaction Data].Name, [Rebates_CBS Transaction Data].[Rebate Date]" _
  9.         & " HAVING ((([Rebates_CBS Transaction Data].[Rebate Date])='" & Period & "' Or " _
  10.         & " ([Rebates_CBS Transaction Data].[Rebate Date])='" & Period1 & "' Or" _
  11.         & " ([Rebates_CBS Transaction Data].[Rebate Date])='" & Period2 & "')" _
  12.         & " AND (([Rebates_CBS Transaction Data].Name)='Frc Furniture Resource Centre'))"
  13. '    Debug.Print frcSQL
  14.     With Tweak
  15.         .Open frcSQL, cnn, adOpenForwardOnly, adLockReadOnly
  16.     End With
  17.  
  18.     a = 1
  19.  
  20.     While Not Tweak.EOF
  21.         Set MonthCheck(a) = Tweak![SumOfBase Amount]
  22.         Debug.Print MonthCheck(a)
  23.         Tweak.MoveNext
  24.         a = a + 1
  25.     Wend
  26.  
  27. End Sub
  28.  
  29.  
This will help me identify if the periods all go over the £60k turnover amount.

Now I have to figure out how to get into the table of rebate rates, go to the relevant suppliers record and amend the rebate rate.

I would assume I could use a find record to get to the relevant place BUT how to change the correct field?

Would an update SQL query work in this case?
Nov 9 '07 #5
Widge
56
And finally, my solution which works!

Expand|Select|Wrap|Line Numbers
  1. Sub FRCTweakRebates()
  2.  
  3. ' the below skips all of this stuff if its not necessary
  4.  
  5.     If (Year(Period) <= 2007 And Month(Period) < 8) And (Year(Period) = 2007 And Month(Period) > 4) Then
  6.         RebateToSet = 1
  7.         GoTo SkipTo
  8.     ElseIf (Year(Period) <= 2007 And Month(Period) <= 4) Then
  9.         RebateToSet = 3
  10.         GoTo SkipTo
  11.     End If
  12.  
  13. 'sets up recordset to hold the last 3 months from period specified turnover in drop down box
  14.  
  15.     Set cnn = CurrentProject.Connection
  16.     Set Tweak = New ADODB.Recordset
  17.  
  18. frcSQL = "SELECT (Sum([Rebates_CBS Transaction Data].[Base Amount])*-1) AS [SumOfBase Amount]" _
  19.         & " from [Rebates_CBS Transaction Data] GROUP BY [Rebates_CBS Transaction Data].Name, [Rebates_CBS Transaction Data].[Rebate Date]" _
  20.         & " HAVING ((([Rebates_CBS Transaction Data].[Rebate Date])='" & Period & "' Or " _
  21.         & " ([Rebates_CBS Transaction Data].[Rebate Date])='" & Period1 & "' Or" _
  22.         & " ([Rebates_CBS Transaction Data].[Rebate Date])='" & Period2 & "')" _
  23.         & " AND (([Rebates_CBS Transaction Data].Name)='Frc Furniture Resource Centre'))"
  24. '    Debug.Print frcSQL
  25.     With Tweak
  26.         .Open frcSQL, cnn, adOpenForwardOnly, adLockReadOnly
  27.     End With
  28.  
  29.     a = 1
  30.  
  31. ' writes the turnover to an array
  32.  
  33.     While Not Tweak.EOF
  34.         MonthCheck(a) = Tweak![SumOfBase Amount]
  35.         Debug.Print MonthCheck(a)
  36.         Tweak.MoveNext
  37.         a = a + 1
  38.     Wend
  39.  
  40.     RebateToSet = 0
  41.  
  42. ' test to see if they pass the 60k mark
  43.  
  44.     FlagTest = MonthCheck(1) > 60000 And MonthCheck(2) > 60000 And MonthCheck(3) > 60000
  45.  
  46. ' sets rebate based on test
  47.  
  48.     If FlagTest = True Then
  49.         RebateToSet = 1
  50.     ElseIf FlagTest = False Then
  51.         RebateToSet = 2
  52.     End If
  53.  
  54. Tweak.Close
  55. cnn.Close
  56. Set cnn = Nothing
  57. Set Tweak = Nothing
  58.  
  59. SkipTo:
  60.  
  61. 'automatically updates the FRC PFH Rebate amount accordingly based on date and/or 60k check
  62.  
  63.     Set cnn2 = CurrentProject.Connection
  64.     Set RebTab = New ADODB.Recordset
  65.  
  66.     RebTabSQL = "SELECT Rebates_Supplier.PfHRebateAmt from Rebates_Supplier WHERE " _
  67.         & "(((Rebates_Supplier.Name) = 'Frc Furniture Resource Centre'))"
  68.  
  69.     RebTab.Open RebTabSQL, cnn2, adOpenKeyset, adLockOptimistic, adCmdText
  70.  
  71.     RebTab!PfHRebateAmt = RebateToSet
  72.     RebTab.Update
  73.  
  74. cnn2.Close
  75. Set cnn2 = Nothing
  76.  
  77.  
  78.  
  79. End Sub
  80.  
Let me know if I've really beat around the bush on this.
Nov 9 '07 #6
Rabbit
12,516 Expert Mod 8TB
I suspect you can do what you want using subqueries but I have trouble following what it is that you want to do.

I only know of two fields and what they represent in your table. You've given me [Rebate Date] and [Amount] as your relevant fields. But you mention a bunch of other fields and I don't know what they represent or how they relate to those two fields.
Nov 9 '07 #7

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

Similar topics

1
by: avinash | last post by:
hi myself avi i am developing one appliacaion in which i am using vb 6 as front end, adodb as database library and sql sever 7 as backend. i want to update one table for which i required data from...
6
by: David Shorthouse | last post by:
Hello folks, I have a problem with an update query on an asp not updating the table in an Access db. The code runs, I have no errors, but when I examine the table, nothing was updated. The query...
8
by: cainlevy | last post by:
I'm wondering if there's any way to speed up create table queries? Besides upgrading hardware, that is. The very simplest table creation query, "create table table1 ( field1 INT(10))" is taking...
6
by: mo | last post by:
I need to bring the ssn's into UniqueSups (supervisors) from tblNonNormalized. My inherited DB is not normalized and I find it extremely irritating due to the workarounds needed. I created...
4
by: rdraider | last post by:
I am looking for some assistance with an update query that needs to link 3 tables: This query ran and reported over 230,000 records affected but did not change the field I wanted changed, not...
5
by: Brandon Mackie | last post by:
I am incredibly new to access and trying to learn as I go. I have set up a few queries one of which is executed by a docmd.runsql in visual basic. Because it is an update query it asks for...
4
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
2
by: Tony Wainwright | last post by:
Hi guys I'm pretty new to mysql and was just wondering if you can update multiple fields at the same time i.e: UPDATE tablename SET field1 = value1 AND field2 = value 2 etc... WHERE field0 =...
4
by: JamieF | last post by:
Sorry if I've missed something obvious, but I'm trying to do a really basic update query in Access 2007, and I can't get it to work. UPDATE DISC INNER JOIN DISC2 ON DISC.DiscID = DISC2.DiscID SET...
1
by: giovannino | last post by:
Dear all, I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !). 1) Given that I'm not a programmer I can't understand why...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.