Results 1 to 14 of 14
  1. #1
    redekopp is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108

    Putting a limit on a field based on another fields value

    I am hoping this is easier then I think it is!



    I have a dialog form that opens .
    Three text fields:
    1. Total acres of claim: (name = txtTotalAcres) (control source is " =[TempVars]![TmpActualAcres] ")
    2. Total acres counted thus far: (name = txtTotalAcresCounted) (control source is "=DSum("[acresCounted]","[tblClaimItemCounts]","[claimitemid]=" & [ClaimItemID])"
    3. Acres of this count: (name = txtCountAcres) (control source = "acresCounted")

    If field #1 = 150 and field #2 = 130 then I want it so that there is no way they can type a value higher than 20 in field #3 without giving a MsgBox and not allowing the value to save.

    Not sure if its a control source of some sort or what! But I need txtCountAcres to know that it can't make the value of txtTotalAcresCounted higher then txtTotalAcres after update. Which also makes me wonder how to make that record update before closing the form.

    Thanks!

  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,642
    The typically place to do validation is the before update event of the control or the form. Here's something on that:

    http://www.baldyweb.com/BeforeUpdate.htm

    Normally I'd suggest the form's event, but since the user can't enter anything in the other two controls, that textbox's event should work too.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    redekopp is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108
    Thanks pbaldy, I edited the code I used and it is responding correctly. Thanks!
    Code:
    Private Sub txtCountAcres_BeforeUpdate(Cancel As Integer)
    
    If (Me.txtCountAcres + Me.txtTotalAcresCounted) > Me.txtTotalAcres Then
      MsgBox "You cannot exceed the Total Acres of this claim."
      Cancel = True
      Else
    End If
    
    End Sub
    Not sure if im allowed to hijack my own thread but I had one more question!

    Im doing an after update of a field that needs to auto populate the following field with the dlookup of a value from the table.

    Code:
    Me.PlantLoss = DLookup("[cropvalue]", "[tblCropSheetData]", "[croprow] = " & [TempVars]![TVCropRow] And "[cropcolumn] = " & [TempVars]![TVCropColumn] And "[calctype] = " & [TempVars]![tmpBranchLossName])
    Me.PlantLoss.Requery
    I get a type mismatch error. All the tempvars give correct values... I cant seem to crack this one.

  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,642
    Your concatenation is off, not restarting the string after the TempVar. See if this helps:

    DLookup Usage Samples
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    redekopp is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108
    hmm i edited it to how it appears to be in that example, so i thought...
    Code:
    Me.PlantLoss = DLookup("[cropvalue]", "[tblCropSheetData]", "[croprow] =" & [TempVars]![TVCropRow] & " And [cropcolumn] =" & [TempVars]![TVCropColumn] & " And [calctype] = '" & [TempVars]![tmpBranchLossName] & "'")
    still getting data type mismatch in criteria expression.
    [cropvalue] = short text
    [croprow] = short text
    [cropcolumn] = short text
    [calctype] = short text

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try replacing the TempVars with known values to see if the correct value is returned.

    So if
    [croprow] = 5
    [cropcolumn] = 10
    [calctype] = "ABC"

    you would have
    Code:
    Me.PlantLoss = DLookup("[cropvalue]", "[tblCropSheetData]", "[croprow] = 5 And [cropcolumn] = 10 And [calctype] = 'ABC'")
    If that works, add one TempVar
    Code:
    Me.PlantLoss = DLookup("[cropvalue]", "[tblCropSheetData]", "[croprow] =" & [TempVars]![TVCropRow] & " And [cropcolumn] = 10 And [calctype] = 'ABC'")
    Continue for the other two variables until the DLookup works....

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    If they're all short text, they all need the single quotes like you have on the last.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    still getting data type mismatch in criteria expression.
    [cropvalue] = short text
    [croprow] = short text
    [cropcolumn] = short text
    [calctype] = short text
    Hmmmmm. The OP must have edited Post#5.

    I'm sure it was
    [cropvalue] = number
    [croprow] = number
    [cropcolumn] = number
    [calctype] = short text


  9. #9
    redekopp is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108
    My apologies, I did indeed edit the field types.. thought i did it fast enough! sorry!
    I tried the single quotes but i think my formatting is wrong... i get confused with all the quotes, i may of over quoted.

    Code:
    Me.PlantLoss = DLookup("[cropvalue]", "[tblCropSheetData]", "[croprow] ="'" & [TempVars]![TVCropRow] & "'"" And [cropcolumn] ="'" & [TempVars]![TVCropColumn] & "'"" And [calctype] = "'" & [TempVars]![tmpBranchLossName] & "'")

  10. #10
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Start of string = double quote
    Quotes in the string = single quote
    e.g.
    Code:
    "[croprow]='" & .....

  11. #11
    redekopp is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108
    Quote Originally Posted by aytee111 View Post
    Start of string = double quote
    Quotes in the string = single quote
    e.g.
    Code:
    "[croprow]='" & .....

    I had expected it to look like this but its not liking it..
    Code:
    Me.PlantLoss = DLookup("[cropvalue]", "[tblCropSheetData]", "[croprow] = '" & [TempVars]![TVCropRow] & "'" And [cropcolumn] = '" & [TempVars]![TVCropColumn] & "'" And [calctype] = '"  & [TempVars]![tmpBranchLossName] & "'"")

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    I'll get out of the way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You keep closing the string when you need to keep it open - every time you close you need an & after the double quote, check your string for that.

    " & [TempVars]![TVCropRow] & "'" And [cropcolumn]...

  14. #14
    redekopp is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108
    hallelujah! Praise the vba lord it works!

    Thanks all.

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

Similar Threads

  1. Replies: 4
    Last Post: 05-22-2015, 05:52 AM
  2. Replies: 4
    Last Post: 01-23-2014, 04:34 PM
  3. Limit data entry based on another field
    By chemengr in forum Forms
    Replies: 5
    Last Post: 01-02-2014, 01:21 PM
  4. Replies: 5
    Last Post: 02-08-2013, 11:28 AM
  5. Replies: 3
    Last Post: 07-13-2011, 08:01 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