我写了一个简单的登录系统
代码如下
Private Sub cmdok_Click()
'检验是否输入用户名
If Trim(txtname) = "" Then
   MsgBox "请输入用户名!", vbExclamation, "登录验证"
   txtname = ""
   txtname.SetFocus
   Exit Sub
End If
'检验是否输入登录口令
If Trim(txtpwd) = "" Then
   MsgBox "请输入登录口令!", vbExclamation, "登录验证"
   txtpwd = ""
   txtpwd.SetFocus
   Exit Sub
End If'根据用户身份创建用于检验用户名和密码
Dim objlog As New Recordset
Select Case cmbtype
   Case "餐饮服务员"
       Set objlog = objcyfwy.Clone
   Case "前台服务员"
       Set objlog = objqtfwy.Clone
   Case "管理员"
       Set objlog = objadmin.Clone
End SelectDim strpwdfield As String  
With objlog                      '检验用户名和密码的合法性
    If .RecordCount > 0 Then
       .MoveFirst
       If cmbtype = "管理员" Then
          .Find "用户名='" & Trim(txtname) & " '"
          strpwdfield = "密码"
       ElseIf cmbtype = "餐饮服务员" Then
          .Find "餐饮服务员名= '" & Trim(txtname) & " '"
          strpwdfield = "餐饮服务员编号"
       ElseIf cmbtype = "前台服务员" Then
          .Find "前台服务员名 = '" & Trim(txtname) & " '"
          strpwdfield = "前台服务员编号 "
       End If
       
       
       
       If .EOF Then
          MsgBox "用户输入错误!", vbCritical, "登录验证"
          txtname.SetFocus
          txtname.SelStart = 0
          txtname.SelLength = Len(txtname)
       
       ElseIf .Fields(strpwdfield) <> Trim(txtpwd) Then
          MsgBox "口令错误!", vbCritical, "登录验证"
          txtpwd.SetFocus
          txtpwd = ""
       Else
         '保存当前用户信息
        currentusername = Trim(txtname)
        currentuserpwd = Trim(txtpwd)
        currentusertype = cmbtype     
   End If
End If
  End With
  Set objlog = Nothing
End Sub
它每次都在  ElseIf .Fields(strpwdfield) <> Trim(txtpwd) Then这句话的时候错误
这是怎么回事???应该怎么改???

