+ Reply to Thread
Results 1 to 6 of 6

Checking a field for a value when the field might be empty

  1. #1
    Forum Contributor
    Join Date
    03-05-2007
    Location
    Falmouth, VA now, Palm Bay, FL for 2 yrs, was Colorado Springs, CO for ten years; Cedark Park, TX; Zeeland, MI; Wilmette, IL; Princeton Junction, NJ; NY, NY
    MS-Off Ver
    365
    Posts
    583

    Checking a field for a value when the field might be empty

    Working in this instance with Forms, but I suspect could be applicable to cells and maybe other.

    In this instance there is a group of vba commands that need be executed if a textbox has a value over 24. It is also possible that the textbox is empty.

    Presently I am doing:

    Please Login or Register  to view this content.
    Because, as you know, if I try:

    Please Login or Register  to view this content.
    I will get a "Type Mismatch" error triggered by "frmA.tbA * 1" if frmA.tbA is empty.

    Is there a way to make the two conditions run from one control line of code? The problem is when I am forced into nested "If"s that all can be empty or have a threshold that must be evaluated.

    As always, Thank-You very much for you consideration!

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Checking a field for a value when the field might be empty

    Hi bruce,

    Do a conversion from string to number using one of these VBA conversion functions first.

    https://msdn.microsoft.com/en-us/vba...sion-functions
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    03-05-2007
    Location
    Falmouth, VA now, Palm Bay, FL for 2 yrs, was Colorado Springs, CO for ten years; Cedark Park, TX; Zeeland, MI; Wilmette, IL; Princeton Junction, NJ; NY, NY
    MS-Off Ver
    365
    Posts
    583

    Re: Checking a field for a value when the field might be empty

    Doesn't seem to work for me, if I read you right. I get the same "Type Mismatch" when I try:
    Please Login or Register  to view this content.

    About the only thing I have found so far involves creating variables and basically:

    Please Login or Register  to view this content.
    I would like to avoid creating and then substituting variables for evaluation if something like I think you were advising and I wrote in the first code-box would work!

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Checking a field for a value when the field might be empty

    Make sure your frmA.tbA is text and not the whole table. Use the immediate window and try different things using debug.print to have it show you what those things are.

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Checking a field for a value when the field might be empty

    Don't know about the rest of your commands but this works as an example.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 12-21-2017 at 10:25 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Forum Contributor
    Join Date
    03-05-2007
    Location
    Falmouth, VA now, Palm Bay, FL for 2 yrs, was Colorado Springs, CO for ten years; Cedark Park, TX; Zeeland, MI; Wilmette, IL; Princeton Junction, NJ; NY, NY
    MS-Off Ver
    365
    Posts
    583

    Re: Checking a field for a value when the field might be empty

    @MarvinP: frmA.tbA was simply my shorthand for a Form named "A" and a TextBox that resided in that form that I also happened to name "A"; I see my attempt to be concise easily lead to confusion! Basically, and again endeavoring to be concise but this time a little more understandable, envision a form with only one textbox. That textbox could be empty or could have a numerical value. That textbox is being evaluated in VBA. In my example I multiply the contents by 1 as that automatically causes the figure, if present, to be treated as a number in the VBA equation/evaluation, as you well know. But if that textbox is empty, Excel throws the error. Because I am working with multiple nested "If" statements/controls, to first test for whether or not TextBox evaluates to vbNullString would dictate/necessitate a full "If-Else-End If" construct for that, and then a partially repetitive with significant variances in the commands full "If-Else-End If" construct for the value that the TextBox contained, if such was the case.

    As to the nesting of multiple "If" statements, each time one "If" evaluates to being true, not only does it trigger a set of commands but it also triggers the evaluation of another "If" statement. At any time when an "If" statement evaluates to False, a set of commands are triggered and the entire rest of the chain of nested "If"s are skipped.

    I could break each "If" out on its own and come up with some additional evaluation as to whether or not that "If" should run, but I was trying to accomplish this as neatly as I could within a tree structure.

    If each textbox being evaluated had a value, this would be a relatively simple process, and if I was comfortable with forcing a value of "0" in any empty textbox, that would also make it simple, but I need there to be a difference in the form between displaying a "0" where a "0" should be, and an empty box that has yet to be evaluated by either the user or by code.

    Though I have made a number of naming substitutions and evaluation statements so I don't get the club I am doing this for upset with me, the following is the exact structure of one of the evaluation trees (yeah, there are a lot more items than one simple textbox involved... I present it simply to attempt to show the level of complexity (above one textbox) as it is much like a basic AI system applied for displaying or hiding items on the form. Note that each item evaluated would cause a crash if it is vbNullString. I suspect variable substitution for field evaluation prior to running the body of present code is my only option):

    Please Login or Register  to view this content.
    @bakerman2: Interesting use of the ":", I hadn't considered this. One complication that still would remain is that I do need to process alternate ("Else") instructions if TextBox1 is vbNullString. I will have to think about this.

    Thank-you both VERY much for considering my problem! What is sometimes most embarrassing is that I occasionally create a problem for myself when there is another way to handle the entire situation; just hoping I am not going down that road again...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 08-15-2016, 02:07 PM
  2. [SOLVED] PowerPivot: Merge Month Field and Year Field Into a Date Field
    By mo4391 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 07-17-2015, 11:59 AM
  3. Replies: 0
    Last Post: 06-26-2012, 09:06 PM
  4. Replies: 3
    Last Post: 03-03-2012, 12:16 PM
  5. checking for a blank field
    By Oldjay in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 12-10-2005, 07:45 PM
  6. Error when checking field for data
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 11:25 PM
  7. Checking whether a field is bold.
    By Craig & Co. in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-23-2005, 12:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1