清空 ComboBox 并将索引重置为 -1

huangapple go评论69阅读模式
英文:

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 = ""

huangapple
  • 本文由 发表于 2020年1月6日 22:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定