我正在制作一个简单的表格来填充表格。其中一个字段是包含一些ID,个人或范围,如下所示:“65,73,99-114”,逗号和短划线作为分隔符。我需要Access来在另一个表中查找这些ID,并将其他列中的一些数据提供到目标表中。这应该在提交表单时完成。 尽管这个任务听起来很简单,而且我知道C#和js的一些,但这是我第一次受到VBA的挑战。我从来没有处理过这样的应用程序,我发现这些手册太模糊和/或偏离主题。基本上,我需要知道从哪些方面入手以及一些常规技巧,但欢迎您提供任何高级帮助。 p.s.进入2016使用VBA解析MS Access窗体中的文本
0
A
回答
3
没有什么从编写一些代码来“分析”出给定的文本,并创建所需的SQL阻止你。这样的代码看起来像C#或VBA并不是真正困难的部分。真正的挑战是编写代码解析出来。 (编写代码! - 这个方法与我的C#很相似)。
我只是空气编码这个,它看起来工作得很好。此代码假定所讨论的列是数字列(因此每个值周围都不需要引号)。
的代码看起来是这样的:
Function MyWhereRanges(strRanges, strField As String) As String
' take the raganges, and return a where clause
Dim vTokens As Variant
Dim strRa As String ' sql for ranges
Dim strIn As String ' sql for "in" clause
Dim s As Variant
Dim strResult As String ' return value
vTokens = Split(strRanges, ",")
For Each s In vTokens
If InStr(s, "-") Then
' this is a range - append the range
If strRa <> "" Then
strRa = strRa & " and "
End If
strRa = strRa & "(" & strField & " between " & _
Split(s, "-")(0) & " and " & Split(s, "-")(1) & ")"
Else
' append to the "in" clause
If strIn = "" Then
strIn = "(" & strField & " in ("
Else
strIn = strIn & ","
End If
strIn = strIn & s
End If
Next s
If strIn <> "" Then strIn = strIn & ")) "
strResult = strIn
If strRa <> "" Then
If strResult <> "" Then strResult = strResult & " and "
strResult = strResult & strRa
End If
MyWhereRanges = strResult ' return the value
End Function
所以,从调试窗口,您可以键入此:
? mywhereranges("5,6,12-13,15-25","InvoiceNum")
从上面的结果是这样的:
(InvoiceNum in (5,6)) and
(InvoiceNum between 12 and 13) and
(InvoiceNum between 15 and 25)
因此,在一个实际的形式,你可以发布一个报告或形式是这样的:
Dim strSQLwhere As String
strSQLwhere = MyWhereRanges(Me.txtBoxRanges, "InvoiceNum")
DoCmd.OpenReport "rptInvoices", acViewPreview, , strSQLwhere
+0
哦,整洁的解决方案。 – Gustav
相关问题
- 1. 解析MS Access中的文本
- 2. 使用VBA将VBA代码添加到MS Access用户窗体
- 3. 使用VBA解析MS Word文档中的文本
- 4. MS Access VBA反向地址解析器
- 5. 使用VBA模块在Access中从窗体获取文本值
- 6. MS Access窗体 - 在
- 7. MS Access VBA启用/禁用文本框
- 8. ms Access窗体子窗体排序
- 9. MS Access VBA使用AbsolutePosition
- 10. 的MS Access VBA
- 11. 在Access VBA中解析txt文件
- 12. MS Access窗体不是从
- 13. MS Access窗体调试
- 14. 连续窗体(ms Access 2003)
- 15. 用于自动调整MS Access 2003窗体中控件/子窗体大小的VBA脚本?
- 16. MS ACCESS vba - error
- 17. 使用Windows API在窗体中使用Ms Access淡入淡出
- 18. 使用MS Access获取Windows窗体中登录的用户
- 19. MS Access:如何引用导航窗体的子窗体?
- 20. 的MS Access富文本的文本框从VBA
- 21. 如何从MS Access窗体执行VBA功能?
- 22. 的MS Access VBA导入文本文件中
- 23. 使用VBA的MS Access打印报告
- 24. 的MS Access VBA使用IN操作符
- 25. Access中的复杂文本解析
- 26. VBA文本文件解析
- 27. MS Access 2007使用VBA打开独立窗体到特定记录
- 28. 删除MS Access窗体上的标签
- 29. 的MS Access - 隐藏列在窗体
- 30. 的MS Access窗体,在表单视图
你可以使用一个零宽度,零高度的文本框用于每个段和查找这些使它变得容易,或者看看使用split函数,这将通过分隔符分割到一个数组。 –
我需要一个如何使用表单中的数据从外部表中提取数据的想法。 – Ubertwink
这不是你的问题,但答案是dlookup,并尝试从表格,绑定控件等表格搜索数据。 –