北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |
如何将SQL查询语句直接写入VBA代码,而不用建立辅助的查询表???
查询:QryPbZone
QryPbZone:如何将此SQL查询语句直接写入VBA代码,无需借用此查询表
SELECT DISTINCT Left([PlID],5) AS 区域ID,
DLookUp("PzName","PbZone","PzID='" & Right([区域ID],3) & "'") AS 区域名称,
DLookUp("PtID","PbType","Right([PtID],2)='" & Left([区域ID],2) & "'") AS 属性ID,
DLookUp("PtName","PbType","Right([PtID],2)='" & Left([区域ID],2) & "'") AS 属类名称
FROM PjList
WHERE (((Left([PlID],5))<>"0"))
ORDER BY Left([PlID],5);
Private Sub TreeLoad()
Dim nodeindex As Node
Dim strSQL as String
Dim Rec As New ADODB.Recordset
Dim i As Integer
'设置爷
Set nodeindex = TVInput.Nodes.Add(, , "爷", "项目列表(List)", "K1", "K2")
nodeindex.Sorted = False
'设置父
Rec.Open "SELECT * FROM PbType ORDER BY PtID;", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For i = 0 To Rec.RecordCount - 1
Set nodeindex = TVInput.Nodes.Add("爷", tvwChild, "父" & Rec.Fields("PtID"), Rec.Fields("PtName"), "K3", "K4")
nodeindex.Sorted = False
Rec.MoveNext
Next i
Rec.Close
'设置子
'* ---------------------------
strSQL="查询QryPbZone的SQL语句":如何利用字符串变量strSQL,将SQL语句直接写入VBA代码,让Rec.Open"strSQL"
Rec.Open "QryPbZone", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For i = 0 To Rec.RecordCount - 1
Set nodeindex = TVInput.Nodes.Add("父" & Rec.Fields("属性ID"), tvwChild, "子" & Rec.Fields("区域ID"), Rec.Fields("区域名称"), "K5", "K6")
nodeindex.Sorted = False
Rec.MoveNext
Next
Rec.Close
'设置孙
'* ---------------------------
Rec.Open "QryPjList", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For i = 0 To Rec.RecordCount - 1
Set nodeindex = TVInput.Nodes.Add("子" & Left(Rec.Fields("PlID"), 5), tvwChild, "孙" & Rec.Fields("PlID"), Rec.Fields("PlName"), "K7", "K8")
nodeindex.Sorted = False
Rec.MoveNext
Next
Rec.Close
End Sub