【access小品】逸马杀犬于道---代码简化示例
时 间:2011-02-12 00:00:00
作 者:煮江品茶 ID:10802 城市:武汉
摘 要:代码简化示例
正 文:
相传欧阳修任职翰林院时,一次与三个下属出游,忽见路旁有匹飞驰的马踩死了一只狗。一人曰:“有黄犬卧于道,马惊,奔逸而来,蹄而死之”,另一人曰:“有黄犬卧于通衢,逸马蹄而杀之。”,第三人曰:“有犬卧于通衢,逸马遭之而毙。” 欧阳修曰:“‘逸马杀犬于道’,六字足矣!”
今有版友名草儿者做一函数五十余句,老汉观之甚为繁琐,便颇不以为然,便递次简化,终以三句搞定。
草儿同志原函数如下:
Public Function GetBarCode(BarCode As String, Optional A1 As String = "*", Optional A2 As String = "%") As String
'用查找数据库中"Usys商品信息全部"表中商品条码或商品编码
On Error GoTo GetRs_Error
Dim StrQuery As String '变量SQL查询语句
Dim Rs As New ADODB.Recordset '新建Recordset对象
Dim Conn As New ADODB.Connection '新建Connection对象
'用于将"%"字符转变成"*"
Dim i As Integer
Dim B, C As String
C = ""
BarCode = Trim(BarCode)
For i = 1 To Len(BarCode)
B = CStr(Mid(BarCode, i, 1)) '返回符串中左边第I起后的1个字符
If B = A1 Then B = A2 '将*改为%用于SQL查询
C = C & B
Next i
BarCode = C
Set Conn = CurrentProject.Connection '打开当前连接
GetBarCode = 0
'先查询编码
StrQuery = "Select 商品条码, 商品编码 FROM Usys商品信息全部 Where (((商品编码) Like '" & BarCode & "')) orDER BY 停产 DESC , 促销 DESC , 销售 DESC , 采购 DESC , 登记日期 DESC;"
Rs.Open (StrQuery), Conn, adOpenForwardOnly, adLockBatchOptimistic
If Not Rs.EOF Then
With Rs
GetBarCode = Rs(0)
End With
Else '查询条码前面部分可不输入
StrQuery = "Select 商品条码 FROM Usys商品信息全部 Where (((商品条码) Like '%" & BarCode & "')) orDER BY 停产 DESC , 促销 DESC , 销售 DESC , 采购 DESC , 登记日期 DESC;"
'Rs.Close
Rs.Open (StrQuery), Conn, adOpenForwardOnly, adLockBatchOptimistic
If Not Rs.EOF Then
With Rs
GetBarCode = Rs(0)
End With
Else
'查询名称是否包涵字符
StrQuery = "Select 商品条码, 商品名称 FROM Usys商品信息全部 Where (((商品名称) Like '%" & BarCode & "%')) orDER BY 停产 DESC , 促销 DESC , 销售 DESC , 采购 DESC , 登记日期 DESC;"
' Rs.Close
Rs.Open (StrQuery), Conn, adOpenKeyset, adLockBatchOptimistic
If Not Rs.EOF Then
With Rs
GetBarCode = Rs(0)
End With
Else
GetBarCode = "1" '查无记录返回 “1”
End If
End If
End If
Rs.Close
Getrs_exit:
Set Rs = Nothing
Set Conn = Nothing
Exit Function
GetRs_Error:
GetBarCode = "2" '错误返回 “2”
MsgBox (Err.Description)
Resume Getrs_exit
End Function
------------------------------------------------------------------------------------------------
1、简单一些可以:
Public Function GetBarCode(BarCode As String) As String
Dim Rs As New ADODB.Recordset
Dim StrQuery As String
StrQuery = "Select 商品条码,商品编码,商品名称 FROM Usys商品信息全部"
Rs.Open StrQuery, CurrentProject.Connection, adOpenForwardOnly, adLockBatchOptimistic
Rs.Find "商品编码 Like '" & BarCode & "'"
If Rs.EOF = False Then
GetBarCode = Rs(0).Value
Else
Rs.MoveFirst
Rs.Find "商品条码 Like '%" & BarCode & "'"
If Rs.EOF = False Then
GetBarCode = Rs(0).Value
Else
Rs.MoveFirst
Rs.Find "商品名称 Like '%" & BarCode & "%'"
If Rs.EOF = False Then
GetBarCode = Rs(0).Value
Else
GetBarCode = "1"
End If
End If
End If
Rs.Close
Set Rs = Nothing
End Function
-----------------------------------------------------------------------------------------------
2、再简单一些可以:
Public Function GetBarCode(BarCode As String) As String
Dim Rs As New ADODB.Recordset
Dim StrQuery As String
StrQuery = "Select 商品条码,商品编码,商品名称 FROM Usys商品信息全部 "
StrQuery = StrQuery & " where 商品编码 Like '" & BarCode & "' or 商品条码 Like '%" & BarCode & "' or 商品名称 Like '%" & BarCode & "%'"
Rs.Open StrQuery, CurrentProject.Connection, adOpenForwardOnly, adLockBatchOptimistic
If Rs.EOF = False Then
GetBarCode = Rs(0).Value
Else
GetBarCode = "1"
End If
Rs.Close
Set Rs = Nothing
End Function
-----------------------------------------------------------------------------------------------
3、还想简单一些可以:
Public Function GetBarCode(BarCode As String) As String
Dim Rs As New ADODB.Recordset
Dim StrQuery As String
StrQuery = "Select 商品条码,商品编码,商品名称 FROM Usys商品信息全部 "
StrQuery = StrQuery & " where 商品条码 & 商品编码 & 商品名称 Like '%" & BarCode & "%'"
Rs.Open StrQuery, CurrentProject.Connection, adOpenForwardOnly, adLockBatchOptimistic
If Rs.EOF = False Then
GetBarCode = Rs(0).Value
Else
GetBarCode = "1"
End If
Rs.Close
Set Rs = Nothing
End Function
-------------------------------------------------------------------------------------------------
4、最后终极简单的可以:
Public Function GetBarCode(BarCode As String) As String
dim str as string
str="商品条码 & 商品编码 & 商品名称 Like '*" & BarCode & "*'"
GetBarCode=Nz(Dlookup("商品条码","Usys商品信息全部",str),"1")
End Function
-------------------------------------------------------------------------------------------------
5、世界末日般的简单可以:
dim str as string
str="商品条码 & 商品编码 & 商品名称 Like '*" & BarCode & "*'"
str=Nz(Dlookup("商品条码","Usys商品信息全部",str),"1")
原代码地址地址:http://www.access-cn.com/forum.php?mod=viewthread&tid=96157&extra=
Access软件网官方交流QQ群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access快速开发平台--对上传...(11.22)
- Access快速开发平台企业版--...(11.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)
- 微软Access邀测新Monaco...(11.12)
- Access列表框左右互选、列表框...(11.11)
- 高效率在导入数据前删除记录(11.10)
- Access报价单转订单示例代码(11.08)
- Access系统自带的日期选择器不...(11.08)