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

查看:315
本文介绍了复制所选值和TextBox值并附加到表 - 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

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

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