Link to home
Start Free TrialLog in
Avatar of Sariff
Sariff

asked on

Access Query from Form Data

Experts - I am having trouble getting the data to feed to the below query from a Form. If I enter the criteria directly it works fine.
SELECT TBL_MAIN.PH_NUM, TBL_MAIN.EE_STATUS
FROM TBL_MAIN
WHERE (((TBL_MAIN.PH_NUM)=[Forms]![frmPolicyData]![txtPOL_NUM]));

Open in new window

Avatar of TextReport
TextReport
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you please check the exact spelling of the form name and that it is open at the time you are running the query.
Can you please check the exact name of the control on the form.

Other than that it looks correct, does it prompt you for a value? Is so then either the form is not open in FORM or DATASHEET view or the control name is wrong.

Cheers, Andrew
Avatar of Rey Obrero (Capricorn1)
is the form "frmPolicyData" open when you are running the query? it should be.
Use the SQL below. Also add following in a module:

Function  fnPH_NUM() As Variant
On Error GoTo 10

If Nz([Forms]![frmPolicyData]![txtPOL_NUM],"")="" Then
     fnPH_NUM() ="<All>"
Else
    fnPH_NUM() =Forms]![frmPolicyData]![txtPOL_NUM]
End IF

Exit Function
10:
     fnPH_NUM() ="<All>"
End Function
 
Mike

SELECT PH_NUM, EE_STATUS FROM TBL_MAIN WHERE PH_NUM= iif(fnPH_NUM()="<all>",[PH_NUM],fnPH_NUM());

Open in new window

Also make sure the MsgBox (Forms]![frmPolicyData]![txtPOL_NUM],"<All>") returns the expected value. Remve it after a test.

Function  fnPH_NUM() As Variant
On Error GoTo 10

MsgBox (Forms]![frmPolicyData]![txtPOL_NUM],"<All>")   'Remve it after a test

If Nz([Forms]![frmPolicyData]![txtPOL_NUM],"")="" Then

    fnPH_NUM() ="<All>"

Else

   fnPH_NUM() =Forms]![frmPolicyData]![txtPOL_NUM]

End IF

Exit Function
10:
    fnPH_NUM() ="<All>"
End Function
coorection...
MsgBox Nz((Forms]![frmPolicyData]![txtPOL_NUM],"<All>")   'Remve it after a test
 
Avatar of Sariff
Sariff

ASKER

Text/Cap: The form was open during the executing of the SQL...
eghtebas: Your fuction return the correct value...

re:> Your fuction return the correct value...

Is the problem solved?

Mike
Avatar of Sariff

ASKER

Unfortunately, no...

I need the SQL to take the input from a cotnrol on a form for use in a DELETE query. I am testing it in SELECT.
"The form was open during the executing of the SQL..." therefore it goes to how you are running the SQL
If you are opening a DAO recordset from the query then you use the PARAMETERS of the QueryDef object to assign the values to the FORMS reference.
Cheers, Andrew
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim cnt As Long
 
    Set db = CurrentDB
    Set qd = db.QueryDefs("MyQuery")
    For cnt = 0 to qd.parameters.count -1
        qd.parameters(cnt) = Eval(qd.parameters(cnt).Name)
    Next cnt
    Set rs = qd.OpenRecordSet()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of TextReport
TextReport
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sariff

ASKER

Thanks