在 Access 中更改 Textbox.DefaultValue [英] Changing Textbox.DefaultValue in Access

查看:46
本文介绍了在 Access 中更改 Textbox.DefaultValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在表单的加载时"事件期间更改 Textbox.DefaultValue,这样每次加载表单时,都会提示用户使用 InputBox 更改特定的 TextBox.Default 值,该值在在我的情况下,表格上的 TextBox 控件称为 Stream.我尝试了以下代码,但每次都给我一个

I would like to be able to change the Textbox.DefaultValue during the 'On Load' event of a form such that each time the form is loaded the user is prompted with an InputBox to change a specific TextBox.Default value which in my case the TextBox control on the table is called Stream. I have tried the following code but each time it gives me a

'运行时错误 3422 无法修改表结构.另一个用户有桌子打开'.

'RunTime Error 3422 Cannot modify table structure. Another user has the table open'.

Private Sub Form_Load()

CurrentDb.TableDefs("Class 1 Students (C1)").Fields("Stream").DefaultValue = InputBox("Enter Stream Letter:")
End Sub

我正在使用 Microsoft Access

I am using Microsoft Access

推荐答案

正如 Doug Glancy 在评论中所说,不要在表设计中更改字段的默认值.而是更改文本框的默认值.

As Doug Glancy said in a comment, don't change the field's default value in table design. Instead change the text box's default value.

这是多用户数据库应用程序中的一个关键点 --- 您不希望一个用户踩到另一个用户的默认值选择.但是,即使这将始终是一个单用户应用程序,更改表设计意味着您无法在表单的记录源中打开该表.

This is a critical point in a multi-user database application --- you wouldn't want one user stomping on another's default value choice. But, even if this will always be a single-user application, changing the table design means you can't have the table open in the record source of your form.

更改文本框默认值很容易.我在表单的标题中添加了一个未绑定的文本框 txtDefaultStream.并且,在它的更新事件之后,我更改了 Me.txtStream.DefaultValue.代码如下.

Changing the text box default value is easy. I added an unbound text box, txtDefaultStream, to my form's header. And, in its after update event, I change Me.txtStream.DefaultValue. The code is below.

这是该表单的屏幕截图.输入前 2 行时,我将 A 作为默认值.然后在默认流文本框中输入 B.请注意,新记录的 Stream 文本框中有 B.

Here is a screenshot of that form in action. I had A as the default when entering the first 2 rows. Then entered B in the default stream text box. Notice the new record has B in its Stream text box.

Private Sub txtDefaultStream_AfterUpdate()
    Dim strDefault As String
    If Len(Trim(Me.txtDefaultStream & vbNullString)) > 0 Then
        strDefault = """" & Me.txtDefaultStream.value & """"
        Me.txtStream.DefaultValue = strDefault
    Else
        Me.txtStream.DefaultValue = vbNullString
    End If
End Sub

这篇关于在 Access 中更改 Textbox.DefaultValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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