Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
'传递参数SQL传递查询语句,MsgString传递查询信息
'自身以一个数据集对象的形式返回
Dim cnn As ADODB.Connection
'定义连接
Dim rst As ADODB.Recordset
'定义字符串
Dim sTokens() As String
'异常处理
On Error GoTo ExecuteSQL_Error
'用Split函数产生一个包含各个字串的数组
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
Public Function ConnectString() As String
'返回一个数据库连接
ConnectString = "FileDSN=studentinfo.dsn;UID=s a;PWD="
End Function
Public Function Testtxt(txt As String) As Boolean
'判断输入内容是否为空
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function
Sub Main()
Dim fLogin As New frmLogin
'显示登录窗体
fLogin.Show vbModal
'判断是否授权用户
If Not fLogin.OK Then
'Login failed so exit app
End
End If
Unload fLogin
'判断是将进入系统
Set fMainForm = New FrmMain
fMainForm.Show
End Sub
Public UserName As String
这个是提示的错误,不过我是按照书本的代码敲的,不知道错哪了