简体   繁体   English

如何从特定表字段创建字典?

[英]How do I create a dictionary from a specific table field?

I'm writing a script that will take data from text reports generated by a legacy system and import it into a more flexible tracking database.我正在编写一个脚本,该脚本将从遗留系统生成的文本报告中获取数据,并将其导入到更灵活的跟踪数据库中。 I'm trying to figure out how to build up a dictionary from data within a table so that I can quickly filter through the imported data using the dictionary exists method and only import the data belonging to my shop.我试图弄清楚如何从表中的数据构建字典,以便我可以使用字典存在方法快速过滤导入的数据,并且只导入属于我的商店的数据。

I have a table called tblShop that contains a field called [WorkcenterID] .我有一个名为tblShop的表,其中包含一个名为[WorkcenterID]的字段。 As my script runs, it's going to be extracting data from the text report, looking at the employee's work center, checking if that employee belongs to me (by comparing the extracted work center text to the dictionary), and if true, writes the data to an import table for further processing.当我的脚本运行时,它将从文本报告中提取数据,查看员工的工作中心,检查该员工是否属于我(通过将提取的工作中心文本与字典进行比较),如果为真,则写入数据到导入表以供进一步处理。

How do I build the dictionary from the table?如何从表中构建字典?

For the curious, here's my code:对于好奇,这是我的代码:

Private Sub Command5_Click()

Dim crscd, startdt, stopdt, starttm, stoptm, bldstr, rmstr, evtid, empn, empw As String
Dim i, cd, ci, es, ee As Integer
Dim cdb As DAO.Database
Dim imt, sht As DAO.Recordset
Dim wcDict As New Scripting.Dictionary

Set cdb = CurrentDb
Set imt = cdb.OpenRecordset("tblImport", dbOpenTable)
Set sht = cdb.OpenRecordset("tblShop", dbOpenTable)
'--- grab pasted text data from form ---
strText = Me.InData.Value
'--- split text data into array so we can read line by line ---
arrlines = Split(strText, vbCrLf)
'--- reset line counters ---
i = 0
ci = -1
cd = -1
es = -1
ee = -1

For Each strline In arrlines
    '--- find location of course info ---
    If Left(strline, 17) = "COURSE  NARRATIVE" Then
        cd = i + 2
    End If
    '--- find location of course location info & event ID
    If Left(strline, 8) = "BUILDING" Then
        ci = i + 1
    End If
    '--- find where assigned employee data starts
    If Left(strline, 6) = "EMP NR" Then
        es = i + 1
    End If
    '--- find where assigned employee data ends
    If es > 0 And IsNumeric(Left(strline, 5)) = False Then
        ee = i - 1
    End If
    '--- extract course code and start/stop dates/times
    If i = cd Then
        crscd = Left(strline, 6)
        startdt = Left(Right(strline, 28), 7)
        starttm = Left(Right(strline, 20), 4)
        stopdt = Left(Right(strline, 15), 7)
        stoptm = Left(Right(strline, 7), 4)
    End If
    '--- extract building number, room number and event ID
    If i = ci Then
        bldstr = Trim(Left(strline, 13))
        rmstr = Trim(Left(Right(strline, 55), 9))
        evtid = Trim(Left(Right(strline, 46), 9))
    End If
i = i + 1
Next
'--- clear import table
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblImport"
DoCmd.SetWarnings True
'--- dump employee data to import table
For n = es To ee
    empn = Left(Left(arrlines(n), 48), 5)
    empw = Left(Right(Left(arrlines(n), 48), 11), 4)
    '--- verfiy employee belongs to us before importing data
    'If wcDict.exists(empw) = True Then
        'imt.AddNew
        'imt!EmpID = empn
        'imt!Workcenter = empw
        'imt.Update
    'End If
Next

wcDict = Nothing
imt.Close
Set imt = Nothing
sht.Close
Set sht = Nothing
cdb.Close
Set cdb = Nothing
End Sub

The solution is to use the following code to loop through the recordset to extract the required data:解决方法是使用以下代码循环记录集提取所需数据:

sht.MoveFirst
Do While Not sht.EOF
    empw = sht![WorkcenterID]
    wcDict.Add empw, vbNullString
    sht.MoveNext
Loop
empw = ""

Insert that before the pasted text data grab, uncomment the verify if block at the bottom and it'll work.插入粘贴的文本数据抓取之前,取消对验证if块在底部,它会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ms access:在表中指定日期如何在查询中创建from_date和to_date字段 - ms access: give a date in a table how do I create a from_date and to_date field in a query 如何在Access with Alter Table中创建十进制字段? - How do I create a decimal field in Access with Alter Table? 如何检查字典中的字段值,然后有条件地分配值? - How do I check if field value in dictionary and then conditionally assign values? SQL 如何检查行中是否在特定等级的表中包含该数据,以及是否在具有该字段名称的另一个表中放置了 Y - SQL how do I Check to see if row has that data in table with on a specific grade, and if it does put a Y in another table with that field name 如何为 Microsoft Access 字段设计验证规则,将输入限制为来自特定 Unicode 范围的字符? - How do I design a validation rule for a Microsoft Access field that restricts input to characters from specific Unicode ranges? 如何在MySQL中创建表别名 - How do I create a table alias in MySQL 如何在表的一个字段中按最小值分组,保留同一行中的所有值? - How do I group by min value in one field of table, keeping all the values from that same row? 如何从另一个字段中选择一个选项来填充表中的两个字段? - How do I populate two fields in a table from picking an option in a further field? 如何使用 MS Access 中的 VBA 使用另一个表中的数据更新单个记录的单个字段 - How do I update a single field of a single record with data from another table with VBA in MS Access 如何防止链接的字段和子表单在Access中编辑我的表? - How do I prevent a linked field and subform from editing my table in Access?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM