简体   繁体   English

VB在特定记录上打开表单

[英]VB open form on a specific record

I am using Microsoft access and i want to open a form to a specific id when a button is clicked.我正在使用 Microsoft 访问,并且我想在单击按钮时打开一个具有特定 ID 的表单。 Can i specify the id in the Do.open form command.我可以在 Do.open 表单命令中指定 id 吗? the code below opens a form but then a dialog box opens asking to enter the id .下面的代码打开一个表单,但随后会打开一个对话框,要求输入 id 。 anyone any ideas ?有人有什么想法吗?

Private Sub button1_Enter()
Dim recordID As Integer
recordID = Me.CurrentRecord
DoCmd.OpenForm "Form3", , , "ID = recordID"
End sub

First, change:首先,改变:

recordID = Me.CurrentRecord

To:到:

recordID = Me.ID

Where Me.ID is the name of a field or control on the current form ( Microsoft Access - get record id when button is clicked ).其中 Me.ID 是当前表单上的字段或控件的名称( Microsoft Access - 单击按钮时获取记录 ID )。

When you refer to a variable, put it outside the quotes:当你引用一个变量时,把它放在引号之外:

DoCmd.OpenForm "Form3", , , "ID = " & recordID

This is going to be fine for an ID, which is numeric, but it will get a little more complicated with text and dates, because you will need delimiters.这对于数字 ID 来说没问题,但是文本和日期会变得更复杂一些,因为您将需要分隔符。 This will work well as long as sometextvar does not contain a quote:只要sometextvar不包含引号,这就会很好地工作:

DoCmd.OpenForm "Form3", , , "Atext = '" & sometextvar & "'"

Otherwise否则

DoCmd.OpenForm "Form3", , , "Atext = '" & Replace(sometextvar,"'","''") & "'"

And dates take #日期需要#

DoCmd.OpenForm "Form3", , , "ADate = #" & somedatevar & "#"

To avoid problems with locales, it is nearly always best to format to year, month, day, so:为避免语言环境出现问题,几乎总是最好将格式设置为年、月、日,因此:

DoCmd.OpenForm "Form3", , , "ADate = #" & Format(somedatevar,"yyyy/mm/dd") & "#"

I tried I had the same, be sure that the name you give to the id is the name used in the form, not in the DB!我试过我有同样的,请确保您给 id 的名称是表单中使用的名称,而不是数据库中的名称! IE, my id is id in DB, but pc_id in my form! IE,我的id在 DB 中是id ,但在我的表单中是pc_id

How about this solution?这个解决方案怎么样?

Private Sub Command10_Click()
On Error GoTo Err_Command10_Click
    Dim IDnr As String
    Dim stDocName As String
    Dim stLinkCriteria As String
    IDnr = InputBox("Enter ID")
    stDocName = "FORM_MASTER"
    stLinkCriteria = "[ID]=" & IDnr
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Exit_Command10_Click:
    Exit Sub
Err_Command10_Click:
    MsgBox Err.Description
    Resume Exit_Command10_Click
End Sub

Here's what has worked for me when I want to open a form (call it the Destination form) from another form (call it the Launch form) AND I want the records viewed in Destination form to be filtered (ie, restricted) to only the record that was current in the Launch form AND the field that I want to use for filtering the records is of a text (or "string") data type:当我想从另一个表单(称为启动表单)打开表单(称为目标表单)并且我希望在目标表单中查看的记录被过滤(即限制)为仅Launch 表单中的当前记录以及我想用于过滤记录的字段是文本(或“字符串”)数据类型:

Private Sub cmdOpenDestinationForm Private Sub cmdOpenDestinationForm

DoCmd.OpenForm "frmDestination",,,"[FieldName]=" & "'" & Me.ControlName & "'" DoCmd.OpenForm "frmDestination",,,"[FieldName]=" & "'" & Me.ControlName & "'"

End Sub结束子

[FieldName] refers a field in the Destination form. [FieldName] 指的是 Destination 表单中的一个字段。 [ControlName] refers to a control (like a text box) on the Launch form. [ControlName] 指的是 Launch 窗体上的控件(如文本框)。 The "Me" also refers to the Launch form, because that's the form that is open when you click the command button (and you could omit it). “Me”也指的是 Launch 表单,因为这是单击命令按钮时打开的表单(您可以省略它)。 In effect, this code is telling Access to (1) open frmDestination, (2) Search through the "FieldName" field until you find a record that has the same value as that in Me.ControlName, and (3) Filter the Destination form so as to show only that one record.实际上,此代码告诉 Access (1) 打开 frmDestination,(2) 搜索“FieldName”字段,直到找到与 Me.ControlName 具有相同值的记录,以及 (3) 过滤 Destination 表单以便仅显示该记录。

The tricky part are all those double quotes ( " ), single quotes ( ' ) and ampersands (&). Note that [FieldName]= has opening and closing double quotes: "[FieldName]=". And don't forget the = symbol. Next, this is followed by & "'". That's an ampersand, a double quote, a single quote, and another double quote. In my experience, you cannot have spaces between the quote symbols. They must be run together as shown. Then this is followed by another ampersand and the name of the control on the Launch form that has the value you want to locate in [FieldName] on the Destination form: & Me.ControlName. And, as stated before, you can omit the Me if you want. I use it because helps me remember that ControlName is a control on the Launch form, not the Destination form. Then this is followed by yet another & and another set of the double and single quotes: "'".棘手的部分是所有那些双引号 ( " )、单引号 ( ' ) 和与号 (&)。请注意 [FieldName]= 有开始和结束双引号:“[FieldName]="。不要忘记 =符号。接下来,后面是 & "'"。那是一个 & 号、一个双引号、一个单引号和另一个双引号。根据我的经验,引号之间不能有空格。它们必须如图所示一起运行. 然后后面跟着另一个&符号和 Launch 窗体上控件的名称,该控件的名称具有您要在 Destination 窗体上的 [FieldName] 中定位的值:& Me.ControlName。并且,如前所述,您可以省略如果你愿意,我会使用它。我使用它是因为帮助我记住 ControlName 是 Launch 表单上的一个控件,而不是 Destination 表单。然后是另一个 & 和另一组双引号和单引号:“'”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM