将包含备注字段的CSV导入Microsoft Access表

时间:2013-12-08 01:26:49

标签: database excel ms-access csv export

首先我要说的是,我不是程序员,所以我不熟悉Visual Basic for Access(VBA)。我理解查询和宏,我可以自动化数据库事务,这可以让我从A t B点开始。

我希望从Microsoft Excel保存CSV文件,并每天将它们导入Microsoft Access 2003。 CSV文件是我从其他公司收到的每日Feed。

非程序员在Microsoft Access中可以做些什么来每天导入一次这个数据?

我最大的问题是备忘录字段。似乎许多导入数据的简单方法(如链接表)会将备注字段截断为前255个字符。我需要提供所有数据。

任何提示,建议或测试程序都会有所帮助。

2 个答案:

答案 0 :(得分:1)

结帐this可能会为您提供所需。

答案 1 :(得分:0)

您可以尝试下面的代码。将其粘贴到模块中并进行必要的更改以满足您的需求。设计一个表格,其中包含您需要的字段及其类型,并在下面引用它。使用将运行事件过程的命令按钮创建表单。调用您创建的模块。

Public Sub ImportTextFile()
' to use the ADODB.Recordset, be sure you have a reference set to ADO
Dim rst As ADODB.Recordset
Dim strFile As String
Dim strInput As String
Dim varSplit As Variant
Dim intCount As Integer


  Set rst = New ADODB.Recordset
' CHANGE THE TABLE NAME HERE
rst.Open "TblNameHere", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
' CHANGE THE TEXT FILE NAME AND LOCATION HERE
strFile = "C:\Desktop\fullextract.txt"

Open strFile For Input As #1

Dim i As Integer

Do Until EOF(1)
   ' This counter is just to get to the applicable line before importing
   intCount = intCount + 1
   ' reads the text file line by line
   Line Input #1, strInput
   ' starts importing on the second line.  Change the number to match which line you
   ' want to start importing from
   If intCount >= 2 Then
       ' creates a single dimension array using the split function
       varSplit = Split(strInput, ",", , vbTextCompare)
       ' adds the record
       With rst
           .AddNew
       ' change the range 0 To 254 to fit your needs. I was importing 254 fields.
            For i = 0 To 254
                .Fields(i) = varSplit(i)
           Next i
           .Update
       End With

   End If
Loop
' garbage collection
Close #1
rst.Close
Set rst = Nothing

End Sub