Vba 删除Access数据库中所有表中所有字符串字段中的所有控制字符

Vba 删除Access数据库中所有表中所有字符串字段中的所有控制字符,vba,csv,ms-access,foreach,control-characters,Vba,Csv,Ms Access,Foreach,Control Characters,我需要清理一个定期接收的Access数据库,以便它的所有表都可以导出到“干净”的CSV,然后由基本SAS通过PROC IMPORT导入 我一般不熟悉Access VBA或编程,但我尝试使用kitbash脚本循环遍历每个表中的每个字段并替换某些字符。它似乎不起作用,并且在运行时出现了几个“类型转换失败”错误 Public Sub ReplaceCharAllTables() Dim strSQL As String Dim fld As DAO.Field Dim db As DAO.Datab

我需要清理一个定期接收的Access数据库,以便它的所有表都可以导出到“干净”的CSV,然后由基本SAS通过PROC IMPORT导入

我一般不熟悉Access VBA或编程,但我尝试使用kitbash脚本循环遍历每个表中的每个字段并替换某些字符。它似乎不起作用,并且在运行时出现了几个“类型转换失败”错误

Public Sub ReplaceCharAllTables()

Dim strSQL As String
Dim fld As DAO.Field
Dim db As DAO.Database

Set db = CurrentDb()

Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentData



' Cycle through all tables in database
For Each obj In dbs.AllTables

    ' Cycle through all fields in the table
    For Each fld In db.TableDefs("[" & obj.Name & "]").Fields

        If fld.Type = dbText And Not IsNull(fld) Then

            strSQL = "Update [" & obj.Name & "] Set [" & fld.Name & "]= Replace([" & fld.Name & "],Chr(10),'. ')"
            DoCmd.RunSQL strSQL

            strSQL = "Update [" & obj.Name & "] Set [" & fld.Name & "]= Replace([" & fld.Name & "],Chr(13),'. ')"
            DoCmd.RunSQL strSQL

        End If

    Next

Next obj

End Sub
请注意,此特定代码当前仅尝试删除两个字符。这只是一个临时试验台

编辑2016.11.30:我只是想说安德烈的解决方案是完美的。最后我需要做一些小的调整,特别是除了文本字段之外还要查看“memo”字段,并将有用的调试信息写入文本文件,而不是大小有限的即时窗口。在一系列字符代码中循环是一种诡计

Public Sub ReplaceCharAllTables()

Dim strSQL As String
Dim fld As DAO.Field
Dim db As DAO.Database
Dim td As DAO.TableDef
Dim strFld As String
Dim arCharCodes As Variant
Dim code As Variant
Dim strFolder As String
Dim n As Integer
Dim strUpdate As String

' Get stuff setup save debug.print log file
strFolder = Application.CurrentProject.Path & "\" & Application.CurrentProject.Name & "_RemoveCharLog.txt"
n = FreeFile()
Open strFolder For Output As #n

' all charcodes to replace
arCharCodes = Array(10, 13, 44)

Set db = CurrentDb()

' Cycle through all tables in database
For Each td In db.TableDefs

    ' Ignore system tables
    If Not (td.Name Like "MSys*" Or td.Name Like "USys*") Then

        ' Cycle through all fields in the table
        For Each fld In td.Fields

            If fld.Type = dbText Or fld.Type = dbMemo Then       ' Check if field is text or memo

                ' Cycle through all character codes to remove
                For Each code In arCharCodes

                    strFld = "[" & fld.Name & "]"

                    strSQL = "UPDATE [" & td.Name & "] " & _
                             "SET " & strFld & " = Replace(" & strFld & ", Chr(" & code & "), '. ') " & _
                             "WHERE " & strFld & " LIKE '*" & Chr(code) & "*'"

                    db.Execute strSQL
                    strUpdate = "Updated " & db.RecordsAffected & " records."

                    'Start printing logs
                    Debug.Print strSQL
                    Debug.Print strUpdate

                    Print #n, strSQL
                    Print #n, strUpdate

                Next code

            End If

        Next fld

    End If

Next td

End Sub

就我所知,原则上你的代码没有问题。主要问题可能是它还试图更新所有系统表-选中导航窗格导航选项中的“系统对象”以查看它们。
它们从MSys或USys开始

还有一些需要改进的地方:

  • 您仍然需要TableDef对象,因此可以直接循环它们,而不是
    AllTables
  • 表字段不能为Null,因此不需要进行此检查
  • 为了提高效率,您希望只更新列实际包含搜索字符的行,因此我添加了where子句
  • 为避免重复代码,请将所有要替换的字符代码放入一个数组中,以进行额外循环
  • 使用
    db.Execute
    而不是
    DoCmd.RunSQL
    :它避免了需要
    DoCmd.SetWarnings False
    ,并提供受影响记录的数量
我的建议是:

Public子ReplaceCharAllTables()
作为字符串的Dim strSQL
模糊fld为刀场
Dim数据库作为DAO.Database
将td设置为DAO.TableDef
Dim strFld As字符串
作为变体的暗古码
变码
'要更换的所有字符代码
古代码=数组(10,13)
Set db=CurrentDb()
'循环浏览数据库中的所有表
对于每个td,单位为db.TableDefs
'忽略系统表
如果不是(td.Name如“MSys*”或td.Name如“USys*”),则
'循环表中的所有字段
对于td.字段中的每个fld
如果fld.Type=dbText,则
对于古码中的每个代码
strFld=“[”&fld.Name&“]
strSQL=“UPDATE[”&td.Name&“]”和_
“SET”&strFld&“=Replace(&strFld&)”,Chr(&code&“,”)”和_
其中“&strFld&”类似“*”&Chr(代码)&“*”
调试.打印strSQL
执行strSQL
调试。打印“已更新”和db.RecordsAffected&“记录”
下一个代码
如果结束
下一个fld
如果结束
下一个td
端接头

如果仍然出现错误,请检查特定的SQL(Ctrl+g显示
Debug.Print
)的输出-它要更新的列数据类型是什么?

是的,我猜
和Not IsNull(fld),然后
而不是不更新具有空值的字段,导致了错误。很酷,感谢您的反馈!