1、FORM1 Me.frmPrivate Sub Form1_Load()Option ExplicitPublic LoginSucceeded As Boolean引用 Microsoft ActiveX Data Objects 2.6 Library定义连接对象和记录集Dim conn As ADODB.ConnectionDim rs As ADODB.Recordset对取消按钮单击事件的响应Private Sub cmdCancel_Click()设置全局变量为 false不提示失败的登录LoginSucceeded = FalseMe.HideEnd Sub对登录按钮单击事件的响
2、应Private Sub cmdOK_Click()Set conn = New ADODB.ConnectionSet rs = New ADODB.Recordset建立无源数据库连接conn.ConnectionString = “driver=sql server;server=Dell-181;uid=sa;pwd=;Database=人事系统管理“conn.ConnectionTimeout = 50conn.OpenDim str As String连接连接对象Set rs.ActiveConnection = conn设置游标类型rs.CursorType = adOpenDy
3、namicIf Trim(txtUserName.Text = “) ThenMsgBox “用户名不能为空,请重新输入用户名!“, vbOKOnly + vbExclamation, “警告“txtUserName.SetFocusElse设置查询字符串str = “select * from login_user where name =“ server=Dell-181;uid=sa;pwd=sa;database=人事系统管理“conn.OpenEnd SubPublic Sub Main()PublicStr = “Provider=SQLOLEDB.1;Integrated Sec
4、urity=SSPI;Persist Security Info=False;Initial Catalog=人事系统管理;Data Source=DELL-181“Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=人事管理系统 ;Data Source=“ Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=人事系统管理;Data Source=DELL-181“On Error
5、GoTo Error_Docnn.Open PublicStrsTokens() = Split(SQL)If InStr(“INSERT,DELETE,UPDATE“, UCase(sTokens(0) Thencnn.Execute SQLElseSet rst = New ADODB.RecordsetWith rst.ActiveConnection = cnn.CursorLocation = adUseClient.CursorType = adOpenStatic.LockType = adLockOptimistic.Open Trim(SQL)End Withrst.Open
6、 Trim(SQL), cnn, adOpenStatic, adLockOptimisticSet ExecuteSQL = rstEnd IfSet cnn = NothingSet rst = NothingExit FunctionError_Do:Dim Err_Str As StringErr_Str = “服务器“ & Err.DescriptionIf MsgBox(Err_Str, vbRetryCancel + vbCritical, “警告“) = vbRetry ThenResumeElseSet cnn = NothingSet rst = NothingEndEnd IfEnd Function