4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
热卖商品
新闻详情
eval 为什么不能执行 变量-CSDN论坛
来自 : CSDN技术社区 发布时间:2021-03-25
YoucanusetheEvalfunctiontoevaluateanexpressionthatresultsinatextstringoranumericvalue.
YoucanconstructastringandthenpassittotheEvalfunctionasifthestringwereanactualexpression.TheEvalfunctionevaluatesthestringexpressionandreturnsitsvalue.Forexample,Eval(\"1+1\")returns2.
IfyoupasstotheEvalfunctionastringthatcontainsthenameofafunction,theEvalfunctionreturnsthereturnvalueofthefunction.Forexample,Eval(\"Chr$(65)\")returns\"A\".
Syntax
Eval(stringexpr)
Thestringexprargumentisanexpressionthatevaluatestoanalphanumerictextstring.Forexample,stringexprcanbeafunctionthatreturnsastringoranumericvalue.Oritcanbeareferencetoacontrolonaform.Thestringexprargumentmustevaluatetoastringornumericvalue;itcan\'tevaluatetoaMicrosoftAccessobject.
NoteIfyouarepassingthenameofafunctiontotheEvalfunction,youmustincludeparenthesesafterthenameofthefunctioninthestringexprargument.Forexample:
Debug.PrintEval(\"ShowNames()\")\'ShowNamesisuser-defined
\'function.
Debug.PrintEval(\"StrComp(\"\"Joe\"\",\"\"joe\"\",1)\")
Debug.PrintEval(\"Date()\")
Remarks
YoucanusetheEvalfunctioninacalculatedcontrolonaformorreport,orinamacroormodule.TheEvalfunctionreturnsaVariantthatiseitherastringoranumerictype.
Theargumentstringexprmustbeanexpressionthatisstoredinastring.IfyoupasstotheEvalfunctionastringthatdoesn\'tcontainanumericexpressionorafunctionnamebutonlyasimpletextstring,arun-timeerroroccurs.Forexample,Eval(\"Smith\")resultsinanerror.
YoucanusetheEvalfunctiontodeterminethevaluestoredintheValuepropertyofacontrol.ThefollowingexamplepassesastringcontainingafullreferencetoacontroltotheEvalfunction.Itthendisplaysthecurrentvalueofthecontrolinadialogbox.
DimctlAsControl,strCtlAsString
Setctl=Forms!Employees!LastName
strCtl=\"Forms!Employees!LastName\"
MsgBox(\"Thecurrentvalueof\" ctl.Name \"is\" Eval(strCtl))
YoucanusetheEvalfunctiontoaccessexpressionoperatorsthataren\'tordinarilyavailableinVisualBasic.Forexample,youcan\'tusetheSQLoperatorsBetween...AndorIndirectlyinyourcode,butyoucanusetheminanexpressionpassedtotheEvalfunction.
ThenextexampledetermineswhetherthevalueofaShipRegioncontrolonanOrdersformisoneofseveralspecifiedstateabbreviations.Ifthefieldcontainsoneoftheabbreviations,intStatewillbeTrue(–1).Notethatyouusesinglequotationmarks(\')toincludeastringwithinanotherstring.
DimintStateAsInteger
intState=Eval(\"Forms!Orders!ShipRegionIn\"_
\"(\'AK\',\'CA\',\'ID\',\'WA\',\'MT\',\'NM\',\'OR\')\")
SeeAlso
Choosefunction,IIffunction,Switchfunction.
Example
Thefollowingexampleassumesthatyouhaveaseriesof50functionsdefinedasA1,A2,andsoon.ThisexampleusestheEvalfunctiontocalleachfunctionintheseries.
SubCallSeries()
DimintIAsInteger
ForintI=1To50
Eval(\"A\" intI \"()\")
NextintI
EndSub
ThenextexampletriggersaClickeventasiftheuserhadclickedabuttononaform.Ifthevalueofthebutton\'sOnClickpropertybeginswithanequalsign(=),signifyingthatitisthenameofafunction,theEvalfunctioncallsthefunction,whichisequivalenttotriggeringtheClickevent.Ifthevaluedoesn\'tbeginwithanequalsign,thenthevaluemustnameamacro.TheRunMacromethodoftheDoCmdobjectrunsthenamedmacro.
DimctlAsControl,varTempAsVariant
Setctl=Forms!Contacts!HelpButton
If(Left(ctl.OnClick,1)=\"=\")Then
varTemp=Eval(Mid(ctl.OnClick,2))
Else
DoCmd.RunMacroctl.OnClick
EndIf

本文链接: http://evalca.immuno-online.com/view-723159.html

发布于 : 2021-03-25 阅读(0)
公司介绍
品牌分类
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616