gpt4 book ai didi

vb6 - "Object variable or With block variable not set"VB6 运行时错误

转载 作者:行者123 更新时间:2023-12-02 19:56:22 24 4
gpt4 key购买 nike

我在使用 VB6 时遇到问题。我有一个表单,上面有几个 ComboBox 对象。我希望通过一个以 SQL 查询作为参数的函数来填充 ComboBox。所以代码看起来像这样

Private Function FillComboBoxFromMDB(ByVal sDBName As String, _
ByVal sSQL As String) As ComboBox
'/*
' * Execute SQL in MDB and fill the ComboBox with the results
' * Returns filled ComboBox
' */
Dim DB As Database
Dim DBRecordset As Recordset

On Error GoTo FillComboBoxFromMDB_ErrHandler

Set DB = OpenDatabase(sDBName, False, False)

If Not DB Is Nothing Then
Set DBRecordset = DB.OpenRecordset(sSQL)
If Not DBRecordset Is Nothing Then
If DBRecordset.RecordCount > 0 Then
Call FillComboBoxFromMDB.AddItem(DBRecordset.Fields(0).Value)
' ^^ This row gives the "Object variable or With block variable not set"
End If
Else
Call WriteLog("Unable to execute " & sSQL)
End If
DB.Close
Else
Call WriteLog("Unable to open " & sDBName)
End If

Exit Function
FillComboBoxFromMDB_ErrHandler:
Call WriteLog("FillComboBoxFromMDB() error: " & Err.Number & " " & Err.Description)
End Function

我这样调用该函数。

Private Function Test()
' Fill the combobox
frmMyForm.cmbMyCombo = FillComboBoxFromMDB("Database.mdb", _
"SELECT MyTable.MyText FROM MyTable")
End Function

所以基本上我明白这归结为实例化,但我在网上没有找到任何有用的信息。 New 关键字的工作方式与 VB.Net 中的工作方式不同。如何实例化 FillComboBoxFromMDB 组合框以便该函数正常工作?这可能吗?

提前致谢!

最佳答案

您的代码表达了这样的信念:标识符 FillComboBoxFromMDB 已获取对测试过程中作业左侧的组合框的引用。

一旦函数尝试(并失败)将结果分配给左侧,则首先执行的情况并非如此,FillCombBoxFromMDB 为 Nothing。

您需要将组合框作为参数传递。

Private Sub FillComboBoxFromMDB(ByVal sDBName As String, _
ByVal sSQL As String, ByVal cbo As ComboBox)
'/*
' * Execute SQL in MDB and fill the ComboBox with the results
' * Returns filled ComboBox
' */
Dim DB As Database
Dim DBRecordset As Recordset

On Error GoTo FillComboBoxFromMDB_ErrHandler

Set DB = OpenDatabase(sDBName, False, False)

If Not DB Is Nothing Then
Set DBRecordset = DB.OpenRecordset(sSQL)
If Not DBRecordset Is Nothing Then
If DBRecordset.RecordCount > 0 Then
Call cbo.AddItem(DBRecordset.Fields(0).Value)
' ^^ This row gives the "Object variable or With block variable not set"
End If
Else
Call WriteLog("Unable to execute " & sSQL)
End If
DB.Close
Else
Call WriteLog("Unable to open " & sDBName)
End If

Exit Sub
FillComboBoxFromMDB_ErrHandler:
Call WriteLog("FillComboBoxFromMDB() error: " & Err.Number & " " & Err.Description)
End Sub

这样调用它:-

 Private Function Test()
' Fill the combobox
Call FillComboBoxFromMDB("Database.mdb", _
"SELECT MyTable.MyText FROM MyTable", _
frmMyForm.cmbMyCombo )
End Function

关于vb6 - "Object variable or With block variable not set"VB6 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1206526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com