MS Access 2003 - Unbound Form 使用 INSERT 语句保存到表;那么子表单呢?
因此,我有一个未绑定的表单,用于在单击按钮时将数据保存到表中。
有没有一种方法可以让我输入子表单,从而允许我在单击同一按钮时将数据保存到表中?基本上我想为用户添加更多的输入选项,虽然我知道其他方法可以做到这一点,但我对这种方式特别好奇(如果可以做到的话)。
假设“父窗体”是 frmMain。并且有两个子表单“sub1”和“sub2”。举个例子,假设 frmMain 上有两个文本框:txtTitle 和 txtTitle。 txt作者。 sub1 和 sub2 上都有一个文本框,表示价格等内容。这个想法是标题和一本书的作者,然后是每家商店的价格(简化)。
所以我尝试了这个(因为我认为值得一试):
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
在没有完成代码的情况下,我想你可能会看到我要解决的问题。我尝试了此操作并收到“Access 找不到表单”“”。我想我也能明白为什么采用这种方法,因为当我单击将新子表单调用到父表单中的按钮时,刚刚输入的值不会在 sub1 关闭和 sub2 打开时保留/保存。 我应该提到的是,上面的想法并不是要成为一种或另一种方法,而是每次都会使用两种子形式。
这是一个例子。我想使用这种方法(如果可能的话)在一个表单中有大约7种不同的子表单选择,并且能够通过SQL语句保存到表中。
我意识到可能有更好的方法,但我只是想知道我是否可以出于好奇而使用这种方法实现这一目标。
一如既往的感谢!
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. And there are two child forms "sub1" and "sub2". Just for example sake lets say on frmMain there are two text boxes: txtTitle & txtAuthor.
sub1 and sub2 both have a text Box on them that represent something like prices. 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. I tried this and got an "Access cannot find the form "" ". 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.
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题在于如何引用控件,而不是
表单!sub1.txtPrice
试试
我!sub1.form.txtPrice
您可以在此处查看如何引用子表单的更多示例
http://www.mvps.org/access/forms/frm0031.htm
Your problem is with how you are referring to the controls, instead of
forms!sub1.txtPrice
try
me!sub1.form.txtPrice
You can see more example of how to refer to sub forms here
http://www.mvps.org/access/forms/frm0031.htm