vba 收到“运行时错误‘380’:无法设置 Value 属性。属性值无效。” 为组合框“值”属性赋值时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24593328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:42:53  来源:igfitidea点击:

Getting a "Run-time error '380': Could not set the Value property. Invalid property value." error when assigning a value to combobox 'value' property

excelexcel-vbacomboboxvba

提问by BillD

Here's the setup: As part of a multiple batting cage scheduling program I am developing, a data dictionary (dReservData) is loaded with data for all the reservations for a selected date. There are 16 fields loaded into dReservData. When a specific reservation is selected by the user, controls on a form are loaded with the appropriate fields from the data dictionary for that reservation. (In the interest of brevity, I only included what I think is the relevant portion of the code.) The "Run-time error '380': Could not set the Value property. Invalid property value." error occurs when I try to assign the value 1 (which you can see from the output of the Debug statement) to the value property of a combobox (combo_ReservationsInstructor). The combobox has 2 columns and is pre-populated with an N x 2 array of Instructor ID's and Names. I am trying to use the assignment of an instructor ID (in this case, ID '1') to the combobox's value property as a means of programmatically selecting an item in the combo.

这是设置:作为我正在开发的多击球笼调度程序的一部分,数据字典 (dReservData) 加载了选定日期的所有预订的数据。有 16 个字段加载到 dReservData。当用户选择了特定的预订时,表单上的控件将加载该预订的数据字典中的相应字段。(为了简洁起见,我只包含了我认为代码的相关部分。)“运行时错误 '380':无法设置 Value 属性。无效的属性值。” 当我尝试将值 1(您可以从 Debug 语句的输出中看到)分配给组合框 (combo_ReservationsInstructor) 的 value 属性时发生错误。组合框有 2 列,并预先填充了一个 N x 2 的教师 ID 和姓名数组。我正在尝试使用分配给组合框的 value 属性的讲师 ID(在本例中为 ID '1')作为以编程方式选择组合中的项目的一种方式。

With form_APScheduler
    .tb_ReservationsBeginTime.Value = dReservData(BegTime)(iStepRes)
    .tb_ReservationsEndTime.Value = dReservData(EndTime)(iStepRes)
    .tb_ReservationsNote.Value = dReservData(ResNote)(iStepRes)

    If dReservData(RentType)(iStepRes) = "Lesson" Or dReservData(RentType)(iStepRes) = "CageRental" Then

        If dReservData(RentType)(iStepRes) = "Lesson" Then
            .combo_ReservationsType.Value = "Lesson"
            Debug.Print "dReservData(ResInstructor)(iStepRes) = " & dReservData(ResInstructor)(iStepRes)    'Debug output: dReservData(ResInstructor)(iStepRes) = 1
            .combo_ReservationsInstructor.Value = dReservData(ResInstructor)(iStepRes)   '***ERROR OCCURS HERE

        ElseIf dReservData(RentType)(iStepRes) = "CageRental" Then
            .combo_ReservationsType.Value = "CageRental"

        End If

回答by Axel Stache

Assigning the selected item of a combobox with the .Valueproperty is not working in all the cases very well. Try to use the .ListIndexproperty instead.

使用.Value属性分配组合框的选定项目在所有情况下都不能很好地工作。尝试改用该.ListIndex属性。

In your example the line where you get the error would look like:

在您的示例中,您收到错误的行如下所示:

.combo_ReservationsInstructor.ListIndex = dReservData(ResInstructor)(iStepRes) -1

Attention: ListIndexof a combobox is starting to count with 0... thats the reason for the -1at the end of the line.

注意:ListIndex组合框的开始从 0 开始计数......这就是行尾的原因-1

This solution is assuming that the instructor IDs are starting with 1 and are continuing without gaps

此解决方案假设教师 ID 以 1 开头并继续无间隙