vba【查询功能】按A-Z首字母查询(上)
HI,大家好!
之前我给大家分享过一个示例,如下图:
这个示例我们可以看到可以按字母A-Z,数字0-9的一个筛选编码,同时可以选择编号后可以显示对应的明细数据。
接下去我就来教大家怎么来做这个示例,这个示例东西比较多,所以我们将他分解一下,今天我们先来做左上角的功能,如下图:
鼠标在放到对应的标签上后,文字对应的变红,且标签有浮动的效果,点击后列表框的数据发生变化,接下去我们来看一下怎么实现这些功能。
01、创建窗体
创建一个窗体,然后在左上角放上26个英文字母的标签,还有数字,没错你没有看错,那些英文字母就是标签控件,需要一个一个的放好,做开发要有耐心,放好这些控件后,在右下角放一个列表框(infoList),右边放上一个子窗体控件(frmChild)。
02、添加代码
这次在窗体里添加两个函数
标签移动通用函数
Private Function lblMove(intCurrButton As String)
On Error GoTo ErrorHandler
Static intLastClicked As String, tmp1
Dim ctl As Control
If intCurrButton <> intLastClicked Then
intLastClicked = intCurrButton
For Each ctl In Me
If ctl.ControlType = acLabel Then
ctl.SpecialEffect = 0
ctl.ForeColor = 0
ctl.FontWeight = 400
End If
Next
tmp1 = intLastClicked
Select Case intLastClicked
Case "ZET9"
Me(intCurrButton).SpecialEffect = 1
' Me.(intCurrButton).ForeColor = 17777215
Me(intCurrButton).FontWeight = 900
Me(intCurrButton).ForeColor = 255
Case "ALL"
Me(intCurrButton).SpecialEffect = 1
' Me(intCurrButton).ForeColor = 17777215
Me(intCurrButton).FontWeight = 900
Me(intCurrButton).ForeColor = 255
Case "OTH"
Me(intCurrButton).SpecialEffect = 1
' Me(intCurrButton).ForeColor = 17777215
Me(intCurrButton).FontWeight = 900
Me(intCurrButton).ForeColor = 255
Case tmp1
Me(intCurrButton).SpecialEffect = 1
' Me(intCurrButton).ForeColor = 17777215
Me(intCurrButton).FontWeight = 900
Me(intCurrButton).ForeColor = 255
Case 0
Me(intCurrButton).SpecialEffect = 0
Me(intCurrButton).ForeColor = 0
Me(intCurrButton).FontWeight = 400
End Select
intCurrButton = intLastClicked
End If
ExitHere:
Exit Function
ErrorHandler:
MsgBox Err.Description, vbCritical
Resume ExitHere
End Function
标签点击通用函数
Private Function lblClick(intCurrButton As String)
On Error GoTo ErrorHandler
Dim strSQL As String
Me.infoList.RowSource = ""
Select Case intCurrButton
Case "All"
strSQL = "select distinct 拼音码 from qry商品信息 where 拼音码 order by 拼音码"
Case "OTH"
strSQL = "select distinct 拼音码 from qry商品信息 where 拼音码 not like '[0-9]*' and 拼音码 not like '[A-Z]*' order by 拼音码"
Case "ZET9"
strSQL = "select distinct 拼音码 from qry商品信息 where 拼音码 like '#*' order by 拼音码"
Case Else
strSQL = "select distinct 拼音码 from qry商品信息 where 拼音码 like '" & intCurrButton & "*' order by 拼音码"
End Select
'Debug.Print strSQL
Me.infoList.RowSource = strSQL
ExitHere:
Exit Function
ErrorHandler:
MsgBox Err.Description, vbCritical
Resume ExitHere
End Function
注意:这里的表也要自己相应的添加一下。
函数添加完成后,像下图一样给每个标签添加对应的代码
这个是函数的一个调用方式。
03、运行测试
最后一步就是运行窗体测试了。
大家快去测试一下吧。
最后是广告时间,大家可以关注我的B站,Access开发视频首发都在我的B站。
从事access开发多年,喜欢access做一些小东西,分享一些小经验
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义