英文:
Clearing a ComboBox and resetting index to -1
问题
我一直在为我的数据库课程的数据库项目努力工作,但卡在一个看似荒谬的问题上;在我的一个表单中清除一个 ComboBox 中的值。我已经尝试了几个小时,但还是无法解决。这个 ComboBox 有一个控制源,将其链接到一个日期/时间数据类型的属性,格式为短时间(hh:ss)。
我尝试了一些方法,但似乎都不起作用,比如:
ComboBox1.Items.Clear()
但这不起作用,因为在 Access 的 VB 版本中根本不存在 Items 属性,我使用的是 Access 2019,其中包含了 VB for Apps 7.1。大多数互联网上的建议都是这种方法。
我还尝试了一个有点古怪的方法:
index = Combo39.ListIndex
Dim indexc As Integer
indexc = 0
If index = -1 Then
'什么都不做,因为它已经为空
Else
Do
Combo39.RemoveItem (indexc)
If indexc = index Then
Exit Do
End If
indexc = indexc + 1
Loop
End If
这段代码基本上会循环,直到循环的次数与组合框的索引号相同。我不太确定为什么,但它不起作用。它似乎会移除一些东西,但不是全部。就好像循环提前终止了一样。
我非常需要帮助,我已经完全不知道如何做这个简单的事情了。非常感谢您的帮助,谢谢。
英文:
I've been working on a database project for my database classes but I am stuck at the most ridiculous thing; clearing a ComboBox of values in one of my forms. I've been trying to figure out how to do this for hours, but I cannot do it. The ComboBox has a control source linking it to an attribute which is a data type of Date/Time, formatted as short time (hh:ss)
I've tried a few things, but nothing really works, like:
ComboBox1.Items.Clear()
but that won't work because the Items property doesn't even exist in Access's version of VB, I run Access 2019 which has VB for Apps 7.1. Most things on the Internet suggest this approach.
I've also tried a rather quirky method:
index = Combo39.ListIndex
Dim indexc As Integer
indexc = 0
If index = -1 Then
'do nothing because its already empty
Else
Do
Combo39.RemoveItem (indexc)
If indexc = index Then
Exit Do
End If
indexc = indexc + 1
Loop
End If
This code basically loops until the number of times the loop has cycled is the same as the index number of the combo box. I am not sure why, but it doesn't work. It does appear to remove some things, but not everything. It's as if the loop breaks early.
I am in huge need of help, I've run completely dry on how to do this one simple thing. I would greatly appreciate help, thank you.
答案1
得分: 1
你的组合框似乎具有值列表作为其行源,你只需清空它:
Me!Combo39.RowSource = ""
英文:
As your combobox seems to have a list of values as its rowsourcet, all you need is to clear this:
Me!Combo39.RowSource = ""
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论