Solved Syntax Error (1 Viewer)

PatAccess

Registered User.
Local time
Today, 04:39
Joined
May 24, 2017
Messages
284
Hello Guys,
I am trying to familiarize myself with Function in MS Access VBA. Does it not return strings. The following code give me an error message
Code:
Function getTraining(strTraining As String) As String
    strTraining = "SELECT DISTINCT [Training] FROM QryTrainedEmployees ORDER BY [Training]"
    Return strTraining
End Function

Why?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:39
Joined
Aug 30, 2003
Messages
36,133
You want the function to return the string? Try

getTraining = strTraining

Though it is odd that you taking strTraining as an input but not using it.
 

PatAccess

Registered User.
Local time
Today, 04:39
Joined
May 24, 2017
Messages
284
You want the function to return the string? Try

getTraining = strTraining

Though it is odd that you taking strTraining as an input but not using it.
I put it as a parameter because I thought it was needed but if I dont want it do I do?
Code:
Function getTraining() As String
    getTraining = "SELECT DISTINCT [Training] FROM QryTrainedEmployees ORDER BY [Training]"
    Return strTraining
End Function
Because that doesn't work either
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:39
Joined
Feb 28, 2001
Messages
27,321
The "Return x" syntax trips me up sometimes, too. It is one of the differences between VBA and VB. Return x in VB returns a value for the function, but in VBA the Return does not use an argument. As Paul has noted, you need the explicit assignment expression in VBA.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:39
Joined
Aug 30, 2003
Messages
36,133
This

Return strTraining

has to be this

getTraining = strTraining
 

PatAccess

Registered User.
Local time
Today, 04:39
Joined
May 24, 2017
Messages
284
This

Return strTraining

has to be this

getTraining = strTraining
I guess I am confuse as to where I assign the string? Because the following does not work either or do you mean I do not use the keyword "Return"? So like this?
Code:
Function getTraining() As String
    Dim sqlTraining As String
    sqlTraining = "SELECT DISTINCT [Training] FROM QryTrainedEmployees ORDER BY [Training]"
    getTraining = sqlTraining
End Function
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:39
Joined
Aug 30, 2003
Messages
36,133
Yes, like that. You understand that's going to return a SQL string, not the result of that string?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:39
Joined
Aug 30, 2003
Messages
36,133
Happy to help!
 

Isaac

Lifelong Learner
Local time
Today, 01:39
Joined
Mar 14, 2017
Messages
8,871
What do you want to do in plain english?
 

Users who are viewing this thread

Top Bottom