SQL statement returning #Name?

Ace71425

Board Regular
Joined
Apr 20, 2015
Messages
130
Hello...I'm trying to run an SQL statement to make a textbox equal a search of a query based on criteria in labels in my form..Now this SQL statement works from Excel to Access but not in Access in my form. I'm thinking that it has to do with in Excel the field I was searching to match Datestamp was actually date formatted and of course the label in the Access form is not a formatted date

Code:
Me.Text371.ControlSource = "SELECT SumOfExpr3 FROM MakePercentages WHERE Datestamp = " & Day27.Caption & " And Rep = " & Label7.Caption & ""

So Rep is a text format and Datestamp is a date format in the table from where the query grabs it from...I need to make that Text371 equal to one of the query results in MakePercentages based on those conditions. Any ideas on what is going wrong with the formatting of this statement here as I know the statement itself does work just coming from Excel. Thanks! Let me know if you need more info.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
My mistake both the fields from the table are text formats either way what's going on with this statement lol
 
Upvote 0
My mistake both the fields from the table are text formats either way what's going on with this statement lol

then try this

Code:
Me.Text371.ControlSource = "SELECT SumOfExpr3 FROM MakePercentages WHERE Datestamp = '" & Day27.Caption & "' And Rep = '" & Label7.Caption & "' "

there are some single quotes mixed with double quotes in there so make sure you copy it correctly
 
Upvote 0
I'll try that first thing tomorrow morning...if it works you are a scholar sir because that was 5 failed hours of my day today
 
Upvote 0
Nothing I try works....I don't understand why Access has to make it so difficult to just have a **** textbox equal to a search of a query
 
Upvote 0
Well, to say DateStamp is text is kind of bizarre 'cuz it sure smells like a date. If they are dates, the sql statement won't work as written - you need to swap the ' for #.
Substitute valid values for your variables and paste the sql into sql view of a new query and run it. If it returns more than one row, that is your problem. You cannot assign a recordset to a textbox. Try something like:
"SELECT SumOfExpr3 FROM MakePercentages WHERE Datestamp = 'DOG' And Rep = 'CAT'"

Rather than directly assign a sql string to an object, it is better to assign it to a variable so you can check the processed result in the vb editor immediate window:
Code:
Dim strSql as String
strSql = "SELECT SumOfExpr3 FROM MakePercentages WHERE Datestamp = '" & Day27.Caption & "' And "
strSql = strSql & "Rep = '" & Label7.Caption & "' 
Me.Text371.ControlSource = strSql
You put a break on the line after the assignment, then when execution stops there, type ?strSql and hit RETURN (in the immediate window) to see the construct. You can mouse over simple variable assignments to see the values, but long strings won't show.
I do not use line continuation characters in sql constructs - too messy for me.

In addition, the sql looks strange, but then I don't know what you're querying. I'm used to seeing something more like
SELECT Sum(tablename1.fieldname1) AS Something WHERE...GROUP BY tablename2.fieldname2;
 
Upvote 0

Forum statistics

Threads
1,221,657
Messages
6,161,084
Members
451,684
Latest member
smllchng5

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top