请问如何在DAO编程访问Access数据库中自动查询到主表的子表。

randwind 2011-03-21 07:55:20
'----------------- Type 类别 --------------------------------------------
dbTblTypeGroup = dbDatabase.CreateTableDef("Type")
With dbTblTypeGroup
dbFieldTypeGroup.dbFieldGoodsTypeIndex = .CreateField("TypeNameKey", dao.DataTypeEnum.dbLong)
dbFieldTypeGroup.dbFieldGoodsTypeIndex.Attributes = dbFieldTypeGroup.dbFieldGoodsTypeIndex.Attributes + dao.FieldAttributeEnum.dbAutoIncrField
dbFieldTypeGroup.dbFieldGoodsType_Bianma = .CreateField("Bianma", dao.DataTypeEnum.dbText, 25)
dbFieldTypeGroup.dbFieldGoodsType_Leibie = .CreateField("Leibie", dao.DataTypeEnum.dbText, 25)
End With
With dbTblTypeGroup.Fields
.Append(dbFieldTypeGroup.dbFieldGoodsTypeIndex)
.Append(dbFieldTypeGroup.dbFieldGoodsType_Bianma)
.Append(dbFieldTypeGroup.dbFieldGoodsType_Leibie)
End With

idx = dbTblTypeGroup.CreateIndex("TypeNameKey")
fldIndex = idx.CreateField("TypeNameKey", dao.DataTypeEnum.dbLong)
idx.Fields.Append(fldIndex)
idx.Primary = True
dbTblTypeGroup.Indexes.Append(idx)

dbDatabase.TableDefs.Append(dbTblTypeGroup)
dbDatabase.TableDefs.Refresh()

'----------------- Name 名称 ------------------------------------------------------------
dbTblNameGroup = dbDatabase.CreateTableDef("Name")
With dbTblNameGroup
dbFieldNameGroup.dbFieldGoodsTypeNameKey = .CreateField("TypeNameKey", dao.DataTypeEnum.dbLong)
dbFieldNameGroup.dbFieldGoodsNameIndex = .CreateField("NameSpecKey", dao.DataTypeEnum.dbLong)
dbFieldNameGroup.dbFieldGoodsNameIndex.Attributes = dbFieldNameGroup.dbFieldGoodsNameIndex.Attributes + dao.FieldAttributeEnum.dbAutoIncrField
dbFieldNameGroup.dbFieldGooddsName_Bianma = .CreateField("Bianma", dao.DataTypeEnum.dbText, 25)
dbFieldNameGroup.dbFieldGoodsName_Mingcheng = .CreateField("Mingcheng", dao.DataTypeEnum.dbText, 25)
End With
With dbTblNameGroup.Fields
.Append(dbFieldNameGroup.dbFieldGoodsTypeNameKey)
.Append(dbFieldNameGroup.dbFieldGoodsNameIndex)
.Append(dbFieldNameGroup.dbFieldGooddsName_Bianma)
.Append(dbFieldNameGroup.dbFieldGoodsName_Mingcheng)
End With

idx = dbTblNameGroup.CreateIndex("NameSpecKey")
fldIndex = idx.CreateField("NameSpecKey", dao.DataTypeEnum.dbLong)
idx.Fields.Append(fldIndex)
idx.Primary = True
dbTblNameGroup.Indexes.Append(idx)

dbDatabase.TableDefs.Append(dbTblNameGroup)
dbDatabase.TableDefs.Refresh()


relNew = dbDatabase.CreateRelation("TypeToName", dbTblTypeGroup.Name, dbTblNameGroup.Name, dao.RelationAttributeEnum.dbRelationUpdateCascade)
relNew.Fields.Append(relNew.CreateField("TypeNameKey"))
relNew.Fields("TypeNameKey").ForeignName = "TypeNameKey"
dbDatabase.Relations.Append(relNew)


代码如上。现在我想通过 TypeNameKey 值自动查询到Type表的一个关系子表的内容。能不能像ADO中一样使用child*什么的直接得到子表内容。

还有DAO中的relation定义后是如何使用的,我刚学这,不是很清除。赶紧现在我虽然定义了但是没有用到,每次我都是通过SQL层层查询到的结果。

希望各位大侠解惑。
...全文
195 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
randwind 2011-03-21
  • 打赏
  • 举报
回复
2楼能不能具体一点,或来一点代码。我看了帮助没有发现什么,可能我没有找到吧。
Alexsunmoon 2011-03-21
  • 打赏
  • 举报
回复
我也遇到同样的问题。请高手指导
孟子E章 2011-03-21
  • 打赏
  • 举报
回复
请参考
C:\Program Files\Common Files\Microsoft Shared\OFFICE11\2052\DAO360.CHM

不同的版本DAO360.CHM位置可能不同

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