Re: How to return a result of a SQL query within an Event Procedure (&what am I doing wrong here)?
- From: raylopez99 <raylopez99@xxxxxxxxx>
- Date: Wed, 2 Jan 2008 02:49:27 -0800 (PST)
Thanks, I figured out a workaround, see below.
RL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Text6_Click() 'Some textbox that you want to run the SQL
query when you click on it
Dim TempVar As Variant
Dim Str01 As String
Str01 = Str001 'global string Str001 set to local string Str01
Dim Str02 As String
Str02 = "SELECT Count(" + Str01 + ") AS Expr1 FROM Table1;" 'combines
two strings, Str02 and Str01, as one string, with Str02 having the
query and the parameter being supplied by the Str01 string, which is
what you want, note how the apostrophes are used
TempVar = GetResult(Str02) ' the result of the SQL query, usually a
scalar number for simple queries
Dim TempText As String
TempText = CStr(TempVar) 'convert number to string
Text6.Value = TempText 'output the number, as a string, to your
textbox Text6
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' put this code in your Global area of your [Event Procedure]
namespace, outside any Sub
Dim Str001 As String 'global variable Str001
Public Function GetResult(sSQL As String) As Variant
On Error GoTo err01 'add error trap
Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rs = db.OpenRecordset(sSQL)
If Not rs.EOF Then
GetResult = rs(0)
rs.Close
End If
db.Close
Set rs = Nothing
Set db = Nothing
GoTo line001XYZ 'you need this I've found to avoid running the next
few lines, for some strange reason
err01: Debug.Print "GetResult error: SQL=" & sSQL & ". Error: " & Error
$
'Debug prints to debug window (useful when debugging, after the
program has run)
line001XYZ: 'skips line err01 and the ones after, if no mistake at
GoTo err001
End Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Jan 1, 5:45 pm, Marshall Barton <marshbar...@xxxxxxxxxx> wrote:
I suggest that you remove the parameter from the query and
just use the DCount function:
Private Sub OUTPUT_Click()
Dim TempVar As Long
TempVar = DCount("*", "Query1", "[some field] = 'a' ")
. . .
--
Marsh
MVP [MS Access]
.
- Follow-Ups:
- References:
- Prev by Date: Re: Access Licensing (distribution question)
- Next by Date: Re: New Wine in Old Bottles problem (never use an old form on a new table schema / architecture)
- Previous by thread: Re: How to return a result of a SQL query within an Event Procedure (&what am I doing wrong here)?
- Next by thread: Re: How to return a result of a SQL query within an Event Procedure (&what am I doing wrong here)?
- Index(es):
Relevant Pages
|