PDA

View Full Version : Conditionally Required In Field Rules


ABC123

Zradene
06-30-2014, 02:27 PM
Hi Guys, I have a Character field which I have set as Auto Increment. I have the Data Entry set to Conditionally Required.
The condition is based on another character field being equal to 'YES', and if this is true I want the auto incremented value to display the next value.
If <> 'Yes' I want the field to stay blank. I have set the Simple Default Expression to "000001".
For some reason the incremented next value still displays even if the conditional field is <> 'Yes'.
Maybe I am missing something here? I have also tried to do this using the field rules on the form itself with no success.
Both fields are contained within a set of 2 databases.

Tom Cone Jr
06-30-2014, 02:44 PM
The Conditionally Required data entry field rule simply prevents the user from saving the new record if a field value is required. It does NOT prevent the auto-increment rule from supplying a value.

Zradene
06-30-2014, 02:59 PM
Hi Tom, ok mate I hear where your coming from. Any suggestions on how I should tackle this then ?

Many thanks.

Stan Mathews
06-30-2014, 03:14 PM
Code the cansaverecord event in the field rules.

if another character field being equal to 'YES'
grab the greatest value in the field presently set to autoincrement
increment that value
put that value in the field presently set to autoincrement
end if

and remove the autoincrement rule.

Zradene
06-30-2014, 03:39 PM
Hi Stan, thanks for the info. I shall give this a try right now.

Much appreciated.

Zradene
06-30-2014, 04:15 PM
I am not quite getting this Tom, I am getting an error message "Table not in change or Enter mode".

Stan Mathews
06-30-2014, 04:40 PM
tbl = table.current()
if alltrim(tbl.fld1) = "Yes"
tbl.id =increment_value( tablemax("aexptest",".T.","id"))
end if

Where fld1 is the field to check for "Yes"
aexptest is the table name
id is the previously autoincremented field

Zradene
07-01-2014, 09:54 AM
38428
Hi Stan, that ID field (CERT) is putting in an incremented number even if the FLD1 (MOTOR) is NO ?
see attached

Stan Mathews
07-01-2014, 10:13 AM
That's not the code I gave you.

You're trying to write a field rule for the testing_items table based on a value in the product table? Ain't gonna happen. Alpha doesn't support set field rules.

You should be able to code the cansave event for your data entry form. I'd have to see a sample set and support files to assist.

Zradene
07-01-2014, 10:18 AM
Stan, I've got this all working now, but I notice the increment still increases even though the non Motors are = to No.
What I am hoping to do is only allow the increment to increase if the Motor = "Yes".
I need to log the numbers when a motor is "Yes", as 000001, 000002, 000003, 000004 etc without having missing increments in between when the Motor = "No".
Fred

Zradene
07-01-2014, 10:19 AM
Ahh, I see.

Zradene
07-01-2014, 10:39 AM
Stan, happy to send you the complete program via Dropbox if you want to take a look.
What I have at the moment is almost perfect, just need the increments to run in sequence.
Fred

Stan Mathews
07-01-2014, 10:46 AM
Just show the code and tell where you're using it. Not just a picture of the code, copy and paste here or attach a text file. If you're doing this on a form attach a screen shot of the form in design mode where we can see the object names.

Zradene
07-01-2014, 12:00 PM
The code being used in the Field Rules within the main table structure, NOT the field rules on the form itself. Screen Shot attached.38429
tbl = table.current()
if alltrim(Product->Motor) = "Yes"
tbl.Cert =increment_value( tablemax("Testing_Items",".T.","Cert"))
end if

Stan Mathews
07-01-2014, 12:40 PM
If you're going to persist with creating a table level field rule which relies on something outside of the table (Product->Motor), at least reference the value as a form object rather than a table->fieldname.

Place the motor field on the form, even if you want to hide it. Check the object name of this new field so you can properly reference it (should be motor, doesn't matter as long as you verify and use what it is).

Then

tbl = table.current() 'I assume the testing_items table is the parent of the set upon which the form is based?
if alltrim(motor.value) = "Yes"
tbl.Cert =increment_value( tablemax("Testing_Items",".T.","Cert"))
end if

I generally recommend checking for the form being open when field rule code depends upon that situation. That way the code doesn't error if you perform record entry outside of the form.

if is_object("yourformname")
tbl = table.current() 'I assume the testing_items table is the parent of the set upon which the form is based?
if alltrim(motor.value) = "Yes"
tbl.Cert =increment_value( tablemax("Testing_Items",".T.","Cert"))
end if

end if

Zradene
07-02-2014, 08:45 AM
Hi Stan, don't understand what is happening here. The object name of the Motor field IS MOTOR, the Testing_items IS38434 the parent in this set.
The CERT field is in the Testing_items table, the Motor field is in the Product table.
For some reason I am still getting this error message indicating that the object name for the MOTOR field is incorrect. This is not the case.

Stan Mathews
07-02-2014, 09:23 AM
Did you put the motor field somewhere on the form. If you didn't it is not an object that Alpha can find.

With our form in design mode use the View menu, Object Explorer (second from bottom). The object names of all objects will appear in the left hand column. Hilight the motor field and all properties available will appear in the right hand column. Hilight the Value property. Use the Copy to Clipboard button at the bottom. You will then see the proper object name syntax, relative and explicit and the value property syntax, relative and explicit.

You can hilight any one of those and it will be transferred to the clipboard for pasting into your script. (The topparent: is "understood" for most situations but it doesn't hurt to use it.)

Zradene
07-02-2014, 09:42 AM
Hi Stan, yes the Motor field is on the form and in the object explorer the object name is MOTOR.

Tom Cone Jr
07-02-2014, 09:55 AM
Fred said:
don't understand what is happening here

Field rules defined for a table are designed to be run whenever data entry to the table occurs. Any form. Any browse. Multiple forms. Multiple browses. They are bound to the table. Unless you include code that looks "beyond" the table itself, your code cannot "see" beyond the table itself. Here, you're trying to retrieve a field value from a record in a second table, that may or may not be open when data entry occurs. You seem to be thinking that because your custom form is based on a set, the table level field rules will be able to automatically "see" records in the related tables. That's a perspective that's form based. But your rules are table based. Big difference.

Zradene
07-02-2014, 10:11 AM
Ok Stan understood. I will use variables and a calc field to sort this problem. As I said, I do need the increment values to be assigned only to Motor = "Yes", but without missing numbers in between when Motor field is "No".

Thanks for all your help on this. I am most grateful.