首页 > 解决方案 > How to pass variables from VBA through SQL?

问题描述

I'm trying to pass values from a Form into my database, via SQL:

Private Sub Befehl4_Click()

Dim SQL As String
Dim unique As String

unique = Forms!frm_test!PN

SQL = "INSERT INTO tab_test (Feld1) VALUES (unique)"
DoCmd.RunSQL SQL

End Sub

When I execute this code from my button in my Form, unique isn't passed to the SQL-command. I have to type it in. When writing the SQL-command this way:

SQL = "INSERT INTO tab_test (Feld1) VALUES (Forms!frm_test!PN)"

It works how expected.

What am I missing or doing wrong?

标签: sqlvbams-access

解决方案


SQL = "INSERT INTO tab_test (Feld1) VALUES ('" & Forms!frm_test!PN & "')"


推荐阅读