使用'线路输入#1'时的字符限制

时间:2014-11-24 17:00:00

标签: vba import access-vba

我正在使用Microsoft Access 2002数据库。我有一个文本文件(来自另一个组织),我使用'DoCmd.TransferText'导入数据库,没有任何重大问题。我每个月都会得到一个新文件,偶尔提供文件的外部组织会更改内容,顺序和/或列数(作为他们自己正在进行的开发的一部分,而不是随机的 - 我假设/希望)。

我目前正在尝试向我的VBA进程添加一些代码,该代码将在将文件导入数据库之前检查文件的布局,并警告我是否已更改。我想我可以通过使用'Line Input#1'将列标题导入到一个字符串中,然后我可以将其与预期的列标题进行比较。

我遇到的问题是我觉得我需要比较的字符数是2,568但是当我使用'Line Input#1'时我似乎只得到前1023个字符,所以比较总是失败。无论如何我可以增加/绕过限制,或者可能导入下一个1023个字符并将它们组合成一个字符串?

P.S。有问题的文件不包含任何回车符,只包含换行符,所以虽然它导入好了,但是'Line Input#1'方法会尝试加载整个文件(仅在理论上,实际上它显然只做1023这就是为什么我在声明变量时将字符串长度设置为2,568(这样它就会忽略文件的其余部分)。

到目前为止,这是我的代码:

Sub FileImport()

Dim db As Database
Dim rstFiles As Recordset
Dim strFileHeaders As String * 2568 'Set Fixed length as we only want to check the first 2,568 characters of the file to confirm layout
Dim strExpectedHeaders As String * 2568

strExpectedHeaders = ";;Totals;;;Fixed Charge - Metered Water;;;;;;Volumetric Water Charge - Metered;;;Fixed Charge - Metered Waste Water;;;;;;" & _
    "Volumetric Waste Water Charge - Metered;;;Property Drainage Charge (p/£RV);;;;;;Roads Drainage Charge (p/£RV);;;;;;" & _
    "Non Domestic Metered Wastewater Fixed Charge;;;;;;Non Domestic Metered Wastewater Volume Charge;;;;;;Fixed Charge - Un-metered Water;;;;;;" & _
    "Water RV Charge - Un-metered;;;;;;Fixed Charge - Un-metered Waste Water;;;;;;Volumetric Waste Water Charge - Un-metered;;;;;;" & _
    "Supply Contract Discount (Water);;;;;;Supply Contract Discount (Waste);;;;;;Direct Debit Discount;;;;;;Water Management Charge;;;;;;;Meter Read History;;;;" & Chr(10) & _
    "Customer reference;Date posted;Bill no;Meter serial no.;From date;To date;Meter size (actual);Meter size (billed);" & _
    "Property address;Billing address;Rateable value;Your ref;PURN;Water SPID;Waste water SPID;% Return to sewer;Net total;" & _
    "VAT amount;Total amount;Net amount;VAT amount;Total amount;Days charged;Unit cost;Consumption;Net amount;VAT amount;" & _
    "Total amount;Days charged;Unit cost 1;Consumption 1;Unit cost 2;Consumption 2;Unit cost 3;Consumption 3;Unit cost 4;" & _
    "Consumption 4;Net amount;VAT amount;Total amount;Days charged;Unit cost;Consumption;Net amount;VAT amount;" & _
    "Total amount;Days charged;Unit cost 1;Consumption 1;Unit cost 2;Consumption 2;Unit cost 3;Consumption 3;Unit cost 4;" & _
    "Consumption 4;Net amount;VAT amount;Total amount;Days charged;Unit cost;Consumption;Net amount;VAT amount;Total amount;" & _
    "Days charged;Unit cost;Consumption;Net amount;VAT amount;Total amount;Days charged;Unit cost;Consumption;Net amount;" & _
    "VAT amount;Total amount;Days charged;Unit cost;Consumption;Net amount;VAT amount;Total amount;Days charged;Unit cost;" & _
    "Consumption;Net amount;VAT amount;Total amount;Days charged;Unit cost;Consumption;Net amount;VAT amount;Total amount;" & _
    "Days charged;Unit cost;Consumption;Net amount;VAT amount;Total amount;Days charged;Unit cost;Consumption;Net amount;" & _
    "VAT amount;Total amount;Days charged;Discount;Consumption;Net amount;VAT amount;Total amount;Days charged;Discount;" & _
    "Consumption;Net amount;VAT amount;Total amount;Days charged;Discount;Consumption;Net amount;VAT amount;Total amount;" & _
    "Days charged;Unit cost;Consumption;Measured site area;Meter serial number;Meter location;Current reading date;" & _
    "Current meter read method;Current 'Actual / Estimate';Current reading;Previous reading date;Previous 'Actual / Estimate';" & _
    "Previous reading;Previous reading date;Previous meter read method;Previous 'Actual / Estimate';Previous reading;Previous reading date;Previous 'Actual / Estimate';Previous reading;"


Set db = CurrentDb()

Set rstFiles = CurrentDb.OpenRecordset("SELECT [id], [filename], [path]" & _
"FROM [import file] " & _
"WHERE (((imported)=False)) ", dbOpenDynaset)

Do
    Open rstFiles![Path] & rstFiles![filename] For Input As #1
    If Not EOF(1) Then Line Input #1, strFileHeaders
    Close #1

    If strFileHeaders = strExpectedHeaders Then
        DoCmd.TransferText acImportDelim, "BS_WWW_Spec", "import - BS WWW", rstFiles![Path] & rstFiles![filename], False, ""
    Else
        db.Execute "INSERT INTO [audit] (eventsource, eventmessage) " & _
            "VALUES (""Sub FileImport"" ,""Layout of FileID " & rstFiles![ID] & " not recognized"")", dbFailOnError
    End If

    rstFiles.MoveNext

Loop Until rstFiles.EOF

rstFiles.Close

End Sub

1 个答案:

答案 0 :(得分:0)

您可以尝试使用input代替line input

Dim strFileHeaders As String * 2568

Open rstFiles![Path] & rstFiles![filename] For Input As #1
Input #1, strFileHeaders
Close #1

它适用于Access 2010,我不知道Access 2002在input上是否也有更严格的最大长度。