紧急求助!VB DataGrid连接数据库出错!
请问程序哪里错了?已引用ADO!参考书上是一样的代码就可以运行,用到我的系统里就一直出错,是不是还需要设置什么?别的没有用DataGrid的模块就能运行,不知道是不是这个...
请问程序哪里错了?已引用ADO!参考书上是一样的代码就可以运行,用到我的系统里就一直出错,是不是还需要设置什么?别的没有用DataGrid的模块就能运行,不知道是不是这个的原因?我能怎么改改?
部分代码如下:
Option Explicit
Dim rs As ADODB.Recordset
Dim SQL As String
Dim msg As String
Dim Index As Integer
Private Sub Form_Load()
Call LoadData '装载数据
End Sub
Private Sub LoadData()
'装载数据
Dim colName As String '保存所选择的查询条件
Dim colValue As String '保存所要查询具体的值
Dim strWhere As String
'得到查询条件
colName = CboQuery.Text '取出条件的值
colValue = txtQuery.Text '取出所要查询的值
'构建条件
strWhere = " where " & colName & " like '%" & colValue & "%'"
Set rs = Nothing
'查询供应商资料
SQL = "SELECT * FROM jiucai " & strWhere & " ORDER BY foodID"
Set rs = SelectSQL(SQL, msg)
Set Me.DataGrid1.DataSource = rs
DataGrid1.Refresh
If rs.RecordCount = 0 Then 【此处运行时显示对象变量或with块变量未设置】
'编辑控件可用性
CmdAdd.Enabled = True: CmdModify.Enabled = False: CmdDelete.Enabled = False
CmdCancel.Enabled = False: CmdSave.Enabled = False
'移动控件不可用
For Index = 0 To 3
CmdMove(Index).Enabled = False
Next Index
Else
'编辑控件可用性
CmdAdd.Enabled = True: CmdModify.Enabled = True: CmdDelete.Enabled = True
CmdCancel.Enabled = False: CmdSave.Enabled = False
'移动控件可用
For Index = 0 To 3
CmdMove(Index).Enabled = True
Next Index
End If
'设置控件Enable值
Call ControlEnable(False)
End Sub
Private Sub ControlEnable(flag As Boolean)
'设置控件的Enable值
For Index = 0 To 7
Me.txt(Index).Enabled = flag
Next Index
Me.CboCategory.Enabled = flag
End Sub
定义的公共模块为:
Public Function SelectSQL(ByVal SQL As String, ByRef msg As String) As ADODB.Recordset
'执行SQL语句,返回ADODB.Recordset
Dim Conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
'出错处理
On Error GoTo ErrorHandle
'判断SQL语句
sTokens = Split(SQL)
If InStr("SELECT", UCase((sTokens(0)))) Then
'打开数据库连接
If OpenConn(Conn) Then
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
Set SelectSQL = rst '返回函数值
msg = "查询到" & rst.RecordCount & " 条记录! "
End If
Else
msg = "SQL语句有误:" & SQL
End If
Finally_Exit: '程序结束的时候进行对象销毁工作
Set rst = Nothing
Set Conn = Nothing
Exit Function
ErrorHandle: '如果SQL语句执行出错,则提示出错信息并转到Finally_Exit
MsgString = "查询错误: " & Err.Description
Resume Finally_Exit
End Function 展开
部分代码如下:
Option Explicit
Dim rs As ADODB.Recordset
Dim SQL As String
Dim msg As String
Dim Index As Integer
Private Sub Form_Load()
Call LoadData '装载数据
End Sub
Private Sub LoadData()
'装载数据
Dim colName As String '保存所选择的查询条件
Dim colValue As String '保存所要查询具体的值
Dim strWhere As String
'得到查询条件
colName = CboQuery.Text '取出条件的值
colValue = txtQuery.Text '取出所要查询的值
'构建条件
strWhere = " where " & colName & " like '%" & colValue & "%'"
Set rs = Nothing
'查询供应商资料
SQL = "SELECT * FROM jiucai " & strWhere & " ORDER BY foodID"
Set rs = SelectSQL(SQL, msg)
Set Me.DataGrid1.DataSource = rs
DataGrid1.Refresh
If rs.RecordCount = 0 Then 【此处运行时显示对象变量或with块变量未设置】
'编辑控件可用性
CmdAdd.Enabled = True: CmdModify.Enabled = False: CmdDelete.Enabled = False
CmdCancel.Enabled = False: CmdSave.Enabled = False
'移动控件不可用
For Index = 0 To 3
CmdMove(Index).Enabled = False
Next Index
Else
'编辑控件可用性
CmdAdd.Enabled = True: CmdModify.Enabled = True: CmdDelete.Enabled = True
CmdCancel.Enabled = False: CmdSave.Enabled = False
'移动控件可用
For Index = 0 To 3
CmdMove(Index).Enabled = True
Next Index
End If
'设置控件Enable值
Call ControlEnable(False)
End Sub
Private Sub ControlEnable(flag As Boolean)
'设置控件的Enable值
For Index = 0 To 7
Me.txt(Index).Enabled = flag
Next Index
Me.CboCategory.Enabled = flag
End Sub
定义的公共模块为:
Public Function SelectSQL(ByVal SQL As String, ByRef msg As String) As ADODB.Recordset
'执行SQL语句,返回ADODB.Recordset
Dim Conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
'出错处理
On Error GoTo ErrorHandle
'判断SQL语句
sTokens = Split(SQL)
If InStr("SELECT", UCase((sTokens(0)))) Then
'打开数据库连接
If OpenConn(Conn) Then
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
Set SelectSQL = rst '返回函数值
msg = "查询到" & rst.RecordCount & " 条记录! "
End If
Else
msg = "SQL语句有误:" & SQL
End If
Finally_Exit: '程序结束的时候进行对象销毁工作
Set rst = Nothing
Set Conn = Nothing
Exit Function
ErrorHandle: '如果SQL语句执行出错,则提示出错信息并转到Finally_Exit
MsgString = "查询错误: " & Err.Description
Resume Finally_Exit
End Function 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询