Results 1 to 12 of 12
  1. #1
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76

    How do i do a check on two input fields from main form and sub form?

    I have a mainform and a subform.

    My main form contains a field called mainamount and subform contains a field called subamount. I want to make sure subamount does not excedd mainamount and i used this code in beforeupdatevent:


    If [subamount] > [mainamount] Then
    MsgBox ("WARNING:You have entered an amount larger that total amount!")
    Cancel = True
    Me.subamount.Undo
    End If


    but it does not work..Which part of the code do i change?

  2. #2
    Rainlover's Avatar
    Rainlover is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2009
    Location
    Queensland Australia
    Posts
    691
    MainAmount does not exist in the subform so it it is not been recognised.

    Your code should first address the Main Form then the field.

    You could use the expression builder in a blank query to help build the code.

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Like:

    If Me.subamount > Forms!mainformname.mainamount Then
    MsgBox ("WARNING:You have entered an amount larger than total amount!")
    Cancel = True
    Me.subamount.Undo
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    Quote Originally Posted by June7 View Post
    Like:

    If Me.subamount > Forms!mainformname.mainamount Then
    MsgBox ("WARNING:You have entered an amount larger than total amount!")
    Cancel = True
    Me.subamount.Undo
    End If
    Hm...sorry i forgot to mention that its a relationship between subform and subsubform not mainform and subform...i have used your code but there was a error 2465(could not find facilitysubform):

    Private Sub LenderCommitment_BeforeUpdate(Cancel As Integer)

    If Me.LenderCommitment > Forms!facilitysubform.FacilityAmount Then
    MsgBox ("WARNING:You have entered an amount larger than total amount!")
    Cancel = True
    Me.LenderCommitment.Undo
    End If
    End Sub

    Do i have the put the brackets in??

    Edit:

    I tried this too but it doesnt work:

    Private Sub LenderCommitment_BeforeUpdate(Cancel As Integer)

    'Forms!AddRollover.rolloversubform.Form.ListLender Rollover

    If Me.LenderCommitment > Forms!AddCommitmentDetail.facilitysubform.Facility Amount Then
    MsgBox ("WARNING:You have entered an amount larger than total amount!")
    Cancel = True
    Me.LenderCommitment.Undo
    End If
    End Sub

    AddCommitmentDetail is the mainform, while facilitysubform is subform, and lender commitment is the field in the subsubform

  5. #5
    Rainlover's Avatar
    Rainlover is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2009
    Location
    Queensland Australia
    Posts
    691

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The code is in BeforeUpdate event of which control on which form?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    sorry guys it worked

  8. #8
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    Edit: Sorry but i need help again..i tried my code this afternoon and it gave an error"2465". Previously i tested the code and it worked..but today it just gave an error.

    This is the code:
    Private Sub LenderCommitment_BeforeUpdate(Cancel As Integer)

    If Me.LenderCommitment > Forms!AddCommitmentDetail.facilitysubform.Form.Fac ilityAmount Then
    MsgBox ("WARNING:You have entered an amount larger than total amount!")
    Cancel = True
    Me.LenderCommitment.Undo
    End If
    End Sub

    This code is placed in the beforeupdate event of LenderCommitment in a subsubform. Im comparing this value against a field in the subform, thus i have used the code

    Forms!AddCommitmentDetail.facilitysubform.Form.Fac ilityAmount.( AddCommitmentDetail is mainform while facilitysubform is subform. FacilityAmount is the field in the subform)

  9. #9
    Rainlover's Avatar
    Rainlover is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2009
    Location
    Queensland Australia
    Posts
    691
    You could use the expression builder in a blank query to help build the code.

    Create a Blank Query.

    Use the expression Builder to find each value in the Forms. Do this for each Value in separate Fields.

    Once you have the correct values showing you can then rebuild your VBA.

    This is not the normal way of doing things but will help you to learn the correct syntax.

  10. #10
    Rainlover's Avatar
    Rainlover is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2009
    Location
    Queensland Australia
    Posts
    691
    Your Form should be open when creating the query.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    When I have subforms I always give the subform container a name different from the form it holds, like: ctrFacility. Then the syntax to refer to controls on subform I use:

    If Me.LenderCommitment > Forms!AddCommitmentDetail.ctrFacility.Form.Facilit yAmount Then
    Last edited by June7; 12-28-2011 at 01:54 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    Rainlover's Avatar
    Rainlover is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2009
    Location
    Queensland Australia
    Posts
    691
    June7

    You are correct. A Sub Form is actually a Control so giving it a separate Name is good practice.

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

Similar Threads

  1. Check-box to hide/show input fields
    By RapidFireGT in forum Forms
    Replies: 2
    Last Post: 12-05-2011, 05:55 PM
  2. Main Form to NewForm for Input
    By leean21 in forum Forms
    Replies: 2
    Last Post: 11-07-2011, 02:06 PM
  3. Replies: 2
    Last Post: 06-17-2010, 04:15 PM
  4. Replies: 1
    Last Post: 06-14-2010, 02:31 AM
  5. Rookie Messed Up Main Input Form
    By genest11 in forum Forms
    Replies: 2
    Last Post: 01-14-2010, 02:17 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