Hor*_*Kol 5 ms-access vba dictionary access-vba
我正在编写一个脚本,它可以提取一些患者数据并生成XML导出.
每个患者记录都有一个相关的医生 - 但不是在每个记录中重复医生的详细信息,我想我会在患者记录中设置医生ID,然后在文档底部的不同部分中包含医生列表.
我需要做的一件事是在患者记录中包含医生的GUID,但实际的数据库关系是本地非唯一ID.我认为最好的方法是使用字典将GUID映射到本地ID列表中.
无论如何,长话短说,这里是构建需求列表的位:
While Not PatientRec.EOF
Set DoctorRec = MyDB.OpenRecordset("Select Lng_Key, Txt_GUID From Tbl_LU_DoctorDetail Where Lng_Key = " & PatientRec![Lng_Doctor])
While Not DoctorRec.EOF
If (IsNull(DoctorRec![Txt_GUID])) Then
DoctorRec.Edit
DoctorRec![Txt_GUID] = CreateGUID()
DoctorRec.Update
End If
DoctorList.Add DoctorRec![Lng_Key], DoctorRec![Txt_GUID]
' outputs something like '5:{03f50fe1-a0a4-4733-906a-771e22845ea6}
MsgBox (DoctorRec![Lng_Key] & ":" & DoctorList.Items(DoctorRec![Lng_Key]))
DoctorRec.MoveNext
Wend
Wend
' outputs nothing!
MsgBox (DoctorList.Item(5))
' but there is something in there???
MsgBox (DoctorList.count)
Run Code Online (Sandbox Code Playgroud)
我也尝试使用CStr将id转换为字符串,但使用DoctorList.Item("5")获得相同的结果
更糟糕的是,当我尝试:
Dim v As Variant
For Each v In DoctorList.Keys
MsgBox (v & ":" & DoctorList.Item(v))
Next
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Run-time error '3420':
Object invalid or no longer set.
Run Code Online (Sandbox Code Playgroud)
测试(和帮助文件)表明Variant'v'没有被设置为Keys属性中的任何东西,但For Each至少尝试循环...
- 更新
我在vbforums上找到了类似的问题:http://www.vbforums.com/showthread.php?t = 622933
我用硬编码的密钥和项目测试:
DoctorList.Add 5, "String"
Run Code Online (Sandbox Code Playgroud)
For Each循环现在成功运行一次,但随后在第二个循环中失败并出现3420错误(即使它应该在第一个循环中停止).
发现问题 - 看起来Dictionaries很乐意使用对象作为键,因此在使用Dictionary.Add方法时,您必须显式使用记录集中字段的Value属性:
DoctorList.Add DoctorRec![Lng_Key].Value, DoctorRec![Txt_GUID].Value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1866 次 |
| 最近记录: |