Public Function viewData(ByVal txtSQL As String, dataGridAll As DataGrid) As IntegerDim rstData As ADODB.RecordsetDim result As String'检索需要的信息result = ExecuteSQL(txtSQL, rstData, False)'设置datagrid的数据源If rstData.RecordCount <> 0 ThenSet dataGridAll.DataSource = rstDataviewData = rstData.RecordCountElseMsgBox "还没有数据!", vbOKOnly + vbExclamation, "警告"viewData = 0End IfEnd Function数据库采用sql2000'函数名:ExecuteSQL'说 明:执行SQL语句'参 数:SQL As String, rst As ADODB.Recordset, Optional enableWrite As Boolean'返回值:SQL语句执行成功——true,失败——false' ******************************************************************************Public Function ExecuteSQL(ByVal SQL As String, rst As ADODB.Recordset, _Optional enableWrite As Boolean = True) As BooleanDim con As New ADODB.ConnectionDim sTokens() As StringOn Error GoTo Execute_ErrorsTokens = Split(SQL)Set con = New ADODB.Connectioncon.Open ConnectStringSet rst = New ADODB.RecordsetIf enableWrite Thenrst.Open Trim$(SQL), con, adOpenStatic, adLockOptimisticElserst.Open Trim$(SQL), con, adOpenStatic, adLockReadOnlyEnd IfExecuteSQL = TrueExit FunctionExecute_Error:ExecuteSQL = FalseExit FunctionEnd Function
1 回答
四季花海
TA贡献1811条经验 获得超5个赞
ExecuteSQL 这个函数贴出来,估计是数据库没链接上
Public Function viewData(ByVal txtSQL As String, dataGridAll As DataGrid) As Integer
Dim rstData As ADODB.Recordset
Dim result As String
'检索需要的信息
result = ExecuteSQL(txtSQL, rstData, False)
if result=true then
'设置datagrid的数据源
If rstData.RecordCount <> 0 Then
Set dataGridAll.DataSource = rstData
viewData = rstData.RecordCount
Else
MsgBox "还没有数据!", vbOKOnly + vbExclamation, "警告"
viewData = 0
End If
else
msgbox "数据库链接失败"
end if
End Function
添加回答
举报
0/150
提交
取消