复制选定值和文本框值并附加到表 - MS Access VBA [英] Copy Selected Value and TextBox Value and Append to Table - MS Access VBA

查看:30
本文介绍了复制选定值和文本框值并附加到表 - MS Access VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在暑期实习,最后在访问数据库方面做了一些工作.我一直在寻找一种使用 VBA 代码的方法,将用户从子表单中的查询中选择的位置编号(如图所示)和他们在先前表单的文本框中输入的数字附加到现有表.此表以自动编号为主键.

I'm doing an internship over the summer and have ended up doing some work on an access database. I have been searching for a way to use VBA code to append a location number which the user has selected from a query in a subform (shown in the picture) and a number which they entered into a text box on a previous form, to an existing table. This table has autonumber as the primary key.

总结当用户单击选择站点"时,我希望访问从另一个表单上的文本框中复制代码并复制他们选择/突出显示的任何位置(在本例中为 9.01)并使用它们中的两个在现有表中形成新记录.我只需要复制 9.01 - 而不是整个记录.

To summarise when the user clicks "Select Site" I would like access to copy a code from a text box on another form and copy whichever location they have selected/highlighted (in this case 9.01) and use the two of them to form a new record in an existing table. I only need to copy the 9.01 - not the whole record.

任何帮助将不胜感激!

推荐答案

您可以使用这样的代码来完成您想做的事情.将此代码插入按钮上的 Click 事件,并将表单、表格、列和参数名称更改为您需要的名称:

You can use code like this to accomplish what you want to do. insert this code to the Click event on your button and change the form, table, column, and parameter names to what you need:

On Error GoTo ErrorHandler

    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef

    Set db = CurrentDb()

    Set qdf = db.CreateQueryDef("", "INSERT INTO [MyTable] (MyCol1, MyCol2) VALUES ([MyVal1], [MyVal2])")
    qdf.Parameters("[MyVal1]") = Forms![MyForm1]![MyControl1]
    qdf.Parameters("[MyVal2]") = Forms![MyForm2]![MyControl2]

    qdf.ReturnsRecords = False
    qdf.Execute

ExitMe:
    Set qdf = Nothing
    Set db = Nothing

    Exit Sub
ErrorHandler:
    MsgBox Err.Number & ": " & Err.Description
    GoTo ExitMe

这篇关于复制选定值和文本框值并附加到表 - MS Access VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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