Results 1 to 5 of 5
  1. #1
    Nevsky78 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    3

    Creating 'Common' VBA code

    Hi all,

    I have code attached to the BeforeUpdate event for each of 12 columns (which correspond to 12 months of the year).

    I know this is possible with a custom function of some sort but would anyone know how I could get this 'built in' and not have to have 12 sub routines?

    What they are saying is if there is any value in any of the other month columns, then you cannot enter an amount.

    I have the examples for apJan, apFeb and apMar.



    Code:
    Private Sub apJan_BeforeUpdate(Cancel As Integer)    
        If Nz(Me.apAug, 0) + Nz(Me.apSep, 0) + Nz(Me.apOct, 0) + Nz(Me.apNov, 0) + Nz(Me.apDec, 0) + Nz(Me.apJul, 0) + Nz(Me.apFeb, 0) + Nz(Me.apMar, 0) + Nz(Me.apApr, 0) + Nz(Me.apMay, 0) + Nz(Me.apJun, 0) > 0 Then
        MsgBox "This record has an amount in another month"
        Me.apJan.Undo
        Cancel = True
        End If
        
    End Sub
    
    
    Private Sub apFeb_BeforeUpdate(Cancel As Integer)
        
        If Nz(Me.apAug, 0) + Nz(Me.apSep, 0) + Nz(Me.apOct, 0) + Nz(Me.apNov, 0) + Nz(Me.apDec, 0) + Nz(Me.apJan, 0) + Nz(Me.apJul, 0) + Nz(Me.apMar, 0) + Nz(Me.apApr, 0) + Nz(Me.apMay, 0) + Nz(Me.apJun, 0) > 0 Then
        MsgBox "This record has an amount in another month"
        Me.apFeb.Undo
        Cancel = True
        End If
        
    End Sub
    
    
    Private Sub apMar_BeforeUpdate(Cancel As Integer)
        
        If Nz(Me.apAug, 0) + Nz(Me.apSep, 0) + Nz(Me.apOct, 0) + Nz(Me.apNov, 0) + Nz(Me.apDec, 0) + Nz(Me.apJan, 0) + Nz(Me.apFeb, 0) + Nz(Me.apJul, 0) + Nz(Me.apApr, 0) + Nz(Me.apMay, 0) + Nz(Me.apJun, 0) > 0 Then
        MsgBox "This record has an amount in another month"
        Me.apMar.Undo
        Cancel = True
        End If
        
    End Sub
    Any help would be gratefully received!

    Nick

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    If you have a table with 12 columns representing months, I think you may have a table structure/normalization issue.
    Please post a jpg of your tables and relationships.

    You may want to review the first few topics at this site
    http://www.rogersaccesslibrary.com/forum/topic238.html

  3. #3
    Nevsky78 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    3
    Hi orange,

    Thank you for getting back to me.

    I am aware that I have not set up the database in the best normalized structure. My issue has been I do not have advanced enough VBA to create a form with datasheet/continuous subform with 12 months horizontally and however many records vertically. I know that requires the use of a temp table based on a crosstab but no matter how much I have tried I haven't been able to set my database up like that.

    The 'data entry' forms need to have 2 filter parameters and a datasheet/continuous subform by months.

    I have therefore got round this by having 12 months columns in the 'base' table and then normalizing that table into the correct structure.

    I've also corrected my code that I have pasted above. I have attached my DB.

    If you have any advice it would be greatly received!

    Thanks,
    Nick
    Attached Files Attached Files

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by Nevsky78 View Post
    ...I know this is possible with a custom function of some sort but would anyone know how I could get this 'built in' and not have to have 12 sub routines?
    If you want carry out some action (such as prohibiting data in 11 Controls if a 12th Control is populated) you're going to have to associate code with all 12 Controls! You can have 'common code,' i.e. a User Defined Function, but you'd still have to Call the Function from each Control!

    Given that you only want Data entered for one month per Record, the logical thing, to my mind, would be to replace your 12 Fields with two Fields, one for the month and one for the amount. On the Form I'd use a Combobox for the month. Once an amount was entered for a given month, you could not enter an amount for a second month except by removing the amount from the original month. You could still do your summing for each month by using Sum() against the 'amount' Field with the 'month' Field being used in 'Where' clause.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Nevsky78 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    3
    Hi Linq,

    Thanks for getting back to me with that.

    The 2nd option would definitely be my ideal solution. However, I don't know how to set up the form (and any crosstabs etc.) so that it appears with Jan to Dec horizontally and the records vertically, dependent on the 2 filters - brand and category.

    Would it be some kind of after update event to put the Month in the month field depending on which combobox you have entered data? i.e. cboJan, cboFeb...

    Regards,
    Nick

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

Similar Threads

  1. common form fields
    By soulice in forum Forms
    Replies: 9
    Last Post: 04-05-2012, 02:58 PM
  2. InStr and Mid code creating type mismatch
    By sephiroth2906 in forum Programming
    Replies: 6
    Last Post: 09-15-2011, 10:59 AM
  3. Common entries within multiple tables
    By JesterMania in forum Queries
    Replies: 4
    Last Post: 08-02-2011, 02:57 PM
  4. Creating Query from Code, Concatenate fields
    By eww in forum Programming
    Replies: 5
    Last Post: 07-18-2011, 02:19 PM
  5. Replies: 1
    Last Post: 06-09-2011, 10:12 PM

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