Solved

Access Query from Form Data

Posted on 2009-04-07
12
614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 28

Expert Comment

by:TextReport
ID: 24088153
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24088163
is the form "frmPolicyData" open when you are running the query? it should be.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 24088226
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
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)

 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 24088309
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 34

Expert Comment

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

Author Comment

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

0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 24088525
re:> Your fuction return the correct value...

Is the problem solved?

Mike
0
 

Author Comment

by:Sariff
ID: 24088642
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
ID: 24088672
"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
ID: 24088734
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 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 250 total points
ID: 24088755
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
ID: 31567553
Thanks
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

627 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