sql查询所有商品的信息_Access/VBA/Excel之Access模糊查询

Part 1:目标

商品清单

59287a6c16dc405d22b37cba17800b9a.png
  1. 根据商品ID检索需要的商品
  2. 通过模糊检索实现,有点百度的味道,只知道部分信息,来查询
  3. 逻辑过程
  • 连接数据库
  • 根据需求确定SQL语句
  • 执行SQL语句,得到recordset
  • recordset写入工作表(字段名+所有记录 列名+每一行)
  • 断开与数据库的连接
  1. 检索商品ID首字母为m的商品

Part 2:代码

Sub test()  Dim cnn As New ADODB.Connection '连接 Dim rs As New ADODB.Recordset  Dim SQL As String Dim tblName  Dim dbAddr dbAddr = ThisWorkbook.Path & "商品清单.accdb" tblName = "商品清单" '连接数据库 With cnn .Provider = "Microsoft.ACE.OLEDB.12.0" .Open "Data Source=" & dbAddr  End With opFilds = "商品ID,商品名称,厂家,数量" searchC = "商品ID like 'm%'" SQL = "Select " & opFilds & " from " & tblName & " where (" & searchC & ")" Set rs = cnn.Execute(SQL)   Dim sht  Dim fildNum  Set sht = ThisWorkbook.Worksheets("示例") sht.Cells.ClearContents fildNum = rs.Fields.Count  For j = 0 To fildNum - 1 Step 1 fildName = rs.Fields(j).Name sht.Cells(1, j + 1) = fildName  Next j sht.Cells(2, 1).CopyFromRecordset rs cnn.Close  Set rs = Nothing Set cnn = NothingEnd Sub

代码截图

8d8bcb30a22ef0248119cb6e144be8d8.png

执行结果

da81cb124777d732cb5e765fe386154b.png

Part 3:部分代码解读

  1. Select 商品ID,商品名称,厂家,数量 from 商品清单 where (商品ID like 'm%')
  2. 中文释义:选取满足商品ID第1位为字母m的商品信息,从结果看,没有区分大小写
  3. 这样的%表示0或者任意字符,有点像*的作用

Part 4:延伸


  • _代表单一字符,searchC = "商品ID like '____'",四个下划线,结果如下
c6c581ecc664c9b253d4ab917f8c2416.png
  • []指定范围,searchC = "商品ID like '[0-9]%'",表示0-9开头的字符串,结果如下
de4f206890aed80183af6117ef5e9a86.png
  • [ ]指定范围,searchC = "商品ID like '[a-z]%'",表示a-z开头的字符串,依然没有区分大小写,结果如下
0178f8165cfbb008e839d00a6913a207.png
  • [!]指定范围之外,searchC = "商品ID like '[!a-z]%'",表示不以a-z开头,结果如下
ca60021737be54d4e692bcf2ac7027b4.png
  • not like取反,searchC = "商品ID not like '[a-z]%'",表示不满足a-z开头的字符串,依然没有区分大小写,结果如下
356a2dbe7c560075683960eb9bd32415.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在 Access 中使用 VBA 代码新建一个 SQL 查询并导出查询结果到 Excel 的示例: ``` Sub exportSQLToExcel() Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rst As DAO.Recordset Dim excelApp As Object Dim excelBook As Object Dim excelSheet As Object Dim i As Integer Dim j As Integer ' 设置数据库文件路径和 SQL 查询语句 Set db = CurrentDb Set qdf = db.CreateQueryDef("", "SELECT * FROM your_table WHERE your_column = your_value") ' 执行查询 Set rst = qdf.OpenRecordset() ' 新建 Excel 应用程序 Set excelApp = CreateObject("Excel.Application") ' 新建 Excel 工作簿 Set excelBook = excelApp.Workbooks.Add() ' 新建 Excel 工作表 Set excelSheet = excelBook.Worksheets(1) ' 将查询结果导出到 Excel 工作表 For i = 0 To rst.Fields.Count - 1 excelSheet.Cells(1, i + 1).Value = rst.Fields(i).Name Next i For i = 0 To rst.RecordCount - 1 For j = 0 To rst.Fields.Count - 1 excelSheet.Cells(i + 2, j + 1).Value = rst.Fields(j).Value Next j rst.MoveNext Next i ' 保存 Excel 工作簿 excelBook.SaveAs "C:\test.xls" ' 关闭 Excel 应用程序 excelApp.Quit ' 释放对象引用 Set rst = Nothing Set qdf = Nothing Set db = Nothing Set excelSheet = Nothing Set excelBook = Nothing Set excelApp = Nothing End Sub ``` 在上面的代码中,需要替换以下内容: - `your_table`:要查询的表名。 - `your_column`:要查询的列名。 - `your_value`:要查询的值。 - `"C:\test.xls"`:导出的 Excel 文件路径和名称。 此外,需要确保 Microsoft Office 中已安装了 Excel 应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值