如何在Microsoft访问表单中创建自由标记字段? [英] How can a free tagging field be created in a Microsoft access form?

查看:189
本文介绍了如何在Microsoft访问表单中创建自由标记字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

安装



Access 2007



这是一个简化的场景。这与我的实际使用案例几乎一样,更容易解释。我在电影和流派之间有很多关系,表格结构在下面。



表:电影
id自动编号
名称文字



表:类型
id自动编号
名称文本

表:movie_genres
movie_id电话号码
genre_id号码

我想要一张表格,允许我列出给定电影的所有类型。但也让我创造新的流派,而无需打开一个单独的表格。类似于免费标记在像Drupal或Wordpress这样的cms网站上的工作方式。



我的尝试1



I已成功创建了一个表单,允许我使用指向movie_genres表的子表单和指向流派表的组合框显示所有标记。这种表单设置还允许我选择现有的值作为新的流派。但是,它并不允许我创建新的流派。



在此表单中,如果我输入的值不存在,我会收到警告您输入的文本不是列表中的项目。



如果我尝试将组合框更改为限制列表:否,我会收到以下警告:第一个可见列不等于绑定列。如果我将第一个可见列作为绑定列,组合框只显示数字而不是名称,这很愚蠢,因为信息是以任何方式出现的。



简化的情况如下所示:





我的尝试2



我也可以创建一个指向movie_genres和genres表格的子表格,并使用指向流派名称的常规文本框。这允许我创建新的值,但它不允许我从现有值中进行选择。没有图片。



在此表单上创建组合框与第二个表单相同。



再次提问



如何创建支持创建和列出现有流派的电影表单项?

解决方案

您可以使用'NotInList'事件轻松地将新值添加到流派列表。给限制列表:是并使用类似于以下代码的代码:

  Private Sub GenreName_NotInList(NewData As String,Response As Integer)
'提示用户验证他们希望添加新值。
如果MsgBox(Genre'& NewData&'不在列表中。添加?,_
vbOKCancel)= vbOK然后
'设置响应参数以指示该数据
'正在被添加。
Response = acDataErrAdded
'将NewData参数中的字符串添加到行源。
DoCmd.SetWarnings False
DoCmd.RunSQLINSERT INTO Genres(GenreName)SELECT'& NewData& ;
DoCmd.SetWarnings True
其他
'如果用户选择取消,则取消错误消息
'并取消更改。
Response = acDataErrContinue
Me.GenreName.Undo
End If
End Sub


Setup

Access 2007

This is a simplified scenario. It is nearly identical to my actual use case just easier to explain. I have a many to many relationship between movies and genres the table structure is below.

Table: movies id autonumber name text

Table: genres id autonumber name text

Table: movie_genres movie_id number genre_id number

I would like a form that allows me to list all genre's for a given movie. But also allows me to create new genre's without opening a separate form. Similar to the way free tagging works in a cms website like Drupal or Wordpress.

My Attempt 1

I have successfully created a form that allows me to display all tags using a sub-form pointing to the movie_genres table and a combo box pointing to the genre table. This form setup also allows me to select existing values as new genres. It does not however allow me to create new genre's.

In this form if I type a value not present I get the warning "The text you entered isn't an item in the list."

If I attempt to change the combo box to "Limit To List: No" I get the warning: "The first visible column... isn't equal to the bound column." If I make the first visible column the bound column the combo box simply displays numbers and not names, which is silly because the information is there either way.

The form for this simplified case looks like:

My attempt 2

I can also create a subform that points to both the movie_genres and genres tables with a regular textbox pointing to genre name. This allows me to create new values but it does not let me select from existing values. No pic of this one.

Creating a combo box on this form act identical to the second form.

The question again

How can I create a movie form item that supports both creation and listing existing genres?

解决方案

You can easily add new values to the list of genres using 'NotInList' event. Leave Limit To List: Yes and use code similar to code below:

Private Sub GenreName_NotInList(NewData As String, Response As Integer)
    ' Prompt user to verify they wish to add new value.
    If MsgBox("Genre '" & NewData & "' is not in list. Add it?", _
         vbOKCancel) = vbOK Then
        ' Set Response argument to indicate that data
        ' is being added.
        Response = acDataErrAdded
        ' Add string in NewData argument to row source.
        DoCmd.SetWarnings False
        DoCmd.RunSQL "INSERT INTO Genres (GenreName) SELECT '" & NewData & "';"
        DoCmd.SetWarnings True
    Else
    ' If user chooses Cancel, suppress error message
    ' and undo changes.
        Response = acDataErrContinue
        Me.GenreName.Undo
    End If
End Sub

这篇关于如何在Microsoft访问表单中创建自由标记字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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