Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio (错误91:对象变量或未设置块变量)在Visual Basic 6.0中声明和访问全局变量时出错_Visual Studio_Vba_Adodb - Fatal编程技术网

Visual studio (错误91:对象变量或未设置块变量)在Visual Basic 6.0中声明和访问全局变量时出错

Visual studio (错误91:对象变量或未设置块变量)在Visual Basic 6.0中声明和访问全局变量时出错,visual-studio,vba,adodb,Visual Studio,Vba,Adodb,我正在设计一个登录表单。我在一个模块中完成了全局声明: Global db As ADODB.Connection Global rs As ADODB.Recordset Global tot As Integer Public Sub access_connector() Set db = New ADODB.Connection db.Provider = "Microsoft.jet.oledb.4.0" db.CursorLocati

我正在设计一个登录表单。我在一个模块中完成了全局声明:

   Global db  As ADODB.Connection
   Global rs  As ADODB.Recordset
   Global tot As Integer

   Public Sub access_connector()
   Set db = New ADODB.Connection
    db.Provider = "Microsoft.jet.oledb.4.0"
    db.CursorLocation = adUseClient
    db.Open App.Path & "\data.mdb"
  End Sub
在表单的“代码”窗口中:

   Private Sub Command1_Click()
   db.Open
   Set rs = db.Execute("SELECT * FROM Login Where UserName='" _
       & txtusername.Text & "'")
   If txtpassword = "" And txtusername = "" Then
      MsgBox "Login not possible"
   Else
   If Not rs.EOF() Then
      If (rs(1) = txtpassword.Text) Then
          MsgBox "Login Successful"
      Else
          MsgBox "Login not success"
      End If
   Else
      MsgBox "EOF Reached"
  End  If
 End If
 db.Close
 End Sub
但当我单击“登录”按钮时,会出现以下错误: 错误91:未设置对象变量或块变量

实际上,我认为(可能不是真的)它不能识别“db”和“rs”对象,因为在调试“db.open”时会突出显示


谁能解决这个问题。我会非常感激的。提前谢谢

我将“db”变量的名称改为“conn”,以强调这是一个连接,而不是数据库。然后,您应该在需要时在表单中打开连接。您必须在表单中指定数据库,因为您之前没有声明数据库变量。也许您应该重新考虑全局ADODB变量,并将它们包含在表单代码中,但我不确定这一点

Global conn As ADODB.Connection
Global tot As Integer

Public Sub access_connector()
Set conn = New ADODB.Connection
conn.Provider = "Microsoft.jet.oledb.4.0"
conn.CursorLocation = adUseClient
End Sub

Private Sub Command1_Click()
Dim rs As ADODB.Recordset

access_connector
conn.Open App.Path & "\data.mdb"
Set rs = conn.Execute("SELECT * FROM Login Where UserName='" & txtusername.Text & "'")
If txtpassword = "" And txtusername = "" Then
    MsgBox "Login not possible"
Else
    If Not rs.EOF() Then
        If (rs(1) = txtpassword.Text) Then
            MsgBox "Login Successful"
        Else
            MsgBox "Login not success"
        End If
    Else
        MsgBox "EOF Reached"
    End If
End If
conn.Close
End Sub

在哪里呼叫访问\u连接器?它可能丢失了,所以很确定,db还是一无所有。非常感谢……我没有打电话给access\u connector。现在prblm被解决了…再次是Thanx。阅读Hans的评论,您也可以保留access_connector sub,正如您所做的一样-尽管为了避免混淆,我会调用变量“conn”,然后从表单中调用它。因为你们在这两个地方都有dp.open,我不确定你们想在多大程度上实现全球化。非常感谢……我没有打电话给access\u connector。现在prblm解决了…又是Thanx。