斑竹,我也遇到类似的问题
Option Explicit
Public txtSQL As String
Dim mrc As New ADODB.Recordset
Private Sub rtfText_SelChange()
fMainForm.tbToolBar.Buttons("粗体").Value = IIf(rtfText.SelBold, tbrPressed, tbrUnpressed)
fMainForm.tbToolBar.Buttons("斜体").Value = IIf(rtfText.SelItalic, tbrPressed, tbrUnpressed)
fMainForm.tbToolBar.Buttons("下划线").Value = IIf(rtfText.SelUnderline, tbrPressed, tbrUnpressed)
fMainForm.tbToolBar.Buttons("左对齐").Value = IIf(rtfText.SelAlignment = rtfLeft, tbrPressed, tbrUnpressed)
fMainForm.tbToolBar.Buttons("置中").Value = IIf(rtfText.SelAlignment = rtfCenter, tbrPressed, tbrUnpressed)
fMainForm.tbToolBar.Buttons("右对齐").Value = IIf(rtfText.SelAlignment = rtfRight, tbrPressed, tbrUnpressed)
End Sub
Private Sub Command2_Click()
Unload Me
cnn.Close
End Sub
Private Sub Form_Load()
'创建Connection对象cnn,关键New用于创建新对象
Dim cnn As New ADODB.Connection
'创建Recordset对象rs1
Dim rs1 As New ADODB.Recordset
'设置连接字符串ConnectionString属性
cnn.ConnectionString = "DRIVER={SQL SERVER};SERVER=YANG-CEBD011996;UID=sa;PWD=;Database=BAM"
'打开到数据库的连接
cnn.Open
'设置查询记录集的SQL语句,从表Departments中读取记录
Dim varSource As String
varSource = "SELECT * FROM tbl_WSType"
'判断连接的状态
If cnn.State = adStateOpen Then
'设置rs1的ActiveConnection属性,指定与其关联的数据库连接
Set rs1.ActiveConnection = cnn
'设置游标类型
rs1.CursorType = adOpenStatic
'打开记录集,将从表Users中读取的结果集保存到记录集rs1中
rs1.Open varSource
'显示记录集中记录的数量
MsgBox "当前记录集中共有记录 " + Trim(rs1.RecordCount) + " 个"
'将指针移动到记录集的最后
ShowTitle
ShowData
rs1.Move rs1.RecordCount
'判断EOF属性的值
If rs1.EOF = True Then
MsgBox "已经达到记录集的结尾"
End If
'关闭记录集
rs1.Close
'关闭cnn对象
cnn.Close
End If
End Sub
Private Sub ShowData()
Dim j As Integer
Dim i As Integer
Dim MsgText As String
Dim cnn As New ADODB.Connection
'创建Recordset对象rs1
Dim rs1 As New ADODB.Recordset
'设置连接字符串ConnectionString属性
cnn.ConnectionString = "DRIVER={SQL SERVER};SERVER=YANG-CEBD011996;UID=sa;PWD=;Database=BAM"
'打开到数据库的连接
cnn.Open
txtSQL = "select * from tbl_WSType"
Set mrc = ExecuteSQL(txtSQL, MsgText)
With msglist
.Rows = 1
Do While Not mrc.EOF
.Rows = .Rows + 1
For i = 1 To mrc.Fields.Count
If Not IsNull(Trim(mrc.Fields(i - 1))) Then
Select Case mrc.Fields(i - 1).Type
Case adDBDate
.TextMatrix(.Rows - 1, i) = Format(mrc.Fields(i - 1) & "", "yyyy-mm-dd")
Case Else
.TextMatrix(.Rows - 1, i) = mrc.Fields(i - 1) & ""
End Select
End If
Next i
mrc.MoveNext
Loop
End With
mrc.Close
End Sub
Private Sub ShowTitle()
Dim i As Integer
With msglist
.Cols = 4
.TextMatrix(0, 1) = ""
.TextMatrix(0, 2) = "WSType"
.TextMatrix(0, 3) = "VirtualProcID"
'固定表头
.FixedRows = 1
'设置各列的对齐方式
'表头项居中
.FillStyle = flexFillRepeat
.Col = 0
.Row = 0
.RowSel = 1
.ColSel = .Cols - 1
.CellAlignment = 4
'设置单元大小
.ColWidth(0) = 1000
.ColWidth(1) = 0
.ColWidth(2) = 1000
.ColWidth(3) = 1000
.Row = 1
End With
End Sub
里面的程序都是我从别的地方COPY来的,摸索着前进啊,现在我的对象里有一个MSflexgrid
但是我查询的纪录不能显示出来,目前运行到Private Sub ShowTitle()里面的 Do While Not mrc.EOF
这一步就报错“ 3704 数据库对象关闭时不允许操作“。
我再把模块的数据也贴出来啊
Public cnn As New ADODB.Connection
Public rst As ADODB.Recordset
Public Function connecttoserver() As Boolean
cnn.ConnectionString = "DRIVER={SQL SERVER};SERVER=YANG-CEBD011996;UID=sa;PWD=;Database=BAM"
cnn.ConnectionTimeout = 30
cnn.Open
connecttoserver = True
End Function
Public Function disconnect() As Boolean
If cnn.State = adStateOpen Then
cnn.Close
End If
disconnect = True
End Function
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast
'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function