求大神带
vb代码:
Public Function ExeSQL(ByVal sql As String) As ADODB.Recordset
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnection As String
Dim strArray() As String
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo Errhandler:
strConnection = "Provider = SQLOLEDB.1;Integrated Security =SSPI;Persist Security Info=False;Initial Catalog=pos;Data Source=127.0.0.1"
strArray = Split(sql)
cn.Open strConnection
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(sql), cn, adOpenKeyset, adLockOptimistic
Set ExeSQL = rs
Else
cn.Execute sql
End If
ExeSQL_Exit:
Set rs = Nothing
Set cn = Nothing
Exit Function
Errhandler:
MsgBox "错误号: " & Err.Number & "错误信息: " & Err.Description, vbExclamation
Resume ExeSQL_Exit
End Function
结果报错说:用户定义类型未定义,求解!
[解决办法]
On Error GoTo Errhandler:
把这句删掉,你就会停在错误的某行了, 然后看看是哪个类型没定义,定义下,找个库就行了
你这个操作数据库的 要引用 ADO控件
[解决办法]
引用 Microsoft ActiveX Data Object x.x。x.x是版本号。