assign default values to fields when empty; using a nested if.

These 4 fields are linked to a part number, and are on a form to which needs validation.

These 4 fields have default values, but some parts do not currently have values on those fields.

When the user visits the form, and the fields are blank, and the user does not provide input for the fields, I need to assign the default value to whichever one of these 4 fields was left blank, or all if need be.

I have a simple If statement for one of the fields:

if Len(uField1) = 0 then 
    uField1 = "9999"
    Msgbox ("part was saved successfully")
end if

Open in new window


I do not want to have 4 separate if statements. If possible I would like to check all the fields using just one Nested If statement, and displaying the part saved message only once.

Would it be possible to get some help to complete this?

Thank you much.
metropiaAsked:
Who is Participating?
 
Paul MacDonaldDirector, Information SystemsCommented:
Okay, so you only ever need the one message.  

As to the nesting IF statements, your code will be much more readable if you don't go that route, but...


if Len(uField1) = 0 then
    uField1 = "9999"
    if Len(uField2) = 0 then
        uField2 = "9999"
        if Len(uField3) = 0 then
            uField3 = "9999"
            if Len(uField4) = 0 then
                uField4 = "9999"
            end if
        end if
    end if
end if

Msgbox ("part was saved successfully")


...versus...

if Len(uField1) = 0 then
    uField1 = "9999"
end if

if Len(uField2) = 0 then
    uField2 = "9999"
end if

if Len(uField3) = 0 then
    uField3 = "9999"
end if

if Len(uField4) = 0 then
    uField4 = "9999"
end if

Msgbox ("part was saved successfully")
0
 
Paul MacDonaldDirector, Information SystemsCommented:
Under what conditions would the part not be saved?  That is, if the user didn't put any information in, you're going to do it for them, right?  So regardless what they type, you're saving something...if I understand you correctly.
0
 
metropiaAuthor Commented:
you are right, if the user does not put any information, then the program will do it for them using the default values.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Martin LissOlder than dirtCommented:
You could also make the textboxes into a control array and then do this


Dim lngIndex As Long

For lngIndex = 0 To uField.Count - 1
    If LenB(Trim(uField(lngIndex).Text = "")) Then
        uField(lngIndex).Text = "9999"
    End If
Next
MsgBox ("part was saved successfully")

Open in new window

0
 
aikimarkCommented:
* you could prepopulate the textboxes with their default values.  You would want the textboxes to select all the text in its GotFocus event.
* you could store the default values in the textboxes' .TAG property
* iterating the textboxes, like MartinLiss suggests, you would assign that textbox's text property to that textbox's tag property if that textbox's text property were a zero length string.
* you can store the default properties in an array with the same dimensions as the textbox control array, instead of using the TAG property.
* if performance iterating the controls is a problem, populate a collection object with the textboxes. Then iterate the collection object variable instead of the form's control collection.
0
 
metropiaAuthor Commented:
using your second code snippet.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.