to: honjo(斌)
不错不错~~但好象和楼主的意思不太吻合,虽然里面包括,但不是简单明了,哈哈~
TO: 楼主
你是判断是否有数据集对吧~可以用很多种方法~
例如:if rs.bof and rs.eof then
msgbox "没有数据"
end if
或者
if rs.recordcount < 1 then
msgbox "没有数据"
end if
或者在SQL语句里面判断~就如 czwwh(江·沙·月) 说的
select count(*) from table where ...
我的代码是这样的,请高手指正
If CheckSTORAGE(Left(ProdList.Text, InStr(ProdList, "|") - 1)) Then
'库存中Update
strSql = "Update T_PRODUCT_STORAGE Set sl=sl+" & txt(1) & ",je=je+" & txt(2) & " Where ProdId=" & Left(ProdList.Text, InStr(ProdList, "|") - 1) & " And State=0"
Else
'库存中Insert
strSql = "Insert Into T_PRODUCT_STORAGE (Whid,Pc,ProdId,sl,je,State) Values(" & Left(WhList.Text, InStr(WhList.Text, "|") - 1) & "," & txt(0) & "," & Left(ProdList.Text, InStr(ProdList, "|") - 1) & "," & txt(1) & "," & txt(2) & ",0)"
End If
Set MyRs = ExSQL(strSql, strMsg)
Private Function CheckSTORAGE(ByVal Pid As Integer) As Boolean
Dim MyRs As ADODB.Recordset, strSql As String
strSql = "Select Id From T_PRODUCT_STORAGE Where Id=" & Pid
Set MyRs = ExSQL(strSql)
If Not MyRs.EOF Then CheckSTORAGE = True
MyRs.Close
End Function
Public Function ConStr() As String
ConStr = "DSN=Sale;UID=sa;PWD="
End Function
'返回一个Recordset和执行相关操作
Public Function ExSQL(ByVal SQL As String, Optional strMsg As String) As ADODB.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 ConStr
If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
strMsg = sTokens(0) & "操作成功"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'rst.MoveLast 'get RecordCount
'If Not rst.EOF Then
Set ExSQL = rst
strMsg = "查询到" & rst.RecordCount & " 条记录 "
'Else
' strMsg = "无记录"
'End If
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
strMsg = "查询错误: " & Err.Description
Resume ExecuteSQL_Exit
End Function