0

尝试创建用户名/密码验证表单。当我单击登录按钮即 comamand1_click() 时,出现以下错误“对象变量或未设置块变量”。当我点击调试它指向

> rs.ActiveConnection = conn

我输入的代码:

Dim a
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub Command1_Click()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\VB Project\logindetails1.mdb"
conn.Open

If conn = Null Then
    MsgBox ("no db")
    End
End If

rs.ActiveConnection = conn
rs.Source = "select * from login"
rs.Open

rs.MoveFirst
While Not rs.EOF
If Text1.Text = rs.Fields("id") And Text2.Text = rs.Fields("password") Then
    a = 1
Else
    a = 0
End If
Wend
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing

If a = 1 Then
    MsgBox ("Login Successful!!")
Else
    MsgBox ("Invalid Details!!")
End If
End Sub

Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
End Sub

Private Sub Command3_Click()
End
End Sub
4

1 回答 1

6

我认为你的 rs( ADODB.Recordset) 变量是Nothing.

您需要创建对象。

Set rs = New ADODB.Recordset

另外,我认为您Set在设置记录集的连接时需要使用关键字:

Set rs.ActiveConnection = conn
于 2012-10-29T16:35:35.790 回答