vba VBA中的FindRecord方法怎么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12644482/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:06:17  来源:igfitidea点击:

How do you use the FindRecord method in VBA?

vbams-accessaccess-vba

提问by user1680893

I have a small query builder on a form I created that allows the user to save the query they built. Works great! Then I realized that they can save multiple queries using the same name. Not good. What I'm doing is when they build the query and choose to save it, I just add the SQL string to a table in one field and a name, that they choose, in another field. Easy enough. So now I figure all I have to do search the Name field and check it against the name the user has chosen. I dont know why, but I cant seem to wrap my mind around it. I can't seem to get the FindRecord to work. Does it return a boolean? Can't I just do something like this...

我在创建的表单上有一个小型查询构建器,允许用户保存他们构建的查询。效果很好!然后我意识到他们可以使用相同的名称保存多个查询。不好。我正在做的是当他们构建查询并选择保存它时,我只是将 SQL 字符串添加到一个字段中的表中,并在另一个字段中添加他们选择的名称。很容易。所以现在我想我所要做的就是搜索“名称”字段并根据用户选择的名称进行检查。我不知道为什么,但我似乎无法理解它。我似乎无法让 FindRecord 工作。它返回一个布尔值吗?我不能做这样的事情吗...

If DoCmd.FindRecord(userNameVariable) = True Then
 msgbox("That name exists") 
Else
 msgbox(That name doesnt exist)
End If

Now that I look at that, it doesn't give any where to look, such as the table and field name.

现在我看到了它,它没有给出任何可以查看的位置,例如表和字段名称。

回答by kb_sou

DoCmd.FindRecord returns the first record found with that criteria. If you only want to check if that name already exists I think that there is a better way to do what you need.

DoCmd.FindRecord 返回使用该条件找到的第一条记录。如果您只想检查该名称是否已经存在,我认为有一种更好的方法可以满足您的需求。

One way is to build a SQL string like "SELECT [Name] FROM [Table] WHERE [Name] = '" & userNameVariable & "'", execute the Query and then test if the recordset is empty. The Code would look like

一种方法是构建一个 SQL 字符串,如 "SELECT [Name] FROM [Table] WHERE [Name] = '" & userNameVariable & "'",执行查询,然后测试记录集是否为空。代码看起来像

Dim rs as recordset
Dim strSQL as string
strSQL = "SELECT [Name] FROM [Table] WHERE [Name] = '" & userNameVariable & "'"
Set rs = CurrentDb.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then 
   msgbox "This Query name already exists, please select another" 
End If

回答by PowerUser

According to msdn, it simply goes to the data you searched for. I don't think that's what you need.

根据msdn,它只是转到您搜索的数据。我不认为那是你需要的。

Here's what I would do (I'm not saying it's the best method, just what I would do). Assuming the table is called TblNameand the field holding the saved name is SQLNamethen:

这就是我会做的(我不是说这是最好的方法,只是我会做的)。假设调用该表TblName并且保存名称的字段为SQLName

If CurrentDb.OpenRecordset( _
    "Select count(*) from TblName where SQLName='" & SQLName & "';") _
    .Fields(0) > 0 Then
    'Do Something
End If

回答by Fionnuala

Saved query names are stored in the unsupported MSysObjects table, so in a local database you can look up that table to find if a query exists:

保存的查询名称存储在不受支持的 MSysObjects 表中,因此您可以在本地数据库中查找该表以查找查询是否存在:

If IsNull(DLookup("name", "msysobjects", "name='" _
    & qryname & "' and type=5")) Then

    Set qdf = CurrentDb.CreateQueryDef(qryname, sSQL)
Else
   MsgBox qryname " already exists."
End If