Solved

Access Query from Form Data

Posted on 2009-04-07
12
612 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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