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
谁能说一下思路是什么,我一点都看不会,谢谢谢谢!!!!
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
谁能说一下思路是什么,我一点都看不会,谢谢谢谢!!!!
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) ' SQL语句中的第一个单词
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
'SQL语句中的第一个单词是 insert,delete,update 时用 cnn.Execute来执行
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'SQL语句中的第一个单词不是insert,delete,update 时用 rst.Open来打开
'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
instr函数的用法楼主应该知道的,如果传递来的sql语句中包含这些命令则进行写入,更新或者删除操作;如果不含有,则打开记录集进行提取数据操作
Public txtSQL As String
Public MsgText As String
If Trim(txtSQL) = "" Then
txtSQL = "select * from manrecord"
End If Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = False Then
With msgList
.Rows = 1
Do While Not mrc.EOF
.Rows = .Rows + 1
For i = 1 To mrc.Fields.Count
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
Next i
mrc.MoveNext
Loop
End With
End If
在msflexGrid里面用为什么会出错呢,说的是,对象变量或with块变量未设置
说是用mrc.EOF地方出错
ExecuteSQL_Error
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
的值。
cnn.Open ConnectString
ConnectString = "FileDSN=personnel.dsn;UID=;PWD="
我的是用Window身份验证,没有什么sa和密码,该怎么弄
MSFlexGrid的行和列都从0开始
Do While Not mrc.EOF-->Do Until mrc.EOF试试
If mrc.EOF = False Then
...
end if--->If not(mrc.EOF or mrc.bof) Then
...
end if2.window身份验证,用户不能指定 SQL Server 2000 登录 ID.
请改成:
ConnectString = "FileDSN=personnel.dsn;"试一试
不过,我没有用过window身份验证方式,不知道这样改是否有效。请参看SQl server的联机帮助.请注意:Microsoft Windows 98 操作系统不支持服务器端的信任连接 API。所以不能为在 Windows 98 上运行的 SQL Server 实例指定 Windows 身份验证模式。
sTokens = Split(SQL) ' SQL语句中的第一个单词
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
说是下标越界,为什么?
那个Execute是干什么用的?谢了.
对象变量或with块变量未设置?
像这样的问题一般会出现在
当你未用NEW 关键字对你所定义的记录集进行定义。