MS Access 2003/2007 - 从 VBA 中使用的 csv 文件导入数据的导入规范......验证字段名称?

发布于 2024-09-28 05:58:15 字数 359 浏览 2 评论 0原文

我有一个使用向导进行的导入,至少足以保存规范。它导入带有标题、引用文本限定符和逗号分隔的 CSV 文件。然后,我在一些由按钮单击事件触发的 vba 中使用导入规范。

以下是我想知道的一些事情:

那么,如果数据中的字段顺序混乱,导入会失败吗?如果数据不包含所有字段导入失败?如果数据中有多余字段导入失败?

那么我可以对导入的数据进行任何类型的验证,以确保字段与规范相同,它们的顺序正确(如果需要的话)等等。

这工作得很好,但我只需注意,如果数据中的某些内容不正常,它无论如何都会导入,并浪费整个时间。

另外...这不是典型的访问设置...这主要是供数据分析师团队将文件导入 mdb...没有前端。

谢谢 贾斯汀

I have an import which I made with the wizard, at least far enough just to save the specification. It imports CSV files, with headers, quote text qualifiers, and comma delimited. I then use the import specification in some vba that fires from a button click event.

Here are some things I am wondering:

So if the fields in the data are out of order the import fails? if the data does not contain all the fields the import fails? if there are extra fields in the data the import fails?

So is there any kind of validation I can do to the data getting imported th ensure that the fields are the same as the spec, they are in the right order (if they need to be), etc.

This works pretty well, but I just see that if something in the data is out of the normal it will import anyway, and throw the whole time off.

Also....this is not a typical access set up...this is mainly for a team of data analysts to import files into a mdb...there is not an front end with this.

thanks
justin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2024-10-05 05:58:15

您可以使用 ADO 记录集检查 CSV 文件。

Public Sub InspectCsvFile()
    Const cstrFolder As String = "C:\Access\webforums"
    Const cstrFile As String = "temp.csv"
    Dim i As Integer
    Dim strConnect As String
    Dim strSql As String

    'early binding requires reference, Microsoft ActiveX Data Object Library '
    'Dim cn As ADODB.Connection '
    'Dim rs As ADODB.Recordset '
    'Dim fld As ADODB.Field '
    'Set cn = New ADODB.Connection '
    'Set rs = New ADODB.Recordset '

    'late binding; no reference needed '
    Dim cn As Object
    Dim rs As Object
    Dim fld As Object
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
        cstrFolder & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
    'Debug.Print strConnect '
    cn.Open strConnect
    strSql = "SELECT * FROM " & cstrFile & ";"
    rs.Open strSql, cn
    Debug.Print "Fields.Count: " & rs.Fields.Count
    For i = 0 To rs.Fields.Count - 1
        Set fld = rs.Fields(i)
        Debug.Print i + 1, fld.Name, fld.Type
    Next i
    Set fld = Nothing
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

如果该连接字符串不适合您,请参阅文本文件的连接字符串

You can inspect the CSV file using an ADO recordset.

Public Sub InspectCsvFile()
    Const cstrFolder As String = "C:\Access\webforums"
    Const cstrFile As String = "temp.csv"
    Dim i As Integer
    Dim strConnect As String
    Dim strSql As String

    'early binding requires reference, Microsoft ActiveX Data Object Library '
    'Dim cn As ADODB.Connection '
    'Dim rs As ADODB.Recordset '
    'Dim fld As ADODB.Field '
    'Set cn = New ADODB.Connection '
    'Set rs = New ADODB.Recordset '

    'late binding; no reference needed '
    Dim cn As Object
    Dim rs As Object
    Dim fld As Object
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
        cstrFolder & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
    'Debug.Print strConnect '
    cn.Open strConnect
    strSql = "SELECT * FROM " & cstrFile & ";"
    rs.Open strSql, cn
    Debug.Print "Fields.Count: " & rs.Fields.Count
    For i = 0 To rs.Fields.Count - 1
        Set fld = rs.Fields(i)
        Debug.Print i + 1, fld.Name, fld.Type
    Next i
    Set fld = Nothing
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

If that connection string doesn't work for you, see Connection strings for Textfile

忱杏 2024-10-05 05:58:15

我会向他们提供正确格式的受保护电子表格。这种保护确保他们无法更改它。

如果导入失败,请向他们提供错误报告。

I would provide them with a protected spreadsheet in the correct format. The protection ensures that they cannot alter it.

Provide them with the error report if/when it fails to import.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文