Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
i just faced a problem when trying to execute the following query.I searched for the exception on the internet and found that using a reserved keyword might raise this issue,and using brackets may solve this problem.But i use brackets all the time in my sql statements but still the exception 'IErrorInfo.GetDescription failed with E_FAIL(0x80004005).' comes up.
Please give me some hand.
VB
qry2 = "SELECT * FROM [per_diem_accomodation] WHERE [project number]='" & projcode & "' AND [employee number]='" & empcode & "'" & _
        "AND [current month]= (SELECT MAX(current month) FROM [per_diem_accomodation] WHERE [project number]='" & projcode & "' AND [employee number]='" & empcode & "')"
Posted
Comments
Richard C Bishop 11-Apr-14 10:21am    
You don't have a space before the 2nd "AND" in your query.
Maciej Los 11-Apr-14 10:25am    
Hawk eye!
Richard C Bishop 11-Apr-14 10:34am    
Thank you Maciej! Now if only I could be that diligent with my own work. :laugh
[no name] 11-Apr-14 10:28am    
I would upvote that one.
Richard C Bishop 11-Apr-14 10:35am    
Very well, how about we both post a solution as yours is good as well.

Your spacing in your query is slightly off, see the alteration below:
qry2 = "SELECT * FROM [per_diem_accomodation] WHERE [project number]='" & projcode & "' AND [employee number]='" & empcode & "'" & _

     " AND [current month]= (SELECT MAX(current month) FROM [per_diem_accomodation] WHERE 

[project number]='" & projcode & "' AND [employee number]='" & empcode & "')"


The second "AND" in your query has a space before it.
 
Share this answer
 
Comments
Maciej Los 11-Apr-14 11:14am    
I need to repeat it: Hawk eye ;)
+5!
Richard C Bishop 11-Apr-14 11:18am    
Too kind!
[no name] 11-Apr-14 12:11pm    
And there you go.
Richard C Bishop 11-Apr-14 12:12pm    
Thank you sir!
Nebilo wrote:

@Richard C Bishop I think it's not the spacing issue,i've tried it but still it doesn't work


OK, second error is here: MAX(current month), replace with: MAX([current month])

You had used one of Access 2007 reserved words and symbols[^]: 'month' ;)

[EDIT]
But i'd suggest you to change the way you're trying to achieve that. As Wes Aday had mentioned, use parametrized queries:

SQL
PARAMETERS [projcode] CHAR, [empcode] CHAR; 
SELECT *
FROM [per_diem_accomodation]
WHERE [project number]= [projcode] AND [employee number]= [empcode] AND [current month]= 
    (SELECT MAX([current month])
     FROM [per_diem_accomodation]
     WHERE [project number]=[projcode AND [employee number]=[empcode]);



Then run OleDbCommand with named parameters[^].

More about: PARAMETERS declaration (MS Access)[^]


[/EDIT]
 
Share this answer
 
v4
Comments
Richard C Bishop 11-Apr-14 11:32am    
Also a nice catch! +5
Maciej Los 11-Apr-14 11:34am    
Thank you, Richard. I had updated my solution about another useful information.
[no name] 11-Apr-14 12:11pm    
Good eye yourself.
Maciej Los 11-Apr-14 12:16pm    
Thank you, Wes ;)
I could have sworn that you look like "Gandalf the Gray" ;)
[no name] 11-Apr-14 12:22pm    
Well some days I feel more like a wizard than others. Doing the whole psychic trying to read the OPs mind things. Maybe someday I will learn how to do that and get the white :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900