VBA将日志文件导入ACCESS表 [英] VBA to import log file to ACCESS Table

查看:73
本文介绍了VBA将日志文件导入ACCESS表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

再次遇到一些问题

我有一个日志文件作为附加样本有2条记录。我正在寻找的是从MS ACCESS数据库逐行读取文件并将记录导入4个不同的ACCESS表。 table1字段名称将保留在:mark和字段值右侧的:mark。

Table2,3& 4将突出显示字段名称。

因此,在读取行并到达行时,以Tb2Fld1开头,然后需要写入下一行(最多25行,可以从Tb1Fld22值决定) table2。

同样的规则适用于Table3,同时读取行并到达行Tb3Fld1,然后下一行(最多4行可以从Tb1Fld41值决定)需要写入table3。

对于Table4,在读取行并到达Tb4Fld1行时,接下来的4行需要写入table4,这可以从Tb1Fld43值决定。

字段值假设写入表2,3,& 4在日志文件中用逗号分隔。最后一个字段Tb1Fld44是需要导入到所有表的UID。

日志文件中的每个记录将以字段名称开头Tb1Fld1并以Tb1Fld44结束。

Hope我解释得很好。期待有利的回复



Hi All
Again stuck with some issues
I have a log file as attached sample with 2 records. What I am looking is to read the file line by line from MS ACCESS Database and import the records to 4 different ACCESS tables. The table1 field names will be left of the : mark and field values are right of the : mark.
Table2, 3 & 4 will have field names as highlighted.
So while reading the lines and reaching the line starts with Tb2Fld1, then the next lines (maximum 25 which can be decided from the Tb1Fld22 value) need to be written to table2 .
Same rule applies to Table3 while reading the lines and reaching the line Tb3Fld1, then the next lines (maximum 4 which can be decided from the Tb1Fld41 value) need to be written to table3.
For Table4 also while reading the lines and reaching the line Tb4Fld1, then the next 4 lines need to be written to table4 which can be decided from the Tb1Fld43 value.
The field values suppose to be written to Table2,3, & 4 are separated by comma in the log file. The last field Tb1Fld44 is the UID which need to be imported to all the tables.
Each records in log file will start Tb1Fld1 as field name and end with Tb1Fld44.
Hope I explained well. Expecting a favorable reply

附加文件
Sample_File 01Dec2014.docx (14.0 KB,301 views)
Attached Files
Sample_File 01Dec2014.docx (14.0 KB, 301 views)

推荐答案

rajeevs,


我很确定我们可以帮助你解决这个问题,但到目前为止你尝试了什么?您是否有使用文件系统对象的经验(这可能是解决此问题的最佳方法)?


Tb1Fld1-44是否显示在文本文件中的实际名称?我认为这是非常可行的,但我们希望我们的海报至少提供解决问题的第一个基本尝试,并协助排除故障。


我很乐意帮助方式.....
rajeevs,

I''m pretty sure we can help you with this, but what have you tried so far? Have you much experience with using File System Objects (which is probably the best way to approach this)?

A re the Tb1Fld1-44 the actual names as they appear in your text file? I think this is very doable, but we expect our posters to at least provide a first basic attempt at solving their problems and we assist with troubleshooting.

I''m glad to help along the way.....


谢谢twinnyfo像往常一样快速响应。

字段名称(在示例文件中留下: )总是静态但由于日志文件的敏感性,我已经更改了日志文件中的数据并给了你。表中的字段已根据日志文件字段定义。我能理解FSO和VBA。但不是那个专家。我有一个模块,它将日志文件导入ACCESS DB,但现在日志文件结构已更改。这些更改是我坚持的table3和table4的附加行。
Thank you twinnyfo for the quick response as usual.
The field names (which are left of the : in the sample file)are always static but due to the sensitivity of the log file I have changed the data in the log file and given you. The fields in the tables are already defined based on the log file fields. I can understand FSO and VBA. But not that expert. I have a module which was importing the log file to ACCESS DB but now the log file structure has changed. The changes are additional lines for the table3 and table4 where I stuck.


嗯,这是一个如何攻击你的代码的大纲(我会帮助你解决任何问题)可能有):


使用FSO,打开文件

创建四个记录集(每个表一个 保存到

为字段1-21添加一条新记录到Table1(为此,您只需将新行读入字符串变量,得到删除:左侧的数据并将数据保存到右侧。


在tb1Fld22,获取该值(这将告诉您要添加的行数)表2.


对于每一行,向table2添加一条新记录,评估必要的数据并将其复制到表中。


然后阅读更多的行,在tb1Fld23上寻找。将这些值添加到table1中的相同记录。


在tb1Fld41,再次获取该值,将多个记录添加到表3中。

关注tbl1Fld42 - 向Table1添加更多内容。


Tbl1Fld43,再次将多行添加到Table4。


将UID添加到Table1。


注意:当我说要向这些表添加记录时,这都是使用您打开的记录集完成的。然后,确保更新记录集,并为表1中的下一条记录做好准备。


这是您应该采用的一般概念。我建议慢慢处理每个部分,调试并仔细检查每个步骤,以确保每个字段的结果都是所需的结果(即数字值作为数字导入,而不是作为文本导入)。

再次,我很高兴帮助您解决任何障碍,但您说您了解FSO和VBA。所以,虽然你不是专家,但是当我们解决这个问题时,我们会加强你的技能!
Well, here is an outline of how to attack your code (I will stand by to assist with any hang-ups you may have):

Using FSO, open the file
Create four recordsets (one for each of the tables that you will be saving to)

Add a new record to Table1 for fields 1-21 (for this, you just read a new line into a string variable, get rid of the data to the left of ":" and save the data to the right.

At tb1Fld22, get that value (which will tell you how many rows to Add to Table2.

For each of those rows, add a new record to table2, evaluate the necessary data and copy it into your table.

Then read more lines, being on the lookout for tb1Fld23. Ad those values to the same record in table1.

At tb1Fld41, again get that value, add that many records to Table 3.

Watch for tbl1Fld42--add more to Table1.

Tbl1Fld43, again add that many rows to Table4.

Add the UID to Table1.

NB: When I say to add records to those tables, this is all done with the recordsets you have opened. Then, make sure you update the recordsets, and get ready for the next record in Table 1.

This is the general concept you should take. I recommend working through each piece slowly, debugging and double-checking each step, to make sure the results for each field are the desired results (i.e. numerical values are imported as numbers, and not as text).

Again, I''m glad to help walk you through any snags, but you said you understand both FSO and VBA. So, although you are not an expert, as we work through this, we will strengthen your skills!


这篇关于VBA将日志文件导入ACCESS表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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