Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate FoxPro Field Naming Constraints

Status
Not open for further replies.

USMA99

Programmer
Oct 12, 2000
82
US
Hello,

Easy one here. I'm sure this has been done many times over and want to avoid reinventing the wheel but I can't find it using the forum search...

We're looking for a procedure to see if a string is valid for use as a field name (not in a DBC). Essentially, this means it must contain only letters, numbers, and underscores, start with a letter and be 10 characters or less in length.

An input mask to do the same would also be helpful.

thanks in advance
 
Good question. I'm sure you'll get some interesting replies. For what it's worth, here is my suggestion. No doubt someone will come up with something neater.

Code:
lcTest = "lcSomething"

lcValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

IF LEN(CHRTRAN(UPPER(lcTest), lcValid, "")) = 0 AND ;
  BETWEEN(LEN(lcTest), 1, 10) AND ;
  (ISALPHA(lcTest) OR LEFT(lcTest, 1) = "_")

  ? "OK"

ELSE

  ? "Bad"

ENDIF

Give it a try.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Code:
Function IsFieldNameOkay()
   Lparameter tcFieldname

   Local llOkay
   TRY
     Create Cursor curFieldNameTest ( (tcFieldname) C(1))
     llOkay = .T.
   CATCH
     llOkay = .F.
   FINALLY
     Use In Select("curFieldNameTest")
   ENDTRY

   Return llOkay
Endfunc

Bye, Olaf.
 
Still you can do something like this:

Code:
Create Cursor curStrange ( "Hello" C(1))

But the resulting field name will be Hello, not "Hello". It has no real value, as you can't create field names with spaces this way, as this let's you expect. There was one use for this, but I forgot.

Nevertheless it should be the best possible test to simply try to create a cursor using that field name. The one thing I first oversaw was you wanted to test if names would also be usable in free tables. You could either change the code to create a free table instead of a cursor and delete the file afterwards, or create an index tag on a test cursor, as that has the same 10 character limitation.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top