473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

VBA SQL statement form parameter not generating Value?

133 100+
The following code is supposed to grab the PO Amt and group the data with the date parameters from a form. However, the coded finds no data. If the date parameter from the form is removed, the code will find data to group.

Any suggestions would be appreciated.

Mary

Expand|Select|Wrap|Line Numbers
  1. Private Sub sql_Step04_b1_Extract_POScreenAmt()
  2.  
  3. On Error GoTo Err_Hndlr
  4.  
  5. '**********************************************
  6. Dim dbs As Database
  7. Dim strSQL As String
  8. Dim strQueryName As String
  9. Dim qryDef As QueryDef
  10.  
  11. 'set variable values
  12. Set dbs = CurrentDb
  13. strQueryName = "sql_Step04_b1_Extract_POScreenAmt"
  14.  
  15. 'Delete old query first - we want fresh data!
  16. dbs.QueryDefs.Delete strQueryName
  17.  
  18. 'Notice below how we inserted the variable as a parameter value - Visual Basic will evaluate strMonth and insert the value for us.
  19.  
  20.  
  21. strSQL = "SELECT POCompletedScreen.[PO #], " & _
  22.             "POCompletedScreen.[PO Total], " & _
  23.             "DateValue([Creation Date]) AS [Creation Date2]" & _
  24.          "FROM POCompletedScreen " & _
  25.          "GROUP BY POCompletedScreen.[PO #], " & _
  26.          "POCompletedScreen.[PO Total], " & _
  27.             "DateValue([Creation Date]) " & _
  28.          "HAVING (((DateValue([Creation Date])) Between [Forms]![F_Waiver_Yr]![txb_date_start] And [Forms]![F_Waiver_Yr]![txb_date_end]))" & _
  29.          "ORDER BY POCompletedScreen.[PO #];"
  30.  
  31. 'Create query definition
  32. Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)
  33.  
  34.  
  35.  
  36. sql_Step04_b1_Extract_POScreenAmt_Exit:
  37.   Exit Sub
  38.  
  39.  
  40. Err_Hndlr:
  41.     MsgBox "[" & Err.Number & "]:  " & Err.Description, vbInformation, "sql_Step04_b1_Extract_POScreenAmt()"
  42. End Sub
  43.  
Sep 30 '10 #1

✓ answered by dowlingm815

From what i can gather, the vba statement looks at the form parameter as a string therefore, it needs to be qualified as such:

Expand|Select|Wrap|Line Numbers
  1. Between #" & [Forms]![F_Waiver_Yr]![txb_date_start] # And #" & [Forms]![F_Waiver_Yr]![txb_date_end] & "#
  2.  

14 2258
Mariostg
332 100+
in you HAVING clause, did you try putting hard coded dates to see if it returns someting?
I am not sure about using parameter values, but normally, dates must be enclosed between #. Other wise something like 2010-03-04 will do 2010 minus 3 minus 4...
Sep 30 '10 #2
dowlingm815
133 100+
Yes, i tested with hard coded dates and it worked. however, the dates are variable.
Sep 30 '10 #3
Mariostg
332 100+
After you define strSQL, can you put a line
Expand|Select|Wrap|Line Numbers
  1. Debug.Print strSQL 
so you/we can see exactly what the query looks like after you enter the parameters in the form.
the output will be visible in the Immediate Window. You can see it by doing Ctrl+g.
If the query does not return any error, just blank output, it is the having clause that basically eliminates all records...
Sep 30 '10 #4
dowlingm815
133 100+
in the immediate window, the

Expand|Select|Wrap|Line Numbers
  1. ?[Forms]![F_Waiver_Yr]![txb_date_start]
  2. ?[Forms]![F_Waiver_Yr]![txb_date_end]
  3.  
returns, the selected date of 06/01/2010 and 06/30/2010.

