Office中国论坛/Access中国论坛

标题: 帮我看看错在那里了? [打印本页]

作者: gxy1000    时间: 2011-2-26 18:11
标题: 帮我看看错在那里了?
本帖最后由 gxy1000 于 2011-2-26 18:17 编辑
  1. Private Sub Command7_Click()
  2.      '定义 connection 对象
  3.      Dim cn As ADODB.Connection
  4.      '定义 recordset 对象


  5.      Dim rs As New ADODB.Recordset
  6.      Dim username As String
  7.      Dim userpass As String
  8.      Dim sql As String
  9.      '使用access内置 connection对象
  10.      Set cn = CurrentProject.Connection
  11.      Text1.SetFocus
  12.      username = Text1.Text
  13.      Text3.SetFocus
  14.      username = Text3.Text
  15.      sql = "select*from users_1 where username =""& username &" 'and
  16.              Password = "'& userpass & '"""
  17.       rs.Open sql, cn
  18.      If rs.EOF Then
  19.            MsgBox "登录失败"
  20.            Text1.SetFocus
  21.            Text1.Text = ""
  22.            Text3.SetFocus
  23.            Text3.Text = ""
  24.     Else
  25.            DoCmd.Close
  26.            DoCmd.OpenForm "主界面"
  27.            MsgBox "登录成功"
  28.     End If
  29.     '关闭 recodset 对象和connection对象并释放内存资源
  30.     rs.Close
  31.     cn.Close
  32.     Set rs = Nothing
  33. End Sub
复制代码

作者: roych    时间: 2011-2-27 11:46
代码不是LZ自己写的吧(恕俺恶毒地这样想一下,(*^__^*) 嘻嘻……),代码贴过来时要顺手把相应的变量改过来的呀。这是我修改后的代码,请对比和你的不同之处。
  1. Private Sub Command7_Click()
  2. '定义 connection 对象
  3. Dim cn As ADODB.Connection
  4. '定义 recordset 对象
  5. Dim rs As New ADODB.Recordset
  6. Dim username As String
  7. Dim userpass As String
  8. Dim sql As String
  9. '使用access内置 connection对象
  10. Set cn = CurrentProject.Connection
  11. Text1.SetFocus
  12. username = Text1.Text
  13. Text3.SetFocus
  14. userpass = Text3.Text
  15. sql = "select*from 用户表 where 用户名 ='" & username & " 'and 密码 = '" & userpass & "'"
  16. rs.Open sql, cn
  17. If rs.EOF Then
  18. MsgBox "登录失败"
  19. Text1.SetFocus
  20. Text1.Text = ""
  21. Text3.SetFocus
  22. Text3.Text = ""
  23. Else
  24. DoCmd.Close
  25. DoCmd.OpenForm "主界面"
  26. MsgBox "登录成功"
  27. End If
  28. '关闭 recodset 对象和connection对象并释放内存资源
  29. rs.Close
  30. cn.Close
  31. Set rs = Nothing
  32. End Sub
复制代码
[attach]44941[/attach]
作者: gxy1000    时间: 2011-2-27 14:40
回复 roych 的帖子

我试试看!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3