标题:VB中sql查询问题
只看楼主
wangqi
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-6-15
 问题点数:0 回复次数:0 
VB中sql查询问题

各位大虾,帮帮忙,我的程序怎么在用SQL语句查询表中的数据出错?提示是:“对象变量或With快变量未设置”程序指向“Set rs = db.RunSelectSQL("SELECT * FROM schoolzone")”程序和调用模块如下:
工程中是用ADO连接数据源,使用的是ACCESS数据库
'数据模块Databases
Public SqlConn As ADODB.Connection
Public sConn As String

Public Sub OpenConn()
If SqlConn Is Nothing = True Then
'建立数据库连接对象
Set SqlConn = New ADODB.Connection
End If
If SqlConn.State <> 1 Then
'打开数据库连接
SqlConn.Open sConn
End If
End Sub


Public Sub CloseConn()
'如果数据库连接对象不为空则关闭数据库连接
If SqlConn.State = 1 Then
SqlConn.Close
End If
End Sub

Public Function RunSelectSQL(ByVal sSQLString As String) As ADODB.Recordset
Dim rs As Recordset
'打开数据库连接
Me.OpenConn
'执行SQL操作
rs.Open sSQLString, SqlConn, adOpenStatic, adLockReadOnly
Set RunSelectSQL = rs
End Function
Private Sub Class_Initialize()
sConn = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=pksystem"
End Sub

程序部分为:
Private Sub Form_Load()
Dim schoolName As Node
Dim db As Databases
Dim rs As Recordset
Dim ndnew As Node
Set schoolName = trvlist.Nodes.Add
schoolName.Text = "XX学院"
Set rs = db.RunSelectSQL("SELECT * FROM schoolzone")
While (Not rs.EOF)
Set ndnew = trvlist.Nodes.Add(schoolName, tvwChild, , rs("szname"))
Wend
db.CloseConn
End Sub

搜索更多相关主题的帖子: sql 查询 
2007-06-15 16:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-147772-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057017 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved