工作code导入超过255个字段制表符分隔txt文件分为两种访问表 [英] Working code to import tab delimited txt file with more than 255 fields into two Access Tables

查看:418
本文介绍了工作code导入超过255个字段制表符分隔txt文件分为两种访问表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code以下将进口超过255个字段制表符分隔文件转换成两个表。只要确保当你设计所有的领域都为字段正确的数据类型被导入了两个表。我最初是通过使用Access导入文本文件向导创建我的表。使用向导前我删除了255场后,创建了第一个表,然后删除了前255个,创建第​​二个表。希望这可以帮助别人,并感谢大家在下面谁帮我这个项目。

公用Sub ImportTextFile()    使用ADODB.Recordset,确保你有一个基准设置为ADO    昏暗首先作为ADODb.Recordset    昏暗RST2作为ADODb.Recordset    昏暗strFile作为字符串    昏暗strInput作为字符串    昏暗varSplit为Variant    昏暗intCount作为整数    首先设置=新ADODb.Recordset    设置RST2 =新ADODb.Recordset    更改表名称这里    rst.OpenAppsImport1,CurrentProject.Connection,adOpenDynamic,ADLOCKOPTIMISTIC    rst2.OpenAppsImport2,CurrentProject.Connection,adOpenDynamic,ADLOCKOPTIMISTIC    更改文本文件名和位置HERE    strFile =G:\首页\ RiskMgtReports \ AutoDatabase \ CreditAppExtract.txt    打开strFile输入#1    昏暗我作为整数    昏暗的N当    N = DMAX(index_number,fullextract_hist)    难道直到EOF(1)        这个计数器只是为了获得适用的线导入前        intCount = intCount + 1        逐行读取文本文件中的行        线路输入#1,strInput        开始导入在第二行。改变数以匹配其行,你        要开始导入        如果intCount> = 2那么        N = N + 1            创建使用分割功能的一维数组            varSplit =斯普利特(strInput,vbTab,vbBinaryCompare)            添加记录            随着RST                。添新                点域(0)= N                 对于i = 1到137                     如果NZ(varSplit(I - 1),)=然后,                     点域(ⅰ)=空                     其他                     如果左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=扬M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=二月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=三月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=四月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=五月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=君M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=七月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=八月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=九月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=十月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=十一月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=十二月M然后                     点域(我)= CDATE(格式(varSplit(I - 1),MM / DD / YYYY))                     其他                     点域(ⅰ)= varSplit(ⅰ - 1)                     结束如果                     结束如果                 接下来我                .Update                .MoveNext我不认为你应该需要这个            结束与            随着RST2                 。添新                 点域(0)= N                 点域(1)= varSplit(0)                 对于i = 138〜274                     如果NZ(varSplit(I - 1),)=然后,                     点域(I - 136)= NULL                     其他                     如果左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=扬M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=二月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=三月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=四月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=五月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=君M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=七月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=八月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=九月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=十月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=十一月M或左(varSplit(I - 1),4)及右(修剪(varSplit(I - 1)),1)=十二月M然后                     点域(I - 136)= CDATE(格式(varSplit(I - 1),MM / DD / YYYY))                     其他                     点域(I - 136)= varSplit(I - 1)                     结束如果                     结束如果                 接下来我                 .Update             结束与        结束如果    循环    ' 垃圾收集    关闭#1    rst.Close    第一个设置=什么    rst2.Close    设置RST2 =什么 结束小组

解决方案

我承认,你想在这里做什么已经是不太理想。我不经常用在需要这么多领域数据的工作。

这里的解决方案基本上是管理两个不同的Recordset对象。

公用Sub ImportTextFile()    使用ADODB.Recordset,确保你有一个基准设置为ADO    昏暗首先作为ADODb.Recordset    昏暗RST2作为ADODb.Recordset    昏暗strFile作为字符串    昏暗strInput作为字符串    昏暗varSplit为Variant    昏暗intCount作为整数    首先设置=新ADODb.Recordset    设置RST2 =新ADODb.Recordset    更改表名称这里    rst.OpenImporttabledata,CurrentProject.Connection,adOpenDynamic,ADLOCKOPTIMISTIC    rst2.Openimporttabledata2,CurrentProject.Connection,adOpenDynamic,ADLOCKOPTIMISTIC    更改文本文件名和位置HERE    strFile =G:\首页\ RiskMgtReports \ AutoDatabase \ fullextract.txt    打开strFile输入#1    昏暗我作为整型    难道直到EOF(1)        这个计数器只是为了获得适用的线导入前        intCount = intCount + 1        逐行读取文本文件中的行        线路输入#1,strInput        开始导入在第二行。改变数以匹配其行,你        要开始导入        如果intCount> = 256然后            创建使用分割功能的一维数组            varSplit =斯普利特(strInput,vbTab,vbBinaryCompare)            添加记录            随着RST                。添新                 对于i = 1至255                     点域(ⅰ)= varSplit第(i-1)                 接下来我                .Update                .MoveNext我不认为你应该需要这个            结束与            随着RST2                 。添新                 对于i = 256到UBound函数(varSplit)                     点域(ⅰ)= varSplit第(i-1)                 接下来我                 .Update             结束与        结束如果    循环    ' 垃圾收集    关闭#1    rst.Close    第一个设置=什么    rst2.Close    设置RST2 =什么 结束小组

