【access小品】裸奔--自定义函数编制讲座六
时 间:2011-06-20 11:57:01
作 者:煮江品茶 ID:10802 城市:武汉
摘 要:自定义函数编制讲座六
正 文:
江羽版主最近在研究拼音声调字符转换为字母的问题,连续用了两种方法,其一是数组法,其二是字符串法。这个问题我也曾用过一个数据表法,说明解决此类问题的办法很多。结合这个问题,想讲一下Replace函数的嵌套问题。由于没有依赖其他的辅助手段,比较起来可以称之为裸奔法。
许多函数都是可以嵌套使用的,比如iif函数、instr函数等等。Replace函数也是可以嵌套使用的,Replace的嵌套执行的是连续的不同替换。只要你愿意可以嵌套很多层,不过为了阅读的方便起见,一般可以顺序的用几次Replace嵌套来解决替换问题。比如拼音声调字符转换为字母的问题,我们可以写如下自定义函数:
function str1(str0 as string) as string
'功能:将拼音声调字符转换为字母
str1=str0
str1=Replace(Replace(Replace(Replace(str1,"ā","a"),"á","a"),"ǎ","a"),"à","a")
str1=Replace(Replace(Replace(Replace(str1,"ē","e"),"é","e"),"ě","e"),"è","e")
str1=Replace(Replace(Replace(Replace(str1,"ī","i"),"í","i"),"ǐ","i"),"ì","i")
str1=Replace(Replace(Replace(Replace(str1,"ō","o"),"ó","o"),"ǒ","o"),"ò","o")
str1=Replace(Replace(Replace(Replace(str1,"ū","u"),"ú","u"),"ǔ","u"),"ù","u")
str1=Replace(Replace(Replace(Replace(str1,"ǖ","v"),"ǘ","v"),"ǚ","v"),"ǜ","v")
end function
这个函数的编制方法可以用到许多方面,如果你想将阿拉伯数字转换为中文数字,也是可以用此方法加以解决的,我们可以编写如下的自定义函数:
function str1(str0 as string,B as boolean) as string
'功能:将阿拉伯数转换为中文字符
'参数:B=true 转换为一、二... B=False 转换为壹、贰...
str1=str0
select case B
case true
str1=Replace(Replace(Replace(Replace(str1,"0","0"),"1","一"),"2","二"),"3","三")
str1=Replace(Replace(Replace(Replace(str1,"4","四"),"5","五"),"6","六"),"7","七")
str1=Replace(Replace(str1,"8","八"),"9","九")
case false
str1=Replace(Replace(Replace(Replace(str1,"0","零"),"1","壹"),"2","贰"),"3","叁")
str1=Replace(Replace(Replace(Replace(str1,"4","肆"),"5","伍"),"6","陆"),"7","柒")
str1=Replace(Replace(str1,"8","捌"),"9","玖")
end select
end function
假如你认为运算符输出不美观,也可以用这个方法来编写一个自定义的转换函数:
function str1(str0 as string) as string
'功能:将运算符转换为输出格式
str1=str0
str1=Replace(Replace(Replace(Replace(str1,"+","+"),"-","-"),"*","×"),"/","÷")
str1=Replace(Replace(Replace(Replace(str1,"=","="),"<>","≠"),"<=","≤"),">=","≥")
str1=Replace(Replace(str1,"<","<"),">",">")
end function
如此等等,你可以按照你的需要,依据此法写出很多实用功能很强的自定义函数来了。通过这个讲座,希望能使初学的版友明白,很多时候自定义函数就是一两句VB语句,关键看你是否真正掌握和理解了它们。掌握和理解了,就能在多种方法中找到最简单最直接的那些东西。
上一讲 下一讲
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.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)
- 微软Access邀测新Monaco...(11.12)
- Access列表框左右互选、列表框...(11.11)
- 高效率在导入数据前删除记录(11.10)
- Access报价单转订单示例代码(11.08)
- Access系统自带的日期选择器不...(11.08)
- 分享一下Access工程中的acw...(11.07)