Public fMain Form As frm Main Public User Name As String Sub Maino Dim lOgin As New frmLogin lOgin Show vbModal If Not lOgin oK then Login Failed so exit app End lf Unload lOgin Set fmain Form= new frmMain fMain Form. Sh End sub Public Function ConnectString( As String returns a DB Connect String ConnectString="File DSN=studentinfo dsn: UID=sa PWD= End Function Public Function ExecuteSQL(By Val SQL As String, MsgString As String) As ADODB Recordset SQL:传递查询语句: MsgString:传递查循信息:自身以一个数据集对象的形式返回 executes SQL and returns Recordset Dim cnn as adodb connection 定义连接 Dim rst As adodB. Recordset Dim tOkens( As String 定义字符串 On Error GoTo Execute SQL Error 异常处理 tOkens=Split(SQL) 用Splt函数产生一个包涵各个子串的数组 Set cnn= New adodB connection 创建连接 cnn. Open Connectstring 打开连接 If InStr("INSERT, DELETE, UPDATE", UCaseS(sTokens(O)))Then 判断字符串中是否有指定内容 cnn. Execute SQL 执行查询语句 Msg String=sTokens(0)&"query successful Set rst= New adodB recordset 创建数据集对象 rst Open TrimS(SQL), cnn, adOpenKeyset, adLockOptimistic 返回查询结果 rst movelast get RecordCount Set Execute SQL=rst 返回纪录集对象
Public fMainForm As frmMain Public UserName As String 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 Function ConnectString() As String 'returns a DB ConnectString ConnectString = "FileDSN=studentinfo.dsn;UID=sa;PWD=" End Function Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) _ As ADODB.Recordset 'SQL:传递查询语句;MsgString:传递查循信息;自身以一个数据集对象的形式返回 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) ‘用 Split 函数产生一个包涵各个子串的数组 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 Execute SQL Exit Set cnn nothing Exit Function Execute SQL Error MsgString="查询错误:"& Err. Description Resume ExecuteSQL Exit End Function Public Function Testtxt(txt As String) As Boolean·判断输入内容是否为空,如果文本框内 容为空,函数将返回True,否则将返回 False If Trim(txt)="The Testtxt= False Else Testtxt True End if End Function
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 Testtxt(txt As String) As Boolean ‘判断输入内容是否为空,如果文本框内 容为空,函数将返回 True,,否则将返回 False If Trim(txt) = "" Then Testtxt = False Else Testtxt = True End If End Function