Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2003
    Location
    Columbia, MO
    Posts
    57

    Unanswered: AfterUpdate Event question

    I have a form that a user has to enter percent into. The way I have it now, for example, if they have 49%, they have to enter .49 otherwise they would get 4900% if they enter 49. What I need is a way for the user to enter 49 and get it to show up as 49%. I was thinking of an AfterUpdate Event where it took the percent value and divided it by 100. However, I cannot for the life of me figure out how to write this. There maybe a better way as well, and I am all ears on suggestions. I would greatly appreciate some input on this subject.

    Thanks,
    David

  2. #2
    Join Date
    Sep 2003
    Location
    T.O.
    Posts
    326
    You probably have error checking to ensure the value they give you is numeric.

    This is where I would put the percent check:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If CDbl(Me.txtPercentage) >= 1 Then Me.txtPercentage = Me.txtPercentage / 100
    End Sub

    This way, if they enter .49 they get .49, if they enter 57 they get .57. The only thing this won't take care of is that elusive 100%. Users who enter based on a decimal system will recognize 1 as 100, but the Before_Update routine will recognize it as .01 which is what you want it to do for the users who don't use the decimal system.

    HTH
    All code ADO/ADOX unless otherwise specified.
    Mike.

  3. #3
    Join Date
    Jan 2003
    Location
    Columbia, MO
    Posts
    57
    Thanks for the reply. This is for the leanness percentage of market hogs. It won't ever get to 100%, so that shouldn't matter. However, I do have a problem with the code. Here's what I have so far:

    Private Sub PercentLean_BeforeUpdate(Cancel As Integer)

    If CDbl(Me.PercentLean) >= 1 Then Me.PercentLean = Me.PercentLean / 100

    End Sub



    I get a runtime error. It says, "The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Access fromsaving the data in the field."

    When I debug, it highlights Me.PercentLean = Me.PercentLean / 100

    Any suggestions?

    Thanks again,

    David

  4. #4
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    You can't change the value of the field in the BeforeUpdate event.
    Put an error check in the BeforeUpdate to make sure they put a number in:

    If not isnumeric(me.percentlean) then
    me.undo 'take out the change the user tried to enter
    cancel = true
    end if

    Put the line that HomerBoo suggested in the AfterUpdate event.
    Inspiration Through Fermentation

  5. #5
    Join Date
    Jan 2003
    Location
    Columbia, MO
    Posts
    57
    That worked like a charm. Thanks for the help. I appreciate the input on both your parts.

    Thanks!

  6. #6
    Join Date
    Sep 2003
    Location
    T.O.
    Posts
    326
    RedNeckGeek: Would a dependent control on decad1's form cause his error to pop up?

    I created a table with one field: Percentage - Number, Double
    and a form bound to this table with one textbox bound to the field, and the before_update event didn't cause this error for me.

    I think the error indicates the field is trying to be updated and gets reset, hence the before_update is called again.

    Just curious as I don't want to run into design problems down the road.

    Thanks.
    All code ADO/ADOX unless otherwise specified.
    Mike.

  7. #7
    Join Date
    Sep 2003
    Location
    T.O.
    Posts
    326
    RedNeckGeek: Would a dependent control on decad1's form cause his error to pop up?

    I created a table with one field: Percentage - Number, Double
    and a form bound to this table with one textbox bound to the field, and the before_update event didn't cause this error for me.

    I think the error indicates the field is trying to be updated and gets reset, hence the before_update is called again.

    Just curious as I don't want to run into design problems down the road.

    Thanks.
    All code ADO/ADOX unless otherwise specified.
    Mike.

  8. #8
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    HomerBoo,

    I'm surprised you got it to work. It's never worked for me, for exactly the reason you mention (it would cause a loop).

    I use the before update to validate/test data entered by the user.
    I perform calculations/changes with the after update event.

    If anyone else in the forum want's to chime in... please do!

    Mark
    Inspiration Through Fermentation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •