1

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

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

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

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

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

文本框名称: CustomerName OrderName OrderDesc DateOfPurchase ProjectDueDate EngineerDueDate ProjectComplete CutplanDueDate MaterialSpecs CutplanCode HardwareSpecs HardwareDueDate HardwareComplete PurchaseOrder PurchaseSupplier

表中的字段具有相同的名称,从顶部 -> 底部,左侧 -> 右侧的顺序完全相同。

表名是 OrderT。

表单名称是 SearchF

4

1 回答 1

5

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

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

    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
于 2013-10-24T13:03:18.133 回答