View Full Version : Derived Value in Textbox


forefront
02-22-2002, 06:10 AM
Ok Ive spent too much time trying to figure this one out by myself, so here I am.

In my form, (named "Project"), I am trying to populate the value of a textbox with the results of a Query (named "QueryName"), and I get the 'ole "#Name?" in the textbox. An input to the query is a value from the form, (called "project_id").

The Table in the Query ("TABLEA")is a Linked Table to a SQL Server 2000 database
Here's my Query, named "QueryName":

PARAMETERS [Forms]![Project]![project_id] Long;
SELECT Sum(TABLEA.budgeted_hours) AS Expr1
FROM TABLEA
WHERE (((TABLEA.project_id)=[Forms]![Project]![project_id]));

When I execute the query, stand alone without the form, it works correctly, prompts me for an input, and returns a single row with the correct value in the column "Expr1"

In my textbox properties, my contol source is set to:
=[QueryName]![Expr1]

But "#Name? appears.

What am I doing incorrectly?
thank you

Rich
02-22-2002, 06:19 AM
Since the query is only returning one record then set the Forms record source to the query, the text box will be the field from the query returning the value.
HTH

forefront
02-22-2002, 06:43 AM
Rich, that doesnt work because The Form itself has many other fields in it, and their values come from a record source thats a table. This partuicular text box that Im havin a problem with is a derived field which gets its value from the Query.
Any other ideas? Anyone?

Rich
02-22-2002, 11:42 AM
Use DLookup on the query or a combo box to display the value.

forefront
02-22-2002, 12:13 PM
Nope, that definately does not do the trick. Sigh.

Rich
02-22-2002, 12:44 PM
So I assume the form with the parameter you are referencing is the same form that your trying to display the record on is that correct? If so then does the query return the correct value if you open it when the form is open?
Actually on second thoughts it looks as though your parameters are incorrect, you don't put brackets around them

[This message has been edited by Rich (edited 02-22-2002).]

forefront
02-22-2002, 01:07 PM
Rich,
Yes thats correct that the form that i want the result displayed is the same form from which I get the input parameter. And yes, when the form is open, and I run the query, I do get the correct result in the query result window.
I believe that brackets are optional (used for when spaces exist in names), either way, with or without brackets it doesnt work. Actually, with or without the "PARAMETERS" line it still doesnt work...
Could it be that, despite the fact that one value is being returned, its actually a result set of one column, but of only one row? I was trying to avoid executing the SQL statement from VBA code to test that theory out but it looks like i might have to.

forefront
02-25-2002, 09:22 AM
rich,
I solved it! I dont know if this is necessarily the easiest way, but it works.
On the form that is displaying all the info,
I wrote a VB OnCurrent Subroutine. The Subroutine creates a SQL statement based on info from the form, and then executes it on the DB, then assigns the forms text field the return value:

Private Sub Form_Current()

Dim db As Database
Dim rec As Recordset
Dim strSQLStatement As String
Dim strWhereStatement As String
Dim strResult As String

Set db = CurrentDb()
strWhereStatement = [Forms]![Project]![project_id]
strSQLStatement = "select sum(column) from TABLEA where project_id = " & strWhereStatement
Set rec = db.OpenRecordset(strSQLStatement, dbOpenDynaset)
strResult = rec(0)
rec.Close
[Forms]![Project]![sum_field] = strResult

End Sub

I dont know whether it has to do with the fact that All my tables are Linked to a SQL Server DB, or not, but it works now.

Thanks for you suggestions.

Hope this helps anyone else out.