Solved

Access Query from Form Data

Posted on 2009-04-07
12
608 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Sariff
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 28

Expert Comment

by:TextReport
Comment Utility
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
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
is the form "frmPolicyData" open when you are running the query? it should be.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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

0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
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
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
coorection...
MsgBox Nz((Forms]![frmPolicyData]![txtPOL_NUM],"<All>")   'Remve it after a test
 
0
 

Author Comment

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

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
re:> Your fuction return the correct value...

Is the problem solved?

Mike
0
 

Author Comment

by:Sariff
Comment Utility
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.
0
 
LVL 28

Expert Comment

by:TextReport
Comment Utility
"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

0
 
LVL 28

Accepted Solution

by:
TextReport earned 250 total points
Comment Utility
If it is a DELETE wuery then you can do a
    qd.execute dbfailonerror
Cheers, Andrew

PS As requester it would help if you confirmed how you are running the SQL
0
 
LVL 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 250 total points
Comment Utility
re:> I need the SQL to take the input from a cotnrol on a form

Do you mean from:  Forms]![frmPolicyData]![txtPOL_NUM]

If not, what control you mean?

Re:> use in a DELETE query

If you want to delete, use

CurrentDB.Execute "Delete * From TBL_MAIN Where PH_NUM = fnPH_NUM();"
With:
Function  fnPH_NUM() As Long
On Error GoTo 10

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

   fnPH_NUM() =91919191

Else

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

End IF

Exit Function
10:
   fnPH_NUM() 91919191
End Function  
Assuming fnPH_NUM is long and never is equal to 91919191
0
 

Author Closing Comment

by:Sariff
Comment Utility
Thanks
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now