ms-access本地化和默认布尔值 [英] ms-access localization and default boolean values

查看:65
本文介绍了ms-access本地化和默认布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的访问客户端可以即时生成SQL插入,更新和删除指令,以在MS-SQL Server上发送.大多数用户具有Access 2007的运行时版本,少数用户使用完整的MS-Access版本2003或2007.今天上午,我们国外的一个新用户使用法语/完整版的Access 2003无法更新包含以下内容的数据:布尔字段.

Our access client generates on the fly SQL inserts, update and delete instructions to be sent on a MS-SQL Server. Most users have the runtime version of Access 2007, and a few use the complete MS-Access version, 2003 or 2007. This morning one of our new users abroad, using a french/complete version of Access 2003, was unable to update data containing boolean fields.

在法语的Access版本中,似乎这些字段填充了"Vrai/Faux"而不是"True/False"值.通过安装2007访问运行时解决了该问题.

It appeared that these fields are, in the french version of Access, populated with "Vrai/Faux" instead of "True/False" values. The problem was solved by installing the 2007 access runtime.

但是我想找到一个永久性的解决方案,在这里我可以从使用Access的本地化版本的地方读取内容,并将本地化的True/False值转换"为标准的True/False.我已经检查了计算机的区域设置,但没有成功,因此它在其他地方.有什么主意吗?

But I'd like to find a permanent solution, where I'd be able to read from somewhere which localized version of Access is in use and 'translate' the localized True/False values to standard True/False. I already checked the regional settings of the computer without success, so it is somewhere else. Any idea?

遵循JohnFX的建议,可以使用以下简单函数将本地True/False转换为通用True/False:

Following JohnFX proposal, it is effectively possible to convert from local True/False to universal True/False with this simple function:

Function xBoolean(xLocalBooleanValue) as Boolean
if cint(xLocalBooleanValue) = -1 Then
    xBoolean = True
endif
if cint(xLocalBooleanValue) = 0 Then
    xBoolean = False
endif
end function

按照@David的评论,我更改了最喜欢的解决方案.他的提议比我的提议聪明.

following @David's comments, I changed the favorite solution. His proposal is smarter than mine.

我通过读取记录集中的字段的值来获取Vrai/Faux值:

I am getting the Vrai/Faux values by reading the value of a field in a recordset:

? debug.print screen.activeForm.recordset.fields(myBooleanField).value 
Vrai

推荐答案

在任何情况下,无论本地化或数据库格式如何,True都不是FALSE或NOT 0.

True is NOT FALSE, or NOT 0, in all cases, no matter the localization or the database format.

因此,如果将所有True的测试替换为NOT 0并将所有False的测试替换为= 0,那么您就避免了Access关键字的本地化问题(我很惊讶VBA和Jet and Access表达式服务仍然无法理解True/False,以及数据库引擎用于存储布尔值的任何约定.

So, if you replace all tests for True with NOT 0 and all tests for False with =0, then you've avoided the issue of localization of the Access keywords (I'm surprised that VBA and the Jet and Access expression services would not still understand True/False, though), as well as whichever convention your database engine uses for storing Boolean values.

通常,您的数据访问层应该为您将其抽象化.ODBC和ADO都会自动执行此操作,因此,根据我的经验,您可以使用已知的布尔值,并且会透明地为您处理.

In general, your data access layer ought to be abstracting that away for you. Both ODBC and ADO do it automatically, so you work with the Boolean values you know and it's taken care of for you transparently, in my experience.

我仍然对这个问题感到困惑,因为这听起来像是一个显示/格式问题,但是对True和False使用NOT 0和= 0可以在所有情况下完全避免这个问题.

I'm also still puzzled about the question, as it sounds like a display/formatting issue, but use NOT 0 and =0 for True and False avoids the problem entirely in all cases.

关于编辑到Philippe问题中的功能:

In regard to the function edited into Philippe's question:

是否存在将函数的参数隐式定义为变量的原因?你是这个意思吗?如果传递了Null,则会在第一个CInt()上出错,因为CInt()无法接受Null.

Is there a reason you've implicitly defined your function's parameter as a variant? Is that what you mean? If it's passed a Null, it's going error out on the first CInt(), as CInt() can't accept a Null.

此外,还有一个逻辑问题,在VBA中,任何数字(0除外)都应返回True.它也是完全冗余的代码.这更简单,并且在所有情况下都返回正确的结果:

Also, there's a logic problem in that in VBA any number but 0 is supposed to return True. It's also completely redundant code. This is simpler and returns the correct result in all cases:

  Function xBoolean(xLocalBooleanValue As Vriant) as Boolean
    If CInt(xLocalBooleanValue) <> 0 Then
       xBoolean = True
    End If
  End Function

或者,还是更远一点:

  Function xBoolean(xLocalBooleanValue As Variant) as Boolean
    xBoolean = (CInt(xLocalBooleanValue) <> 0)
  End Function

并处理参数中传递的Null:

And to handle Nulls passed in the parameter:

  Function xBoolean(xLocalBooleanValue As Variant) as Boolean
    xBoolean = (CInt(Nz(xLocalBooleanValue, 0)) <> 0)
  End Function

我不确定在您当前正在使用它的上下文中这是否有必要,但是我总是讨厌编写代码,使我无法想象它会出错的情况-即使我知道它不会中断它的工作在目前的情况下,您永远不知道最终会在哪里使用它,因此,如果您预期可以解决的情况,则应该处理.

I'm not sure that's necessary in the context you're currently using it, but I always hate writing code where I can imagine a case where it will error out -- even if I know it can't break in its present context, you never know where it might end up getting used, so should you anticipate a condition that can be handled, you should handle it.

过早的优化?

否-它在武器上设置了安全锁,以防止被滥用.

No -- it's putting a safety lock on a weapon that keeps it from being misused.

(另一方面,如果处理该错误的代码比开始使用该功能时要花更多的代码,我会三思而后行)

(on the other hand, if it took more lines of code to handle the antipated error than the function started out with, I'd think twice about it)

这篇关于ms-access本地化和默认布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