PDA

View Full Version : Turning Scripts into UDF's


ABC123

MoGrace
04-21-2007, 12:25 PM
I have created a few simple UDF's for common tasks like opening forms or adding new records, and I know many here find these a great aid in scripting - so I am wondering what sorts of tasks have others turned into UDF's? I am looking for some ideas here... and perhaps we could share our favorite ones and mention how we use them?

Most of mine were written in 4.5 when things were simpler ;)

MoGrace
04-21-2007, 03:13 PM
Here's one I use to open a form with a query on one of the tables that the user can drop with the Show_All() button on the toolbar.



FUNCTION Q_Frm as L(frm_name as C,tbl_name as C,q_filt as C,q_ord as C = "NULL_VALUE()")
'Ex: Q_frm("In Stock List","Yarn_Item","CUSTID = '"+get_custid+"'","")
'if no index is specified RECNO() order will be used, use "" to specify no order
'note global variable get_custid must be declared and assigned a value first
'to use in filter expression
DIM shared frm as P
DIM tbl as P
trace.writeln("' Query Form: "+frm_name)
frm = obj(frm_name)
IF .not. is_object(frm) THEN
frm = :form.load(frm_name)
END IF
query.description = "Q_Form"
query.filter = q_filt
trace.writeln("' Q Filter: "+q_filt)
query.order = q_ord
'force to create a new query
query.options = "M"
'allow user to show all records in form
idx = eval("frm:tables:"+tbl_name+".query_create()")
recs = idx.records_get()
trace.writeln("' Records found: "+ltrim(str(recs)))
trace.writeln("'---------------")
IF recs <1 THEN
cr = chr(13)+chr(10) 'can use function crlf() instead
msg1 = "No records were found that match your criteria"+cr
msg2 = "Do you want to display all records?"
msg_str = msg1+msg2
response = ui_msg_box("Query Result",msg_str,3+32)
SELECT
CASE response = 2
idx.drop()
frm.close()
GOTO ENDIT
CASE response = 6
frm.show_all()
END SELECT
END IF
frm.resynch()
frm.show()
frm.activate()
'---------------
ENDIT:
'---------------
END
END FUNCTION

csda1
04-23-2007, 10:50 AM
Hi Robin,

Much of what you are looking for is located in the code archive section of the forum message board. Some of my more recent contributions that have good examples or techniques in them are

HTTP Status codes to Descriptions (http://msgboard.alphasoftware.com/alphaforum/showthread.php?t=64460)
Clean up messy phone format (http://msgboard.alphasoftware.com/alphaforum/showthread.php?t=59345)
Conversion to Roman Numerals (http://msgboard.alphasoftware.com/alphaforum/showthread.php?t=22892)
AgeMonths (http://msgboard.alphasoftware.com/alphaforum/showthread.php?t=22983)
Min, Max code of up to 10 values, any type (http://msgboard.alphasoftware.com/alphaforum/showthread.php?t=61362)

There are also many code pieces and functions scattered throughout the forum areas provided by many contributers.

DaveM
05-16-2007, 05:10 PM
bump

DaveM
05-18-2007, 12:06 PM
Robin,

It may be of interest to know that when we were doing clipper, if you used the same code more than 2 places, you made a udf. Functions were a way of life in clipper and those functions were actually bought, sold and traded like any commodity.

It seems more and more like Alpha is developing toward a function oriented program with the strength of xbasic get better with each new release. There are some really great functions being used out there and it would be nice if people shared some of them in this code part of the forum.

Dave Mason