Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Location
    ny, ny
    Posts
    224

    Unanswered: declaring a variable

    i have two fields in a form .. if the difference between them is higher than a certain value i shud pop up a msgbox
    i think wat i need to do is declare a variable n use that to find the difference
    how do i declare a variable in vba

    thank you

  2. #2
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    in the _AfterUpdate() event of each of your fields:
    doSomeMaths

    then

    private sub doSomeMaths()
    const maxDiffAllowed = 100 'or whatever you want
    if (field1 - field2)^2 > maxDiffAllowed^2 then
    msgbox "whatever you want"
    endif
    end sub

    but it is a very interesting question: how to do unsigned maths in VBA. there must be a better way than ^2

    perhaps remove the sign bit
    if ((field1 - field2) and 32768) > maxDiffAllowed then 'for an integer

    izy
    currently using SS 2008R2

  3. #3
    Join Date
    Apr 2004
    Location
    ny, ny
    Posts
    224
    dont think i wanna do anything complicated
    just simple math ... save the result and check if it is higher than what it should be
    is there no way of simply declaring a variable and using it

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    should have been XOR 32768, but never mind.


    you can declare any number of variables you like, nothing could be easier:
    dim myvar
    ok, it's an ugly variant type, but it's declared.

    now what?

    you need somewhere to put the variable (the code module of your form)
    you need some code to fill the variable.
    you need an event of some sort to cause the code to run.


    my suggestion of the _afterupdate() event is not unreasonable, but you could just as well make a button and use it's _Click() event. you don't even need a variable with my example - just use the names of the text boxes.

    izy
    currently using SS 2008R2

  5. #5
    Join Date
    Feb 2004
    Location
    CT,USA
    Posts
    250
    Originally posted by fullymooned
    dont think i wanna do anything complicated
    just simple math ... save the result and check if it is higher than what it should be
    is there no way of simply declaring a variable and using it
    Izy has the solution for your not so easy task of popping up a message about values entered into txtboxes. Perhaps she spoke over your head? Where did you get "lost"?

Posting Permissions

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