各位大虾,帮帮忙,我的程序怎么在用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