?
Solved

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

Posted on 2012-09-07
6
Medium Priority
?
309 Views
Last Modified: 2012-09-17
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.
0
Comment
Question by:metropia
6 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 38377546
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
 

Author Comment

by:metropia
ID: 38377560
you are right, if the user does not put any information, then the program will do it for them using the default values.
0
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 1200 total points
ID: 38377586
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 50

Expert Comment

by:Martin Liss
ID: 38377887
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
 
LVL 46

Expert Comment

by:aikimark
ID: 38378878
* 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
 

Author Closing Comment

by:metropia
ID: 38407099
using your second code snippet.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question