-1

I can't get it work in VBA althought it works within access. Which is the mistake?

Private Sub Command517_Click()
Dim SQL As String

    SQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
    " MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
    " MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
    " MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
    " FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
    " ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
    "(((Tags.Tag) = [Forms]![MacroAttività]![Text511]) " & _
    " UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
    " MacroAttività.Descrizione, MacroAttività.ID, " & _
    " MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
    " MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
    " MacroAttività.Note, MacroAttività.Tipologia " & _
    " FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
    " WHERE (((Tags.Tag)=[Forms]![MacroAttività]![Text513])); "
DoCmd.RunSQL SQL
End Sub
1
  • What about creating a procedure with a parameter e.g. CREATE PROCEDURE GetStuff ( :tag VARCHAR(20) ) AS SELECT * FROM Stuff WHERE tag = :tag; ...then in VBA code simply invoke the proc and pass a value for the parameter value? Commented Sep 23, 2016 at 12:43

1 Answer 1

2

Form variables have to be read from outside of the SQL statement, so my solution closes the SQL statement adds the form variable and then reopens the SQL quotation. I have added string qualifiers(single quotes) around the form variables; assuming they are strings by the use of "text".

varSQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
" MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
" FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
" ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
"(((Tags.Tag) ='" & [Forms]![MacroAttività]![Text511] & "')" & _
" UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
" MacroAttività.Descrizione, MacroAttività.ID, " & _
" MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note, MacroAttività.Tipologia " & _
" FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
" WHERE (((Tags.Tag)='" & [Forms]![MacroAttività]![Text513] & "')); "

SQL is a reserved word so change your SQL variable name to something like varSQL. (Update: not a reserved word)

currentdb.execute and domcd.runSQL only work for action queries as far as I am aware. you cant execute a select query using these methods. try the following:

dim rs as recordset
dim varSQL as string

varSQL = "SELECT..."

set rs=currentdb.openrecordset(varSQL,dbOpenDynaset)

me.[text1] = rs.fieldname
'send recordset values to form

rs.close
Sign up to request clarification or add additional context in comments.

9 Comments

It gave me bracket error in the first form variable, I added a bracket here & [Forms]![MacroAttività]![Text511]) & "')" & _ then it gave the same error missing operator.
My edit should work, I moved the close bracket back in to the SQL statement.
no it's definitely not an action query. It just receives the criteria from a textbox and gives me back the results on a form. How should I open a recordset?
anything at all? Please Help
I know select isnt an action query. what I meant was you cant use currentcb.execute or docmd.runsql and have "SELECT" in your statement. i'll edit my post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.