msaccess-vcs-addin icon indicating copy to clipboard operation
msaccess-vcs-addin copied to clipboard

clsDbHiddenAttribute (possibly others) fails when objectname (to be exported) has & in name

Open datenbank-projekt opened this issue 1 year ago • 2 comments

I am taking over an old Access database. Tablesnames have ' and & characters in them. Exporping fails in line If Application.GetHiddenAttribute(contType, doc.Name) Then in clsDbHiddenAttribute when doc.Name has a &.

Maybe something like this helps (not my code, but our favorites AI's):

Function EscapeObjectName(objectName As String) As String
    Dim safeName As String
    Dim i As Integer
    Dim c As String
    Dim specialCharacters As String
    
    ' Define special characters that need escaping
    specialCharacters = "~`!@#$%^&*()-+=|\/{}[]:;'<>,.?/ "

    ' Check if the name contains any special characters
    For i = 1 To Len(objectName)
        c = Mid(objectName, i, 1)
        If InStr(specialCharacters, c) > 0 Then
            ' If it contains special characters, escape the whole name
            EscapeObjectName = "[" & objectName & "]"
            Exit Function
        End If
    Next i
    
    ' If no special characters are found, return the original name
    EscapeObjectName = objectName
End Function

'example usage:

Sub TestEscapeObjectName()
    Dim unsafeName As String
    Dim safeName As String
    
    unsafeName = "Table&Name"
    safeName = EscapeObjectName(unsafeName)
    
    Debug.Print "Original Name: " & unsafeName
    Debug.Print "Safe Name: " & safeName
    
    ' Example usage in a query
    Dim sql As String
    sql = "SELECT * FROM " & safeName
    Debug.Print "SQL: " & sql
    
    ' Example usage in checking hidden attribute
    Dim isHidden As Boolean
    isHidden = Application.GetHiddenAttribute(acTable, safeName)
    Debug.Print "Is Hidden: " & isHidden
End Sub


datenbank-projekt avatar Jul 09 '24 13:07 datenbank-projekt