比如做一个登陆程序用户名,密码,当用户名或者密码不正确超过三次,自动退出程序,此段程序代码如何实现。

解决方案 »

  1.   

    用一个变量来记录登录次数:Public FailCounter As Integer
    在Form_load时为该变量作初始化。If PassWordOk Then
      '进入程序
      Unload Me
      Load FrmMain
      FrmMain.show
    Else
      FailCounter=FailCounter+1
      If FailCounter=3 Then
        Unload Me
        End
      End If
    End If
      

  2.   

    我这台电脑没有装VB,所以只能凭记忆了!以下是"伪语言":)dim n as integer
    n=0private sub command1_click()
     n=n+1
     if n<=3 then                            ' 当输入密码的次数小于3次
        if text1.password<>"123456" then     '设123456是密码
           megbox("你的密码错误",确定,取消,)   '我记不得megbox的具体格式了
           if 返回的是确定 then
              exit sub
           else
              end
           endif
        else
           '进入程序
        endif
      else
          megbox("你已经3次输错密码!你无权进入系统!")
          end
     endif
    end sub
      

  3.   


    If username<>"UrUsername" or password<>"UrPassword" then 
    Counter=Counter+1
    if Counter>=3 then
    .....other code
    unload me
    end if
    end if
    'Passed code
    ......
      

  4.   

    If PassWordOk Then
      '进入程序
      Unload Me
      Load FrmMain
      FrmMain.show
    Else
      FailCounter=FailCounter+1
      If FailCounter=3 Then
        Unload Me
        End
      End If
    End If
      

  5.   

    楼上的几位都说的不错,不过有一点没有提到,就是可以用局部静态变量,如下
    sub cmd_click()
        static Cnt as integer
        cnt = cnt+1
        if checkuserpassword then 
            msgbox "OK"
        elseif cnt>=3 then
            msgbox "Exit"
        else
            msgbox "Try Again"
        end if
    end sub
      

  6.   

    for a=0 to 2
      if 用户名真 and 密码真 then
        进入主程序
      end if
    next a
      

  7.   

    userpwd = DeCrypt(pwdtxt, uidtxt)
            loginuid = uidtxt
            DatabaseName = databasenametxt
            cn.ConnectionString = "provider=" & providercob & ";server=" & servernametxt & ";uid=" & uidtxt & ";pwd=" & userpwd & ";database=" & DatabaseName
            cn.CursorLocation = adUseClient
            cn.Open
            logsuccess = True
        End If
        Dim msg  As String
        Set snp = Nothing
        s = " SELECT  授权.*"
        s = s & " FROM 授权 "
        s = s & " where 授权.编号='" & Trim$(nametxt.Text) + "' and (管理类别=" & IIf(app_type = "系统维护", "'系统维护'", "'" & app_type & "管理'") & ")"
        snp.Open s, cn
        If snp.EOF Then
            Chk = 1
        Else
            If Not IsNull(snp("口令")) Then
                If txtpwd.Text <> snp("口令") Then
                    Chk = 2
                Else
                    If IsNull(snp("WorkAt")) Then
                        Chk = 0
                    Else
                        If snp("WorkAt") <> ComputerName Then
                            Chk = 3
                        Else
                            Chk = 0
                        End If
                    End If
                End If
            Else
                If txtpwd.Text <> "" Then
                    Chk = 2
                Else
                    If IsNull(snp("WorkAt")) Then
                        Chk = 0
                    Else
                        If snp("WorkAt") <> ComputerName Then
                            Chk = 3
                        Else
                            Chk = 0
                        End If
                    End If
                End If
            End If
        End If
        If Chk = 0 Then
            DoEvents
            '进入
            LogSuccess
            welfrm.Visible = True
            welfrm.Refresh
            Me.MousePointer = 0
            Unload Me
            Exit Sub
        ElseIf Chk = 3 Then
            msg = Chr$(13) & "该用户已在" & snp("WorkAt") & "上工作" & Chr$(13) & "不准再次登录!"
            MsgBox msg, 48
        Else
            counter = counter - 1
            If counter = 0 Then
                msg = Chr$(13) & "你不是合法用户!" & Chr$(13) & "对不起,再见!"
                MsgBox msg, 48
                cn.Close
                Set snp = Nothing
                Set cn = Nothing
                End
            ElseIf counter = 1 Then
                msg = Chr$(13) & "这是最后一次.请仔细!"
            Else
                msg = Chr$(13) & "别急,想好.请再试一次"
            End If
            If Chk = 1 Then
                MsgBox "无此编号∶" & nametxt.Text & msg, 48
            ElseIf Chk = 2 Then
                MsgBox "错误口令" & Chr$(13) & msg, 48
            End If
            txtpwd.Text = ""
        End If
    Exit Sub
    errinfo:
        viewerrinfo
        If MsgBox("是否检查数据配置信息,忽略错误,继续运行?", 4 + 32) = 6 Then
            Err = 0
            Me.MousePointer = 0
            servercmd.Value = True
        Else
            End
        End If
      

  8.   

    Private Sub text1_keypress(keyascii as integer)
     static Pword as string '定义静态变量
     static Counter integer
     static Numberoftries as integer
     Numberoftries=Numberoftries+1
     if nUmberoftries=12 then end '三次输入密码不对程序结束
     Counter=Counter+1
     Pword=pword+chr$(keyascii) '把ASCII码转换为字符
     keyascii=0
     text1.text=string$(Counter,"*")
     if Lcase$(Pword) ="abcd" then '把大写字转换为小写字母
          text1.text=""
          Pword=0
          Msgbox"口令正确,继续..."
          Count=0
          form2.show
     Elseif counter=4 then
          counter=0
          Numberoftries=0
          Pword=""
          text1.text=""
          msgbox="口令不对,请重新输入"
     END IF
    END SUB
      

  9.   

    If FailCounter=3 Then
        Unload Me
        End
      
    你还不如再加上EXIT SUB呢