解决方案 »

  1.   

    ElseIf objlog(strpwdfield).value <> Trim(txtpwd) Then
      

  2.   

    ElseIf objlog(strpwdfield).value <> Trim(txtpwd) Then
      

  3.   

    ElseIf trim(objlog(strpwdfield).value) <> Trim(txtpwd) Then
      

  4.   

    全程序为
    Option Explicit
    Const maxlogtimes As Integer = 3   '定义允许登录的最大次数
    Dim objadmin As Recordset          '保存数据库中的管理员账户纪录
    Dim objqtfwy As Recordset           '保存数据库中的前台服务员的账户纪录
    Dim objcyfwy As Recordset           '保存数据库中的餐饮服务员的账户纪录Private Sub cmdcancel_Click()
    If MsgBox("你确定要退出登录???" & vbCrLf _
              & "是否真的退出???", vbYesNo + vbQuestion, "登录验证") = vbYes Then
              Unload Me
              End If
    End Sub
    Private Sub Form_Load()cmbtype.AddItem "前台服务员"
    cmbtype.AddItem "餐饮服务员"
    cmbtype.AddItem "管理员"
    cmbtype.ListIndex = 0 '设置默认身份Call init
    '访问数据库获得管理员信息
    Set objadmin = New Recordset
    With objadmin
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .Open "select * from 管理员", objcn
        Set .ActiveConnection = Nothing           '断开与数据库的连接
    End With
        '访问数据库获得前台服务员信息
    Set objqtfwy = New Recordset
    With objqtfwy
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .Open "select * from 前台服务员", objcn
        Set .ActiveConnection = Nothing          '断开与数据库的连接
    End With
     
     
     '访问数据库获得餐饮服务员信息
    Set objcyfwy = New Recordset
    With objcyfwy
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .Open "select * from 餐饮服务员", objcn
        Set .ActiveConnection = Nothing           '断开与数据库的连接
    End Withobjcn.Close                                '关闭数据库连接
    Set objcn = Nothing                        '释放数据库连接
    End SubPrivate Sub cmdok_Click()
    '检验是否输入用户名
    If Trim(txtname) = "" Then
       MsgBox "请输入用户名!", vbExclamation, "登录验证"
       txtname = ""
       txtname.SetFocus
       Exit Sub
    End If
    '检验是否输入登录口令
    If Trim(txtpwd) = "" Then
       MsgBox "请输入登录口令!", vbExclamation, "登录验证"
       txtpwd = ""
       txtpwd.SetFocus
       Exit Sub
    End If
    Static intlogtimes As Integer              'intlogtimes用来保存用户请求验证的次数
    intlogtimes = intlogtimes + 1
    If intlogtimes > maxlogtimes Then
         MsgBox "您已经超过允许的登录的验证次数!" & vbCr _
              & "应用程序将结束!", vbCritical, "登录验证"
    End
    End If
    '根据用户身份创建用于检验用户名和密码
    Dim objlog As New Recordset
    Select Case cmbtype
       Case "餐饮服务员"
           Set objlog = objcyfwy.Clone
       Case "前台服务员"
           Set objlog = objqtfwy.Clone
       Case "管理员"
           Set objlog = objadmin.Clone
    End SelectDim strpwdfield As String
    With objlog
        If .RecordCount > 0 Then
           .MoveFirst
           If cmbtype = "管理员" Then
              .Find "用户名='" & Trim(txtname) & " '"
              strpwdfield = "密码"
           ElseIf cmbtype = "餐饮服务员" Then
              .Find "餐饮服务员名= '" & Trim(txtname) & " '"
              strpwdfield = "餐饮服务员编号"
           ElseIf cmbtype = "前台服务员" Then
              .Find "前台服务员名 = '" & Trim(txtname) & " '"
              strpwdfield = "前台服务员编号 "
           End If
           
           
           
           If .EOF Then
              MsgBox "用户输入错误!", vbCritical, "登录验证"
              txtname.SetFocus
              txtname.SelStart = 0
              txtname.SelLength = Len(txtname)
           
         ElseIf .Fields(strpwdfield) <> Trim(txtpwd) Then
             MsgBox "口令错误!", vbCritical, "登录验证"
              txtpwd.SetFocus
              txtpwd = ""
           Else
             '保存当前用户信息
            currentusername = Trim(txtname)
            currentuserpwd = Trim(txtpwd)
            currentusertype = cmbtype
           
            If cmbtype = "管理员" Then
               MsgBox "欢迎进入酒店管理系统!", vbInformation, "登录成功"
               Unload Me
               xtmain.Show
         
            ElseIf cmbtype = "前台服务员" Then
               MsgBox "欢迎进入前台服务系统!", vbInformation, "登录成功"
               Unload Me
               qtfwxt.Show
            ElseIf cmbtype = "餐饮服务员" Then
               MsgBox "欢迎进入餐饮服务系统!", vbInformation, "登录成功"
               Unload Me
               cyfwxt.Show
            End If
         
       End If
    End If
      End With
      Set objlog = Nothing
    End Sub
    Private Sub Form_Unload(Cancel As Integer) '释放对象
    Set objadmin = Nothing
    Set objqtfwy = Nothing
    Set objcyfwy = Nothing
    End Sub
      

  5.   

    你的这条语句有问题strpwdfield = "前台服务员编号 ",我猜应该没有最后的空格吧ElseIf .Fields(trim(strpwdfield)) <> Trim(txtpwd) Then
      

  6.   

    objlog(strpwdfield).value & ""
    最好加上这个 & "" 否则为null时出错
      

  7.   

    另外你这个好复杂啊 代码重复比较多
    操作员类型先从数据库中提出显示到cmbtype中
    然后客户要选择吧
    选择完了后 输入用户名和密码直接验证就可以了
    所有的操作员应该在一个表中 表中添加一个字段 操作员类型
    那样代码就好看多了