3

When I hardcode the values and execute the SQL statement within Access 2010, the command is executed and the values required are returned.

SELECT [Event ID], [Crowd Size], [Event Name] FROM Event WHERE ([Event Date]=#8/11/2012#) AND ([Event Name]="Peter")

However when I use text boxes in place of the hardcoded values

("SELECT [Event ID], [Crowd Size], [Event Name] FROM [Event] WHERE ([Event Name]='" + textBoxEventName.Text + "') AND ([Event Date]=#" + textBoxEventDate.Text + "#)");

When trying to read from the datareader, it is unable to do so as there doesn't seem to be any values in there.

I have gone through the code in debug mode and the values in the text boxes are correct, but still doesn't happen.

9
  • Have you checked the result of the string concatenation? Commented Feb 22, 2012 at 23:20
  • 3
    Try assigning your sql string to a text variable and check using the debugger that it is as you expect. Commented Feb 22, 2012 at 23:21
  • This has nothing to do with Access since this is before it hits the database. This is going from VS form to whatever function is generating the sql. May want to step through to see what's going on. Have you tried textboxEventDate.Text.ToString("formathere")? Commented Feb 22, 2012 at 23:28
  • @adrianbanks I have assigned the textbox contents to a string variable, the debug mode shows the values as it should, but it still doesn't seem to be working. Commented Feb 22, 2012 at 23:36
  • @phoog I'm afraid I don't understand. Commented Feb 22, 2012 at 23:38

3 Answers 3

1

Access is a bit tricky with the SQL Queries that it runs...

Make sure that the date you are grabbing is in the correct format, or assign it a format your-self.

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx (Standard Date and Time Format Strings)

also make sure that there are all the quotation marks necessary and that you are not using any of the "reserved words"

http://support.microsoft.com/kb/286335 (List of reserved words in Access)

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

1 Comment

[Event Date] is a Date/Time field presumably. Check that it has nothing to do with the time component.
0

Try this: see this page: http://www.tizag.com/sqlTutorial/sqlselect.php

Comments

0

After many hours of fiddling around with this problem,

The solution is that Access SQL likes dates in the format

#YYYY/MM/DD#

Comments

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.