this is what the debug.print returns for the SQL statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT POCompletedScreen.[PO #], POCompletedScreen.[PO Total], DateValue([Creation Date]) AS [Creation Date2]FROM POCompletedScreen GROUP BY POCompletedScreen.[PO #], POCompletedScreen.[PO Total], DateValue([Creation Date]) HAVING (((DateValue([Creation Date])) Between [Forms]![F_Waiver_Yr]![txb_date_start] And [Forms]![F_Waiver_Yr]![txb_date_end]))ORDER BY POCompletedScreen.[PO #];
  2.  
Sep 30 '10 #5
Mariostg
332 100+
Ah no wonder, it is like the [Form]... statements are taken as a pure string, you want that:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT POCompletedScreen.[PO #], " & _
  2.             "POCompletedScreen.[PO Total], " & _
  3.             "DateValue([Creation Date]) AS [Creation Date2]" & _
  4.          "FROM POCompletedScreen " & _
  5.          "GROUP BY POCompletedScreen.[PO #], " & _
  6.          "POCompletedScreen.[PO Total], " & _
  7.             "DateValue([Creation Date]) " & _
  8.          "HAVING (((DateValue([Creation Date])) Between" & _
  9.          [Forms]![F_Waiver_Yr]![txb_date_start] & " And " & _
  10.          [Forms]![F_Waiver_Yr]![txb_date_end] & ")) ORDER BY POCompletedScreen.[PO #];"
  11.  
Sep 30 '10 #6
dowlingm815
133 100+
i'm not following you, you are implying that "[Forms]![F_Waiver_Yr]![txb_date_start]" converts itself into a string.

however, i have a similar statement works which is executed prior to this sql statement. the other code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub sql_Step01_a_PO_MONTH()
  2.  
  3. On Error GoTo Err_Hndlr
  4.  
  5. '**********************************************
  6. Dim dbs As Database
  7. Dim strSQL As String
  8. Dim strQueryName As String
  9. Dim qryDef As QueryDef
  10.  
  11. 'set variable values
  12. Set dbs = CurrentDb
  13. strQueryName = "sql_Step01_a_PO_MONTH"
  14.  
  15. 'Delete old query first - we want fresh data!
  16. dbs.QueryDefs.Delete strQueryName
  17.  
  18. 'Notice below how we inserted the variable as a parameter value - Visual Basic will evaluate strMonth and insert the value for us.
  19.  
  20.  
  21. strSQL = "SELECT FIMSMGR_FPBPOHD.FPBPOHD_CODE, FIMSMGR_FPBPOHD.FPBPOHD_TRANS_DATE, FIMSMGR_FPBPOHD.FPBPOHD_PO_DATE, FIMSMGR_FPBPOHD.FPBPOHD_CHANGE_SEQ_NUM, FIMSMGR_FPBPOHD.FPBPOHD_PCLS_CODE, FIMSMGR_FPBREQH.FPBREQH_DOC_REF_CODE, FIMSMGR_FPBPOHD.FPBPOHD_BUYR_CODE, FIMSMGR_FPRPODT.FPRPODT_COMM_DESC, Sum(FIMSMGR_FPRPODA.FPRPODA_AMT) AS SumOfFPRPODA_AMT, FIMSMGR_FPRPODT.FPRPODT_COMM_CODE " & _
  22.         "FROM FIMSMGR_FPRPODA " & _
  23.         "INNER JOIN (FIMSMGR_FPRPODT INNER JOIN " & _
  24.             "((FIMSMGR_FTVRQPO RIGHT JOIN FIMSMGR_FPBPOHD ON FIMSMGR_FTVRQPO.FTVRQPO_POHD_CODE = FIMSMGR_FPBPOHD.FPBPOHD_CODE) " & _
  25.             "LEFT JOIN FIMSMGR_FPBREQH ON FIMSMGR_FTVRQPO.FTVRQPO_REQD_CODE = FIMSMGR_FPBREQH.FPBREQH_CODE) " & _
  26.             "ON FIMSMGR_FPRPODT.FPRPODT_POHD_CODE = FIMSMGR_FPBPOHD.FPBPOHD_CODE) " & _
  27.             "ON (FIMSMGR_FPRPODA.FPRPODA_POHD_CODE = FIMSMGR_FPRPODT.FPRPODT_POHD_CODE) " & _
  28.             "AND (FIMSMGR_FPRPODA.FPRPODA_POHD_CODE = FIMSMGR_FPBPOHD.FPBPOHD_CODE) " & _
  29.             "AND (FIMSMGR_FPRPODA.FPRPODA_CHANGE_SEQ_NUM = FIMSMGR_FPBPOHD.FPBPOHD_CHANGE_SEQ_NUM) AND (FIMSMGR_FPRPODA.FPRPODA_CHANGE_SEQ_NUM = FIMSMGR_FPRPODT.FPRPODT_CHANGE_SEQ_NUM) " & _
  30.         "GROUP BY FIMSMGR_FPBPOHD.FPBPOHD_CODE, FIMSMGR_FPBPOHD.FPBPOHD_TRANS_DATE, FIMSMGR_FPBPOHD.FPBPOHD_PO_DATE, FIMSMGR_FPBPOHD.FPBPOHD_CHANGE_SEQ_NUM, FIMSMGR_FPBPOHD.FPBPOHD_PCLS_CODE, FIMSMGR_FPBREQH.FPBREQH_DOC_REF_CODE, FIMSMGR_FPBPOHD.FPBPOHD_BUYR_CODE, FIMSMGR_FPRPODT.FPRPODT_COMM_DESC, FIMSMGR_FPRPODT.FPRPODT_COMM_CODE, FIMSMGR_FPRPODT.FPRPODT_ITEM " & _
  31.         "HAVING (((FIMSMGR_FPBPOHD.FPBPOHD_TRANS_DATE) Between [Forms]![F_Waiver_Yr]![txb_date_start] And [Forms]![F_Waiver_Yr]![txb_date_end]) AND ((FIMSMGR_FPRPODT.FPRPODT_ITEM)=1))" & _
  32.         "ORDER BY FIMSMGR_FPBPOHD.FPBPOHD_TRANS_DATE DESC;"
  33.  
  34.  
  35. 'Create query definition
  36. Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)
  37.  
  38.  
  39.  
  40. sql_Step01_a_PO_MONTH_Exit:
  41.   Exit Sub
  42.  
  43.  
  44. Err_Hndlr:
  45.     MsgBox "[" & Err.Number & "]:  " & Err.Description, vbInformation, "sql_Step01_a_PO_MONTH()"
  46. End Sub
  47.  
Sep 30 '10 #7
Mariostg
332 100+
Ok you can disregard my last, it works differently when you use a query def than when you use a domcd.runsql which I am used to.

Not sure now why it would not work at the moment. I will sleep on that tonight.
Sep 30 '10 #8
MMcCarthy
14,534 Expert Mod 8TB
You are missing #'s to define your start and end date.

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT POCompletedScreen.[PO #], " & _
  2.             "POCompletedScreen.[PO Total], " & _
  3.             "DateValue([Creation Date]) AS [Creation Date2]" & _
  4.          "FROM POCompletedScreen " & _
  5.          "GROUP BY POCompletedScreen.[PO #], " & _
  6.          "POCompletedScreen.[PO Total], " & _
  7.             "DateValue([Creation Date]) " & _
  8.          "HAVING (((DateValue([Creation Date])) Between #[Forms]![F_Waiver_Yr]![txb_date_start]# And #[Forms]![F_Waiver_Yr]![txb_date_end]#))" & _
  9.          "ORDER BY POCompletedScreen.[PO #];"
Sep 30 '10 #9
dowlingm815
133 100+
nope, the # has no affect on it. i tried and got the same results. moreover, it works on the other sql statements.
Oct 1 '10 #10
colintis
255 100+
Can you tell me a bit about the [Creation Date] you used in the DateValue? What format is inside this field? The problem might comes from it.
Oct 1 '10 #11
dowlingm815
133 100+
Good morning,

The [Creation date] is a date type field. Originally, it was a string but is was converted in the back-end.
Oct 1 '10 #12
dowlingm815
133 100+
From what i can gather, the vba statement looks at the form parameter as a string therefore, it needs to be qualified as such:

Expand|Select|Wrap|Line Numbers
  1. Between #" & [Forms]![F_Waiver_Yr]![txb_date_start] # And #" & [Forms]![F_Waiver_Yr]![txb_date_end] & "#
  2.  
Oct 1 '10 #13
colintis
255 100+
When you look into the [Creation date] from the table view, is it simply in the format of mm/dd/yyyy?

Normally by default, SQL recogned date fields as mm/dd/yyyy format, so is your [Creation date] originally a DATE/TIME datatype? if so then you try the code again without DateValue
Oct 2 '10 #14
dowlingm815
133 100+
the DateValue was dropped after, the code was corrected with the above which is re-posted below. i agree less code the more efficient. thank you for your assistance.

mary

Expand|Select|Wrap|Line Numbers
  1. Between #" & [Forms]![F_Waiver_Yr]![txb_date_start] # And #" & [Forms]![F_Waiver_Yr]![txb_date_end] & "#
  2.  
Oct 2 '10 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Abdul Mohsin | last post by:
is it possible to get hidden parameter values using window.opener.document.form.parameter.value. I am getting this error " Object not found" Please help Abdul
1
by: Chuck | last post by:
I am writing a database program in C#. I have a situation when I type the '(' after typing a method call, VS goes into an infinite loop. I then have to exit VS. Has anyone else had any trouble...
1
by: Jurij P | last post by:
I have a problem. I would like to change a value from textbox with JavaScript before I submit the form (submit updated value). Can anyone please help me? thank you, Jure
1
by: Mike Bridge | last post by:
I'm getting a nonsensical error when binding a DataTable with one row to a DropDownList. The DataTable's row contains the fields "id" (INT), "description" (VARCHAR) and with a couple other...
9
by: Ale K. | last post by:
What's the best way to make a form return a value.... i want to try to avoid using module variables for doing this... Thanks. Alex.
5
by: Lisa J. | last post by:
Heloe I would like to be able to pass a condition of an IF statement as parameter as follow aStr=” (A=1) and (B=2)” if aFunc(“aStr”) then …. End if
0
by: darrel | last post by:
What does this error mean? Specified argument was out of the range of valid values. Parameter name: value System.Web It's being thrown here: DDL_SubCategory.Enabled = True...
7
by: thisis | last post by:
Hi All, myGetImage.asp is suppose to create a temporary file in the client side, and display in on the client web browser. myGetImage.asp needs to know the clients web browser temporary...
8
by: Dean Slindee | last post by:
I have a form whose Property value I need to get at from a class (contained in another project, same solution). Here is the form's property: Private booIsInsert As Boolean = False Public...
49
dima69
by: dima69 | last post by:
Hi all. Here is a problem. I want to sort a form by absolute value. Let's say, if I have a field named "theSum", I'd like to set the form OrderBy property to "Abs()". If I use "Advanced...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.