简体   繁体   English

MS Access 2003-未绑定表单使用INSERT语句保存到表; 子窗体呢?

[英]MS Access 2003 - Unbound Form uses INSERT statement to save to table; what about subforms?

So I have an unbound form that I use to save data to a table on button click. 因此,我有一个未绑定的表单,可用于在单击按钮时将数据保存到表中。

Is there a way I can have subforms for entry that will allow me to save data to the table within that same button click? 有没有一种方法可以让我输入子表单,使我可以在单击同一按钮的同时将数据保存到表中? Basically I want to add more entry options for the user, and while I know other ways to do it, I am particularly curious about doing it this way (if it can be done). 基本上,我想为用户添加更多的输入选项,尽管我知道执行此操作的其他方式,但我对这种方式(如果可以做到)特别好奇。

So lets say the 'parent form' is frmMain. 因此,可以说“父表单”是frmMain。 And there are two child forms "sub1" and "sub2". 并且有两种子形式“ sub1”和“ sub2”。 Just for example sake lets say on frmMain there are two text boxes: txtTitle & txtAuthor. 举例来说,在frmMain上说两个文本框:txtTitle和txtAuthor。 sub1 and sub2 both have a text Box on them that represent something like prices. sub1和sub2都有一个文本框,代表诸如价格之类的东西。 The idea is Title & author of a book, and then a price at each store (simplified). 这个想法是书的标题和作者,然后是每个商店的价格(简化)。

So I tried this (because I thought it was worth a shot): 所以我尝试了一下(因为我认为值得一试):

Dim db as DAO.database
Dim sql as String


sql = "INSERT INTO (Title, Author, PriceA, PriceB) VALUES ("

if not isnull(me.txtTitle) then
    sql = sql & """" & me.txtTitle & ""","
Else
    sql = sql & " NULL,"
End If

if not IsNull(me.txtAuthor) then
    sql = sql & " """ & me.txtAuthor & ""","
else
    sql = sql & " NULL,"
end if

if not IsNull (forms!sub1.txtPrice) then
    sql = sql & " """ & forms!sub1.txtPrice & ""","
else
    sql = sql & " NULL,"
end if

without finishing the code, i think you may see the GOTCHA i am headed for. 如果没有完成代码,我想您可能会看到我所追求的GOTCHA。 I tried this and got an "Access cannot find the form "" ". 我尝试了此操作,并获得了“ Access找不到表格””“。 I think I can pretty much see why on this approach too, because when I click the button that calls the new sub form into the parent form, the values that were just entered are not held/saved as sub1 closes and sub2 opens. 我想我也几乎可以理解为什么使用这种方法,因为当我单击将新的子表单调用到父表单的按钮时,在sub1关闭和sub2打开时,不会保留/保存刚刚输入的值。 I should mention that the idea above is not intended to be a one or the other approach, rather both sub forms used everytime. 我应该提到的是,上述想法并非旨在成为一种或另一种方式,而是每次都使用两种子形式。

so this is an example. 所以这是一个例子。 i want to use this method (if possible) to have about 7 different sub form choices in one form, and be able to save to a table via a SQL statement. 我想使用这种方法(如果可能的话)在一种形式中具有大约7种不同的子形式选择,并能够通过SQL语句保存到表中。

I realize that there may be better ways, but I am just wondering if I can get there with this approach out of curiousity. 我意识到可能会有更好的方法,但是我只是想知道我是否可以出于好奇而采用这种方法。

Thanks as always! 一如既往的感谢!

Your problem is with how you are referring to the controls, instead of 您的问题在于您如何引用控件,而不是

forms!sub1.txtPrice 表格!sub1.txt价格

try 尝试

me!sub1.form.txtPrice 我!sub1.form.txt价格

You can see more example of how to refer to sub forms here 您可以在此处看到有关如何引用子表单的更多示例

http://www.mvps.org/access/forms/frm0031.htm http://www.mvps.org/access/forms/frm0031.htm

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

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