Vanthelost Vanthelost - 1 year ago 136
SQL Question

VBA run time Error 3129 Illegal SQL-Statement

I'm trying to make a VBA Code to run a query when someone clicks on the button.

It always displays me the error in the title when I run it. Can someone help mes please? I'm thankful for every response.

Option Compare Database
Option Explicit

Sub btnNamebox_Click()

DoCmd.OpenForm "F-Smartphone-Hinzufuegen"
Dim strSQL As String
DoCmd.RunSQL (strSQL)

strSQL = "SELECT Nachname" & _
"FROM tblPersonal " & _
"WHERE Handynummer = " & Forms![F-Smartphone-Hinzufuegen]![FKHandynummer] & ";"

txtnamebox.Value = strSQL

End Sub

Answer Source

Your code makes little sense - you run a SQL statement before you define it, and you can't RunSQL a SELECT statement anyways.

Use DLookup() instead:

S = Nz(DLookup("Nachname", _
        "tblPersonal", _
        "Handynummer = " & Forms![F-Smartphone-Hinzufuegen]![FKHandynummer]), "")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download