AccessBlog.net

News, links, downloads, tips and tricks on Microsoft Access and related

About Me Search
Alex
Name:Alex Dybenko

Location:Moscow, Russia

Thursday, February 07, 2008

Is field name valid?

I wrote some export to DBF (yes, people still using DBF to exchange data!) routine today, and looked for some function to check that field name is valid. Have found nothing, so I wrote my own. Here it is, if you think something should be added – please write in comments:

Function IsValidFieldName(strFieldName As String) As Boolean
    Dim i As Long
    'first check that is does not start with number
    IsValidFieldName = Not (strFieldName Like "#*")
    For i = 1 To Len(strFieldName)
        Select Case True 'then check for allowed chars
            Case Mid(strFieldName, i, 1) Like "[a-z]"
            Case Mid(strFieldName, i, 1) Like "[0-9]"
            Case Mid(strFieldName, i, 1) Like "_"
            Case Else
                'found something - not passed
                IsValidFieldName = False
                Exit For
        End Select
    Next i
End Function

2 Comments:

Anonymous Anonymous said...

Hi Alex,

just di it in one step:

Function IsValidFieldName2(strFieldName As String) As Boolean

If Not strFieldName Like "*[!a-z,0-9,_]*" And _
Not strFieldName Like "#*" Then
IsValidFieldName2 = True
End If

End Function

--
best regards
KN

11:56 PM  
Blogger Alex Dybenko said...

Thanks!
also tried to make it in one line, but have not succeed. Cool!

12:58 PM  

Post a Comment

<< Home