为什么使用命名范围不返回任何值?
我有一本包含几张纸的工作簿。我填充第二张表上的列表框(从单元格中提取静态数据),单击按钮,它运行良好。
当我使用命名范围填充列表框时,列表框会按照我想要的方式填充,但我收到错误,因为代码认为我没有选择列表框中的任何内容,即使我选择了。因此它通过 ""
而不是 "title"
。
这是一个常见问题吗?命名范围不是问题,因为它将数据传递到列表框,并且我知道它正在选择数据,因为一旦列表框失去焦点,它就会将单元格的内容吐出到单元格 A1
。
更奇怪的是,我将列表框的内容设置为 Msg1
。因此,A1
填充了 Msg1
(我在列表框中实际选择的内容)。但是当我尝试在代码中使用 Msg1
时,它告诉我 Msg1
是 ""
。同样,这种情况仅在我使用动态命名范围时发生,而不是在单元格 K1:K9
中使用静态数据时发生。
Private Function strEndSQL1 As String
Dim strSQL As String
strSQL = ""
'Create SQL statement
strSQL = "FROM (SELECT * FROM dbo.Filter WHERE ID = " & TextBox1.Text & " And Source IN (" & Msg1 & ")) a FULL OUTER JOIN "
strSQL = strSQL & "(SELECT * FROM dbo.Filters WHERE ID = " & TextBox2.Text & " And Source IN (" & Msg1 & ")) b "
strSQL = strSQL & "ON a.Group = b.Group
strEndSQL = strSQL
End Function
I have one workbook with several sheets. I populate the listboxes (pulling static data from cells) on the 2nd sheet, click a button and it runs fine.
When I populate the listboxes with a named range, the listbox populates the way I want, but I get an error because the code thinks that I didn't select anything in the listbox, even though I did. So it passes through ""
instead of "title"
.
Is this a common issue? The named range isn't a problem because it passes through the data to the listbox and I know it's selecting data because as soon as the listbox loses focus, it spits out the contents of the cell into cell A1
.
What's even stranger is that I have the contents of the listbox set to Msg1
. So A1
gets populated with Msg1
(what I actually selected in the listbox). But when I try and use Msg1
in the code, it tells me that Msg1
is ""
. Again, this only happens when I use the dynamic named range, not with static data in cells K1:K9
.
Private Function strEndSQL1 As String
Dim strSQL As String
strSQL = ""
'Create SQL statement
strSQL = "FROM (SELECT * FROM dbo.Filter WHERE ID = " & TextBox1.Text & " And Source IN (" & Msg1 & ")) a FULL OUTER JOIN "
strSQL = strSQL & "(SELECT * FROM dbo.Filters WHERE ID = " & TextBox2.Text & " And Source IN (" & Msg1 & ")) b "
strSQL = strSQL & "ON a.Group = b.Group
strEndSQL = strSQL
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您如何填充列表框,或者列表框是来自表单工具栏还是控件工具箱。如果是后者,下面是填充和检索值的示例。
请注意,Value 属性将取决于您在 BoundColumn 属性中设置的内容。
I'm not sure how you're filling the listbox, or whether the listbox is from the Forms toolbar or the Control Toolbox. If it's the latter, here's an example for populating and retrieving values.
Note that the Value property will depend on what you've set in the BoundColumn property.