Solved

Access Query from Form Data

Posted on 2009-04-07
12
611 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
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

821 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