Trying to run append query in vba

Anonymous
2022-12-13T20:37:08+00:00

Can someone please help me understand why this doesn't work and what the best fix is please? Thank You!

Microsoft 365 and Office | Access | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2022-12-13T22:05:49+00:00

    DAO operates at the database level, no at the Access interface level. So it doesn't "know" anything about Access forms and controls. Try this:

    Private Sub Command2_Click()
        Dim sql As String
        Dim dbs As DAO.Database
        sql = "INSERT INTO [TargetTable] (CompanyName, ParentRefFullName, [Name], JobDesc) " & _
            "VALUES ('" & Forms!Form1!Text1 & "', " & _
                    "'" & Forms!Form1!Text1 & ":" & Forms!Form1!Combo1 & "', " & _
                    "'" & Forms!Form1!Text2 & "', " & _
                    "'" & Forms!Form1!Text3 & "')"
        Set dbs = CurrentDb
        dbs.Execute sql, dbFailOnError
    End Sub
    

    Instead of embedding the form references in the sql, it concatenates them at runtime.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful