2

我有两张桌子,我有一个链接到其中一张的表格。我想检查一个值,如果为真,则使用 VBA 将记录添加到另一个表中。任何人都可以帮助我吗?

这是我的代码,但它不起作用:

Dim rec1 As DAO.Recordset
Dim rec2 As DAO.Recordset

Set rec1 = CurrentDb.OpenRecordset("TotalTPAq")
Set rec2 = CurrentDb.OpenRecordset("Visi")

rec1.MoveFirst
Do Until rec1.EOF

    If rec1!Date = PlanDate.Value Then ' planDate is a text box
        rec2.AddNew
        rec2![Planing Date History] = PlanDate.Value
        rec2.Update
        rec2.Close
    End If
    rec1.MoveNext
Loop
rec1.Close

Set rec2 = Nothing
Set rec1 = Nothing

DoCmd.Close
4

1 回答 1

3

这应该为您提供一个开始:

'Run query to fill table
Private Sub btnRnQry_Click()

    'No value entered
    If IsNull(Me.txtEntry) Or Me.txtEntry = "" Then
        MsgBox ("Is null or empty")
    Else
        'Assign value to variable
        Dim entry As String
        entry = Me.txtEntry

        Dim sql As String
        sql = "INSERT INTO tableTwo ([First Name],Surname,[Phone Number] )" & _
              "SELECT * " & _
              "FROM tableOne " & _
              "WHERE [First Name] = '" & entry & "';"

        'Run the SQL
        DoCmd.RunSQL sql

    End If

End Sub
于 2013-04-04T13:26:50.480 回答