auto naming convention

peterjb44

Registered User.
Local time
Today, 00:23
Joined
Mar 7, 2013
Messages
55
Hi all,

not sure if the code below works or not so can someone test it for me.

I can get it to run, I input the form name and it says conversion complete, but when I check the form in design mode the name and control are still the same with no prefix

I made a macro for this to run.......
runcode function name fConvertControlNames()

what am I doing wrong? :banghead: I could of added the prefix myself in the time I've wasted with this :confused:

I got the code from here http://www.utteraccess.com/wiki/index.php/Naming_Conventions

thanks

peter


Code:
'==============================================================================
' NAME: fConvertControlNames
' DESC: Converts the prefix on Controls for a new form
' RETURNS: Boolean: False if function failed
' ARGS: Optional strFormName: Name of the form to perform the operation on
'                             If ommited you will be prompted for the form name
'       Optional bNotifyCompletion: notify when completed
'
'==============================================================================
'ErrStrV3.01
Public Function fConvertControlNames( _
   Optional strFormName As String = "", _
   Optional bNotifyCompletion As Boolean = True _
   ) As Boolean
On Error GoTo Error_Proc
Dim Ret As Boolean
'=========================
 Dim strFieldPrefix As String
 Dim strControlPrefix As String
 Dim iLenFieldPrefix As Integer 'length of the field prefix
 
 Dim frm As Form
 Dim ctl As Control
 Dim bFlag As Boolean 'loop flag
 Dim i As Integer 'loop counter & msgbox return
'=========================
 
 'change these to match your convetions
 strFieldPrefix = "fld"
 strControlPrefix = "ctl"
 
 
 'init field prefix length
 iLenFieldPrefix = Len(strFieldPrefix)
 
 
 
 'check for a provided formname, prompt if none
 If Len(strFormName) = 0 Then
   strFormName = InputBox("Please enter the Form Name:")
 End If
 
 'make sure they entered something
 If Len(strFormName) = 0 Then
   MsgBox "No Form Name supplied, the function will exit"
   GoTo Exit_Proc
 End If
 
 'make sure the form exists
 bFlag = False
 For i = 0 To CurrentProject.AllForms.Count - 1
   If CurrentProject.AllForms(i).name = strFormName Then bFlag = True
 Next
 
 If Not bFlag Then
   MsgBox "The Form Name you entered (" & strFormName & _
          ") does not exist!", vbCritical
   GoTo Exit_Proc
 End If
 
 'if the form is open prompt the user to close it...
 For Each frm In Forms
   If frm.name = strFormName Then
     i = MsgBox("The form " & strFormName & " is open and will need " & _
                "to close to perform the operation.  Would you like " & _
                "to close the form and continue?  Changes will be " & _
                "saved...", vbYesNo, "Close Form?")
     If i = vbYes Then
       DoCmd.Close acForm, strFormName, acSaveYes
     Else
       MsgBox "Function Cancelled, no changes made"
       GoTo Exit_Proc
     End If
   End If
 Next frm
 Set frm = Nothing
 
 
 
 'now that we know the form exists and is not in use, we'll do
 'what we came to do...
 
 'open form in hidden design view
 DoCmd.OpenForm strFormName, acDesign, , , , acHidden
 
 'loop the controls, checking for the field prefix and converting when found
 For Each ctl In Forms(strFormName)
   If Left(ctl.name, iLenFieldPrefix) = strFieldPrefix Then
     ctl.name = Replace(ctl.name, strFieldPrefix, strControlPrefix, 1, 1)
   End If
 Next ctl
 
 'save and close the form
 DoCmd.Close acForm, strFormName, acSaveYes
 
 'notify if required
 If bNotifyCompletion Then
   MsgBox "Conversion complete"
 End If
 
 
 Ret = True
'=========================
Exit_Proc:
 Set frm = Nothing
 Set ctl = Nothing
 fConvertControlNames = Ret
 Exit Function
Error_Proc:
 Ret = False
 Select Case Err.Number
   Case Else
     MsgBox "Error: " & Trim(Str(Err.Number)) & vbCrLf & _
       "Desc: " & Err.Description & vbCrLf & vbCrLf & _
       "Module: zDEVmodTools, Procedure: fConvertControlNames" _
       , vbCritical, "Error!"
 End Select
 Resume Exit_Proc
 Resume
End Function
 
Did you do this?

'change these to match your convetions
strFieldPrefix = "fld"
strControlPrefix = "ctl"


If you have controls on your form, you can replace the prefix to a new prefix through this function.

I created a form and prefixed my controls with xxx.

by changing these lines

strFieldPrefix = "xxx"
strControlPrefix = "cntl"

It did convert the prefixes to "cntl"

Seems you had to be pretty consistent and standardized to use the xxx or whatever in the first place.

But if you have a specific requirement, this function does convert the prefixes.

Good luck.
 
Last edited:
Hi jdraw,

lol, I misunderstood what this done, for some reason I thought it added a prefix to the field name and the control name :o

thanks for the reply

peter
 
I think you could adjust the code to do so. But if you ran it multiple times you may get multiple prefixes - UNLESS you specifically check to see if your desired prefix already existed.
 

Users who are viewing this thread

Back
Top Bottom