Results 1 to 14 of 14
  1. #1
    oregoncrete is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    15

    Very new to VBA, could use some basic help

    Hello all,



    I am quite new to VBA, but am learning quickly. I am trying to eventually find which day we hit our breakpoint, then post that date to a table, and any remaining sales be tallied up into a Sales after Breakpoint field. Right now I am just working on creating an equation to find our breakpoint.

    Here is what I have so far:

    Code:
    Public Sub BreakPoint()
    Dim cum_sales As Currency
    Dim bp_found As String
    Dim prof_postbp As Currency
    Dim FindBP As Recordset
    Set FindBP = CurrentDb.OpenRecordset("Totals By Date", dbOpenDynaset)
    cum_sales = 0
    bp_found = n
    If Not FindBP.EOF Then FindBP.MoveFirst
    If bp_found = n Then
    Do While cum_sales - [Module Cost] < 0
    FindBP.MoveNext
    cum_sales = cum_sales + [Total Sales]
    Loop
     
    End If
    End Sub
    I do not think I am referencing records correctly and would like some help with this. I want to be using my current database, and I want the table it is accessing to be Totals By Date.

    Any help is greatly appreciated!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    It would be

    cum_sales = cum_sales + FindBP![Total Sales]
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    george,

    two things. First, the rs needs to be opened officially before you start moving between the recs:
    Code:
    FindBP.movelast
    FindBP.movefirst
    And to reference a field in a recordset, the ! symbol is used:
    Code:
    FindBP!fieldnameHere
    And, if the field name has spaces in it, you need [] braces to combat the parsing issue:
    Code:
    FindBP![field name here]

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Quote Originally Posted by ajetrumpet View Post
    george,

    two things. First, the rs needs to be opened officially before you start moving between the recs:
    Code:
    FindBP.movelast
    FindBP.movefirst
    In my experience that's only necessary when you want an accurate record count. I never do it for loops like that.

    I'd add that this line is unnecessary:

    If Not FindBP.EOF Then FindBP.MoveFirst

    The recordset will always start at the first record.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    oregoncrete is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    15
    Thank you both greatly for the help. I have made those changes and I have receive this error:

    Runtime Error '3265':

    Item not found in this collection.

    When Debugged it highlights this portion:

    cum_sales = cum_sales + FindBP![Total Sales]

    Any advice?



    EDIT*

    Quote Originally Posted by pbaldy View Post
    In my experience that's only necessary when you want an accurate record count. I never do it for loops like that.

    I'd add that this line is unnecessary:

    If Not FindBP.EOF Then FindBP.MoveFirst

    The recordset will always start at the first record.
    This is great help also, thank you!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    The error implies that isn't the actual field name in the table. Is it perhaps an underscore rather than a space?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    oregoncrete is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    15
    Quote Originally Posted by pbaldy View Post
    The error implies that isn't the actual field name in the table. Is it perhaps an underscore rather than a space?
    You're a life-saver; it was an underscore. Thank you so very much!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    We were happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    oregoncrete is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    15
    Actually, I could use a little more help now. That is if anyone is up to it . Once it has found cum_sales >= 0 I need it to post the Sales Date from this record to a Breakeven Date field on another table.

    Would this look like:

    If cum_sales - FindBP![Module Cost] >= 0 Then
    bp_found = y
    If bp_found = y Then
    _______ FindBP![Sales Date] to Seasons![Breakeven Date]

    I dont know which action would go before FindBP![Sales Date]

  10. #10
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by pbaldy View Post
    In my experience that's only necessary when you want an accurate record count. I never do it for loops like that.

    I'd add that this line is unnecessary:

    If Not FindBP.EOF Then FindBP.MoveFirst

    The recordset will always start at the first record.
    thanks, Paul. I will send you a message if I prove that wrong. I have never actually tested it. Just always assumed the safe side.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Oregon: You can open a recordset on the other table, then

    rsOtherTable!FieldName = FindBP![Sales Date]

    Adam: Just checked in the db I'm working on. I open 25 recordsets throughout the db, and there are only 2 "MoveLast" lines, both of which when I needed a record count. If it's required in some instance other than that, I haven't run into it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    oregoncrete is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    15
    Quote Originally Posted by pbaldy View Post
    Oregon: You can open a recordset on the other table, then

    rsOtherTable!FieldName = FindBP![Sales Date]

    Adam: Just checked in the db I'm working on. I open 25 recordsets throughout the db, and there are only 2 "MoveLast" lines, both of which when I needed a record count. If it's required in some instance other than that, I haven't run into it.
    Thank you for your continued help, I am sure this is what I will need, however, how do I open a second recordset simultaneous to my first?

    If I do:

    Set FindBP = CurrentDb.OpenRecordset("OTHER TABLE", dbOpenDynaset)
    then won't FindBP![Sales Date] give me an error since it was on my original opened recordset?

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Along the lines of:

    Dim FindBP As Recordset
    Dim rsTarget As Recordset

    Set FindBP = CurrentDb.OpenRecordset("Totals By Date", dbOpenDynaset)
    Set rsTarget = CurrentDb.OpenRecordset("TheOtherTable", dbOpenDynaset)

    That said, I'm not sure where you want the value in the other table. If a new record then that will work and you'd use .AddNew. If you mean to modify an existing record, you'd want to open the recordset on an SQL statement that only returned the desired record, and use .Edit.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    oregoncrete is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    15
    Quote Originally Posted by pbaldy View Post
    Along the lines of:

    Dim FindBP As Recordset
    Dim rsTarget As Recordset

    Set FindBP = CurrentDb.OpenRecordset("Totals By Date", dbOpenDynaset)
    Set rsTarget = CurrentDb.OpenRecordset("TheOtherTable", dbOpenDynaset)

    That said, I'm not sure where you want the value in the other table. If a new record then that will work and you'd use .AddNew. If you mean to modify an existing record, you'd want to open the recordset on an SQL statement that only returned the desired record, and use .Edit.
    Thank you! Unfortunately at this time I do not know any SQL, but I am doing my best to learn quickly.

    The table that contains my sales data has a new record for each product set on every day. So say Set 1 sells 10$ on day 1 and so on. So on this table there are numerous records for Set 1. On the other table I have everything categorized by timeframe, so say in Winter '10 we had Set 1 and Set 2.

    EX:

    Table 1:
    (Set 1) (###$) (1/1/11) (Set 1 Cost)
    (Set 1) (###$) (1/2/11) (Set 1 Cost)
    (Etc.)

    Table 2:
    (Winter '10) (Set 1) (Set 1 Cost) (Breakpoint Date)

    I have it calculating Sales for Set 1 on day one, then adding cumulatively. So cumulative sales - Set Cost >= 0 then update breakpoint date on Table 2.

    Again, thank you very much. If this is too time-consuming or I am asking too much then please don't hesitate to tell me so

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Basic Questions
    By jlclark4 in forum Access
    Replies: 6
    Last Post: 12-13-2010, 12:09 PM
  2. Basic Question
    By Dalagrath in forum Forms
    Replies: 12
    Last Post: 10-31-2010, 05:36 AM
  3. Very Basic DB (Relationships)
    By SilentPirate007 in forum Database Design
    Replies: 4
    Last Post: 06-06-2010, 01:25 PM
  4. Really basic question
    By jimlaw in forum Forms
    Replies: 1
    Last Post: 07-27-2009, 07:20 AM
  5. Basic Question
    By chris11590 in forum Forms
    Replies: 0
    Last Post: 08-04-2008, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums