Re: Expression Builder basic question on syntax / Or / how to incorporate a SQL query into a textbox control?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Dec 28, 9:56 am, raylopez99 <raylope...@xxxxxxxxx> wrote:
I would like to add a textbox on my control that is not linked to a
database (like the other textboxes are, which are linked to fields in
the database), that takes the sum of all the textboxes in the form.
How to do this using "Expression Builder"? (Or otherwise?). I'm
having problems with the syntax, since I keep getting the "#Name?"
runtime error on the textbox. **See below, I also would like to know
using Visual Basic if possible.

Concrete example:

Field #1 (linked to a database): Quantity
Field #2 (" ") : Price
Textbox #3 (this I want to compute from fields #1 and #2, in a textbox
control on my Access 2003 form): = Quantity * Price

Any way appreciated. What I did was run a SQL query ("SELECT
Sum(Subform1.Quantity*Subform1.PricePerShare) AS SumTotal
FROM AccountStocks INNER JOIN Subform1 ON
AccountStocks.AccountStockID=StkTransactions.Account_StockID
GROUP BY AccountStocks.AccountStockID;) <--two tables, linked by a
key, the query being called "Query1" having alias SumTotal

This SQL query works (when run seperately), giving a sum for each
record, and just for learning purposes I'd like to incorporate this
query using the "Expression builder" for the textbox #3.

However, I'll take any easy way of doing the above.

Thank you

RL

PS--I tried various things, and nothing seems to work. For example I
took the name of the Textbox, Textbox3, and set it equal to ("Query1!
SumTotal"). Then....(various other things tried deleted)...

UPDATE: Ok, surfing the net before I hit enter (sorry I'm on a dialup
modem right now, so surfing is painful) I see that a lot of people
don't use the Expression Builder, instead they like to use Visual
Basic in an [event procedure] That's fine, I'd like to know how to
incorporate the SQL query above...do I use it in an event procedure
(and which one? There are so many, in the past for other stuff I use
"OnGotFocus" but I doubt that's the best for this query since the user
would have to click on the box to get an answer).

Also, do I set the SQL query as a "Control Source" under the
Properties tab of the textbox #3? (Just tried that, using an equal
sign in front, and without it, and it doesn't work, still gives the
#Name? scope problem again).

Any ideas? This question is very basic so please don't think too
hard...(I think).

RL

Hi,

Here's a cheap and cheerful way to get an SQL result into a textbox.
Put the following code in a module:

Public Function GetResult(sSQL As String) As Variant

On Error GoTo err
Dim rs As Recordset

InitDB

Set rs = db.OpenRecordset(sSQL)
If Not rs.EOF Then
GetResult = rs(0)
rs.Close
End If

Set rs = Nothing
Exit Function
err:
Debug.Print "GetResult error: SQL=" & sSQL & ". Error: " & Error$

End Function

And then you can either set the control source of the textbox to:

=GetResult("select count(*) from SomeTable")

or use VBA to call the function and set the textbox's value to the
result.

Hope this helps,
James
.



Relevant Pages

  • RE: Expression Builder basic question on syntax / Or / how to incorpor
    ... it is always best to avoid unnecessary hits on the database. ... Use only the control name without the Me. ... runtime error on the textbox. ... This SQL query works, ...
    (microsoft.public.access.formscoding)
  • Expression Builder basic question on syntax / Or / how to incorporate a SQL query into a textbox con
    ... the database), that takes the sum of all the textboxes in the form. ... runtime error on the textbox. ... control on my Access 2003 form): ... This SQL query works, ...
    (microsoft.public.access.formscoding)
  • Form questions
    ... I have a single textbox and submit button on the form. ... enter a string in the textbox and query the Name_Last column for that ... map the textbox value to the SQL query. ... I am not sure how to map @LastName to textbox1.text. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Form questions
    ... I have a single textbox and submit button on the form. ... enter a string in the textbox and query the Name_Last column for that ... map the textbox value to the SQL query. ... I am not sure how to map @LastName to textbox1.text. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Store textbox value in to table
    ... you don't need to store their sum. ... you'll need to both store it AND keep it and all the fields ... a code that sums up all the textbox values in to ?Textbox30?. ... table field in the database. ...
    (microsoft.public.access.forms)