database - MS Access: programmatically rename columns -
I need to update all column names in the access database of 8 tables. In short, each underscore in column name Should be replaced with the location. Can you recommend a quick way to do this?
I wrote the code for an app last week:
< code> public sub RenameFields (TDF DAO.TableDef as ByRef) dim fld DAO.Field dim strFieldName string dim strNewName string "========== as as as DebugkPrint === ================================= "& amp; VbCrLf & amp; UCase (tdf.Name) select FLD tdf.Fields strFieldName = fld.Name strNewName = Middle (strFieldName, 4) for each episode Left (strFieldName, 3) episode "Boo", "here", "CUR" " Dat "," Haif "," Int "," Mem "," Oz "," TCTT "FDL. Name = StrNeweweb debug.print TDF. No & amp; "." & Amp; StrFieldName & amp; "= & Gt;" & Amp; Select strNewName End Next fld set FLD = Nothing End Sub
In this case, it was used to name a previous developer absurdly VBA variable naming conventions Field (txtLastName what final names, for example, should be) and I had all of them need to remove the code for this is called like:
dim TDF each TDF in DAO.TableDef as CurrentDB.TableDefs Then left (tdf.Name, 3) = "tbl" then say '& lt; = This line can vary for you. RenameFields (tdf) is done if you set the next tdf tdf = nothing
You can easily rename () for RenameFields () sub, the new name to use something like this:
public sub RenameFields (TDF DAO.TableDef as ByRef) dim fld DAO.Field dim strFieldName string dim strNewName As String Debug.Print As "========== ========================== ========== "& amp; VbCrLf & amp; In UCase (tdf.Name) for each Feldi tdf.Fields strFieldName = fld.Name strNewName = Change (strFieldName, "_", "") If strFieldName & lt; & Gt; StrNew name then debug fld.Name = strNewName. Print tdf.Name & amp; "." & Amp; StrFieldName & amp; "= & Gt;" & Amp; StrNewName end if the next fld set fld = nothing end sub
all said, I really, really strongly suggest that you not with spaces Because of the underscore, because it creates more, it is much more difficult to work with field names in SQL and code. I would use a black case with no underscore or location or dash, so that the line above has changed ():
strNewName = change (StrConv (change (strFieldName, "_ "," "," Last name "or" last name "will be changed to" last name "," vbProper "," ", vbNullString)
field names Camel cases for readability are standard for access developers as long as I'm programming in Access (since 1996), and special characters or Rick The location is touted to be causing issues (ie, the square brackets).
Comments
Post a Comment