This code below will import a tab delimited file with over 255 fields into two tables. Just make sure when you design your two tables all your fields have the correct data types for the fields being imported. I originally created my tables by using Access import text file wizard. Before using the wizard I deleted the fields after 255 to create the first table and then deleted the first 255 to create the second table. Hopes this helps someone and thanks to everyone below who helped me with this project.

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

   Set rst = New ADODb.Recordset
   Set rst2 = New ADODb.Recordset
   ' CHANGE THE TABLE NAME HERE
   rst.Open "AppsImport1", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
   rst2.Open "AppsImport2", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
   ' CHANGE THE TEXT FILE NAME AND LOCATION HERE
   strFile = "G:\Home\RiskMgtReports\AutoDatabase\CreditAppExtract.txt"

   Open strFile For Input As #1

   Dim i As Integer
   Dim n As Long

   n = DMax("index_number", "fullextract_hist")

   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
       n = n + 1
           ' creates a single dimension array using the split function
           varSplit = Split(strInput, vbTab, , vbBinaryCompare)
           ' adds the record
           With rst
               .AddNew
               .Fields(0) = n
                For i = 1 To 137
                    If Nz(varSplit(i - 1), "") = "" Then
                    .Fields(i) = Null
                    Else
                    If Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Jan M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Feb M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Mar M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Apr M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "May M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Jun M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Jul M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Aug M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Sep M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Oct M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Nov M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Dec M" Then
                    .Fields(i) = CDate(Format(varSplit(i - 1), "mm/dd/yyyy"))
                    Else
                    .Fields(i) = varSplit(i - 1)
                    End If
                    End If
                Next i
               .Update
               '.MoveNext 'I don't think you should need this
           End With
           With rst2
                .AddNew
                .Fields(0) = n
                .Fields(1) = varSplit(0)
                For i = 138 To 274
                    If Nz(varSplit(i - 1), "") = "" Then
                    .Fields(i - 136) = Null
                    Else
                    If Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Jan M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Feb M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Mar M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Apr M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "May M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Jun M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Jul M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Aug M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Sep M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Oct M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Nov M" Or Left(varSplit(i - 1), 4) & Right(Trim(varSplit(i - 1)), 1) = "Dec M" Then
                    .Fields(i - 136) = CDate(Format(varSplit(i - 1), "mm/dd/yyyy"))
                    Else
                    .Fields(i - 136) = varSplit(i - 1)
                    End If
                    End If
                Next i
                .Update
            End With
       End If
   Loop
   ' garbage collection
   Close #1
   rst.Close
   Set rst = Nothing
   rst2.Close
   Set rst2 = Nothing

End Sub

解决方案

I admit that what you're trying to do here is already less than ideal. I don't often work with data where this many fields are needed.

The solution here is basically to manage two different recordset objects.

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

   Set rst = New ADODb.Recordset
   Set rst2 = New ADODb.Recordset
   ' CHANGE THE TABLE NAME HERE
   rst.Open "Importtabledata", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
   rst2.Open "importtabledata2", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
   ' CHANGE THE TEXT FILE NAME AND LOCATION HERE
   strFile = "G:\Home\RiskMgtReports\AutoDatabase\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 >= 256 Then
           ' creates a single dimension array using the split function
           varSplit = Split(strInput, vbTab, , vbBinaryCompare)
           ' adds the record
           With rst
               .AddNew
                For i = 1 to 255
                    .Fields(i) = varSplit(i-1)
                Next i
               .Update
               '.MoveNext 'I don't think you should need this
           End With
           With rst2
                .AddNew
                For i = 256 to UBound(varSplit)
                    .Fields(i) = varSplit(i-1)
                Next i
                .Update
            End With
       End If
   Loop
   ' garbage collection
   Close #1
   rst.Close
   Set rst = Nothing
   rst2.Close
   Set rst2 = Nothing

End Sub

这篇关于工作code导入超过255个字段制表符分隔txt文件分为两种访问表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