VB.net COMExeption未处理

时间:2015-09-30 06:40:49

标签: vb.net

我正在使用vb.net和MS访问创建用户登录系统。我不确定我的系统出了什么问题,并且收到错误消息"在与所请求的名称或序号相对应的集合中找不到项目"该错误出现在" User.Find(用户名)"在DO循环的第一行。这是我的代码:

Public Class Login

Dim LoginError As String ' This will tell the user what is wrong with his login

Public Function Login()
    Dim DBConn As New ADODB.Connection ' This is how we tell visual studio 
    'how to connect to our database 

    Dim User As New ADODB.Recordset 'We pass our argument through our recordset

    Dim Username As String 'This will be our "Query"
    Dim strUserDB As String 'This get sets to the email field in our database.
    Dim strPassDB As String 'Same as above just for the password

    Dim blnUserFound As Boolean 'I will be using a "DO" loop so I will use 
    'this as my condition

    DBConn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
                 "Data Source = '" & Application.StartupPath & "\UserDetails2000.mdb'")
    'The inverted comas in the dataOuce statement as itt keeps the location of your 
    'file as one string.

    User.Open("tblUserDetails", DBConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
    'This is my table           'This is my connection         'These are some settings

    blnUserFound = False
    Login = False
    Username = "User = '" & txtEmail.Text & "'" 'This tells the database to find the email field 
    'Equivilent to what was entered in the textbox

    Do
        User.Find(Username) 'This is the full statement that sends my 'Query' to the record set
        If User.BOF = False And User.EOF = False Then
            'BOF = Begining of file, EOF = End of file, it tests whether the database has 
            'reached its sentinal value, if it hasent then the username has been found, If it has,
            'the username has been found.

            strUserDB = User.Fields("Email").Value.ToString
            '"Email" is my table field. I am setting strUserDB to the username field of my table
            strPassDB = User.Fields("Password").Value.ToString

            If strUserDB <> txtEmail.Text Then
                User.MoveNext()
                'This IF statement handles different CASE usernames, Example, admin and AdMiN
                'We use this if statement to differentiate between different CASE letters
            Else
                blnUserFound = True
                If strPassDB = txtPassword.Text Then
                    User.Close()
                    DBConn.Close()
                    Return True
                Else
                    LoginError = "Invalid Password"
                    User.Close()
                    DBConn.Close()
                    Return False
                End If
            End If
        Else
            LoginError = "Invalid Username"
            User.Close()
            DBConn.Close()
            Return False

        End If
    Loop Until blnUserFound = True
    LoginError = "Invalid Username"
    User.Close()
    DBConn.Close()
    Return False


End Function

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    If Login() = True Then
        MessageBox.Show("Login Succesful", "Login Status")
    Else
        MessageBox.Show(LoginError, "Login Status")
    End If
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

验证tblUserDetails是否包含名为User的列。

在Access中,User也可能是reserved keyword,因此请尝试将Username设置为:

Username = "[User] = '" & txtEmail.Text & "'"