Solved

Access Query from Form Data

Posted on 2009-04-07
12
610 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 33

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 33

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 33

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 33

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 33

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

813 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

9 Experts available now in Live!

Get 1:1 Help Now