从文本框和按钮[Access]中将数据添加到表/查询中

时间:2013-10-24 11:56:36

标签: ms-access access-vba

我正在寻找帮助,尝试通过文本框和按钮将数据添加到表/查询中。目前,有两个按钮将连接到文本框;搜索&添加。

搜索我已经完成了搜索附加到表格的查询,搜索输入到文本框中的输入。简单。

虽然现在我还想创建一个添加按钮,一旦你将信息放入文本框并单击添加而不是搜索,它会直接将该信息添加到表格中并保存,以便您以后查看时间点。

这是我在网上找到的代码,但我不知道如何使用它从文本框中获取数据:

Private Sub Command344_Click()

INSERT INTO OrderT (CustomerName,OrderName,OrderDesc,DateOfPurchase,ProjectDueDate,EngineerDueDate,ProjectComplete,CutplanDueDate,MaterialSpecs,CutplanCode,HardwareSpecs,HardwareDueDate,HardwareComplete,PurchaseOrder,PurchaseSupplier);
VALUES (CustomerName,OrderName,OrderDesc,DateOfPurchase,ProjectDueDate,EngineerDueDate,ProjectComplete,CutplanDueDate,MaterialSpecs,CutplanCode,HardwareSpecs,HardwareDueDate,HardwareComplete,PurchaseOrder,PurchaseSupplier);

End Sub

按钮名称: Command344

TextBox名称: 顾客姓名 ORDERNAME OrderDesc 购买日期 ProjectDueDate EngineerDueDate ProjectComplete CutplanDueDate MaterialSpecs CutplanCode HardwareSpecs HardwareDueDate HardwareComplete 采购订单 PurchaseSupplier

表格中的字段与顶部完全相同的顺序具有相同的名称 - > bottom,left - >右。

表名是OrderT。

表格名称是SearchF

1 个答案:

答案 0 :(得分:4)

您可以通过查询或直接从表单中提取数据来执行此操作。

要在查询中执行此操作,您可以在按钮后面添加类似此(未经测试)的代码:

    Dim sSQL as String

    Set sSQL = "INSERT INTO OrderT (CustomerName,OrderName,OrderDesc,DateOfPurchase,ProjectDueDate,EngineerDueDate,ProjectComplete,CutplanDueDate,MaterialSpecs,CutplanCode,HardwareSpecs,HardwareDueDate,HardwareComplete,PurchaseOrder,PurchaseSupplier)
    VALUES (" & Me.CustomerName & "," & Me.OrderName & "," & Me.OrderDesc & "," & Me.DateOfPurchase & "," & Me.ProjectDueDate & "," & Me.EngineerDueDate & "," & Me.ProjectComplete & "," & Me.CutplanDueDate & "," & Me.MaterialSpecs & "," & Me.CutplanCode & "," & Me.HardwareSpecs & "," & Me.HardwareDueDate & "," & 
Me.HardwareComplete & "," & PurchaseOrder & "," & Me.PurchaseSupplier & ");"

    DoCmd.RunSQL sSQL

从数据表单中提取数据(我的首选方法)看起来像这个(未经测试的)代码:

Dim db as Database
Dim rec as Recordset

Set db = CurrentDB
set rec = db.OpenRecordset ("Select * from OrderT")

rec.AddNew
rec("CustomerName") = Me.CustomerName
rec("OrderName") = Me.OrderName
etc...
rec.Update

Set rec = Nothing
Set db = Nothing