vba 如何检查 Access 中的附件字段是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21106241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if an attachment field is empty or not in Access?
提问by smith22554
I am having some trouble with MS Access 2010. I am trying to modify a database I downloaded, a template from Microsoft‘s web site, I am doing a little modifying to keep track of the food nutrition. I have some experience with basic from the old days of the 16 bit Atari days. I'm not totally green to VBA, just know enough to get in trouble. I have a Form that uses a select query to populate lower half of the form. I added an attachment field to the foods table which has over 8500 record that is the table for the query. My problem is to add VBA code in a module to see if there is a photo present or not. I want to be able to show an icon grayed out for no photo and a regular icon if there is a photo file. But that's for when I get it work to begin with. I call the function in the field properties:
我在使用 MS Access 2010 时遇到了一些问题。我正在尝试修改我下载的数据库,一个来自 Microsoft 网站的模板,我正在做一些修改以跟踪食物营养。我从过去的 16 位 Atari 时代开始就有一些基本的经验。我对 VBA 并不完全了解,只是知道足以惹上麻烦。我有一个使用选择查询来填充表单下半部分的表单。我在foods表中添加了一个附件字段,它有超过8500条记录,作为查询表。我的问题是在模块中添加 VBA 代码以查看是否存在照片。如果有照片文件,我希望能够显示一个没有照片的灰色图标和一个常规图标。但那是我开始工作的时候。我在字段属性中调用函数:
Photo1: chkAttachment([Photo])
This one to start with gives me and error saying: "The multi-valued field '[Photo]' is not a valid in the expression 'chkAttachment([Photo])'. When I change it to:
这个一开始给我和错误说:“多值字段'[照片]'在表达式'chkAttachment([照片])'中无效。当我将其更改为:
Photo1: ChkAttachment([Photo].[FileName])
I get and #Error for the empty fields and a "Has Photo" for the one with a file in it.
我得到了空字段的 #Error 和一个带有文件的“有照片”。
The Following code is the function I am referencing To Show what I am trying to do
以下代码是我正在引用的功能,以显示我正在尝试执行的操作
Public Function chkAttachment(fldPhoto As String) As String
On Error GoTo chkAttachment_Err
chkAttachment = ""
'Debug.Print fldPhoto
If fldPhoto = Null Then
chkAttachment = "No Photo"
Else
chkAttachment = "Has Photo"
End If
chkAttachment_Exit:
Exit Function
chkAttachment_Err:
MsgBox " It don't like Error number: " & Err.Number & " " & Error$
Resume chkAttachment_Exit
End Function
The query won't even call the code if the attachment is empty. I Googled the following question, and searched on this web site “Microsoft Access 2010 VBA how to query attachment field” without any luck. I have tried to use some code to count the number of files in the attachments, I found in the thread “How to query number of attachments from Attachment field in Microsoft Access?” from Aug 2011, but couldn't figure out how to get it to work. When it comes to SQL, I'm in the dark with that part.
如果附件为空,查询甚至不会调用代码。我在 Google 上搜索了以下问题,并在此网站上搜索了“Microsoft Access 2010 VBA 如何查询附件字段”,但没有任何运气。我曾尝试使用一些代码来计算附件中的文件数,我在“如何从 Microsoft Access 中的附件字段中查询附件数?”的帖子中找到。从 2011 年 8 月开始,但无法弄清楚如何让它工作。说到 SQL,我对那部分一无所知。
Thanks for any help that comes my way.
感谢您对我的帮助。
Steven
史蒂文
回答by Gord Thompson
If you have an Attachment
control named [attachPhoto] on your form and that control is bound to the [Photo] field (i.e., the Control Source
of the Attachment control is Photo
) then you can just check the value of
如果您Attachment
的表单上有一个名为 [attachPhoto]的控件并且该控件绑定到 [Photo] 字段(即,Control Source
附件控件的 是Photo
),那么您只需检查的值
Me.attachPhoto.AttachmentCount
to see if the record has any attachments.
查看记录是否有任何附件。
(Note that if you don't want the users to actually seethe [attachPhoto] control you can just set its Visible
property to No
.)
(请注意,如果您不希望用户实际看到[attachPhoto] 控件,您可以将其Visible
属性设置为No
。)
回答by parakmiakos
I believe this is causing the #ERROR in your field
我相信这会导致您的领域出现 #ERROR
If fldPhoto = Null Then
Correct checking for Null would be the following:
对 Null 的正确检查如下:
If IsNull(fldPhoto) Then
But then you might still get a Null exception while calling the function. So you might want to edit your overall code to the following:
但是,在调用该函数时,您可能仍然会遇到 Null 异常。因此,您可能希望将整体代码编辑为以下内容:
If (fldPhoto = "") Then
and when calling the function :
当调用函数时:
Photo1: ChkAttachment(Nz([Photo].[FileName]))
Hope I helped
希望我有所帮助
回答by Terry S
You can check it in SQL statement " Not (tblxx.Pic.FileData) Is Null " probably in the where clause. I did it this way using a recordset.
您可以在 SQL 语句“ Not (tblxx.Pic.FileData) Is Null ”中检查它可能在 where 子句中。我使用记录集以这种方式做到了。