Control Source property: Dcount: Unable to specify Multiple Condition - ms-access-2010

I have two commands in Microsoft Access 2010 that works fine individually:
=DCount("*","Order","DATE = #" & [Forms]![formOrder]![DATE] & "#")
=DCount("*","Order", "STATUS = 'ST." & [Forms]![formOrder]![StatusType] & "'")
However, when combining them it doesn't work:
=DCount("*","Order","DATE = #" & [Forms]![formOrder]![DATE] & "#" AND "STATUS = 'ST." & [Forms]![formOrder]![StatusType] & "'" )
Any explanation and a possible workaround would be much appreciated?

The third argument to domain aggregate functions like DCount() is a string that is essentially the WHERE clause of a SQL statment without the WHERE keyword. In trying to combine your criteria you are attempting to AND two strings together, which is not valid. That is, your criteria parameter is
"Condition1" AND "Condition2"
which won't work. Instead, you need to put the AND within the string itself
"Condition1 AND Condition2"
In other words, instead of
"Field1 = value1" AND "Field2 = value2"
you need to use
"Field1 = value1 AND Field2 = value2"

You've got too many double quotes:
=DCount("*","Order","DATE = #" & [Forms]![formOrder]![DATE] & "# AND STATUS = 'ST." & [Forms]![formOrder]![StatusType] & "'" )
I removed the one after the second hashmark, and one in front of the word Status.
Essentially, the third part of the DCount function is an SQL WHERE clause without the word "WHERE", and you must follow the same syntax and structure rules as with standard SQL.

Related

syntax error in ASP Classic SQL Query when using conditional IF

I am trying to sort out different elements in a ASP Classic SQL Query using IF and THEN, and have tried many different ways to get it to work .. the "problem child" is this:
StatsSQL = "SELECT COUNT(v.col) as num_not_null, COUNT(v.col) * 1.0 / COUNT(*) * 100 as percent_not_null, COUNT(*) as toltalColsNeedsFilled FROM EFP_EmploymentUser t CROSS APPLY (VALUES (t.ITAdvicedFirst),(t.ITAdvicedSecond),(t.ITDepartmentDone),(t.CFOAdvicedFirst),(t.CFOInfoProvided),(t.CFOAdvicedSecond),(t.CFODone),(t.EconomyAdviced),(t.EconomyDone)," & IF objFlowNotDone("Academy") = "yes" THEN Response.Write("(t.AcademyAdviced),(t.AcademyDone),") END IF IF objFlowNotDone("Publicator") = "yes" THEN Response.Write("(t.PublicatorAdviced),(t.PublicatorDone),") END IF & "(t.PortraitAdviced),(t.PortraitDone)) v(col) WHERE ID = '19';"
The Error I get:
Microsoft VBScript compilation error '800a03ea'
Syntax error
/flow.asp, line 271
Can anyone please help me in the right direction to solve this?
Best Regards
Stig :-)
You shouldn't be using Response.Write unless you actually want the contents of the Response.Write statement to be displayed in your output, and you really need to start a new line for your conditional statements. Here's what you appear to be trying to do. I'm assuming that your SQL is correct and the only issue here is with your VBScript.
StatsSQL = "SELECT COUNT(v.col) as num_not_null, COUNT(v.col) * 1.0 / COUNT(*) * 100 as percent_not_null, COUNT(*) as toltalColsNeedsFilled FROM EFP_EmploymentUser t CROSS APPLY (VALUES (t.ITAdvicedFirst),(t.ITAdvicedSecond),(t.ITDepartmentDone),(t.CFOAdvicedFirst),(t.CFOInfoProvided),(t.CFOAdvicedSecond),(t.CFODone),(t.EconomyAdviced),(t.EconomyDone)"
If objFlowNotDone("Academy") = "yes" THEN
StatsSQL = StatsSQL & ",(t.AcademyAdviced),(t.AcademyDone)"
End If
If objFlowNotDone("Publicator") = "yes" THEN
StatsSQL = StatsSQL & ",(t.PublicatorAdviced),(t.PublicatorDone)"
End If
StatsSQL = StatsSQL & ",(t.PortraitAdviced),(t.PortraitDone)) v(col) WHERE ID = '19';"

How to get a value of a label by query string from code behind

I passed the values of a two labels with ID Me.Lbladid1 and Lbldortodorid to query string on myform1
I tried to retrieve these values into other labels on myform2 but only the first query string seam to work but the second returns a null value. Please help. This is my code
'passing the values through query string
Response.Redirect("myform2.aspx?adsid=" & Me.Lbladid1.Text & " & dortodorid=" & Me.Lbldortodorid.Text & " ", False)
'retrieving the query string values on myform2
me.label1.text= Request.QueryString("adsid") 'this works
me.label2.text= Request.QueryString("dortodorid") 'this retuns null value
You likely need to remove the spaces from between the query arguments:
Response.Redirect("myform2.aspx?adsid=" & Me.Lbladid1.Text & "&dortodorid=" & Me.Lbldortodorid.Text , False)

Code won't execute after recordset returns using if-statement

Can anyone help me explain the following problem
In a SQL-query I get info about the occurrence of an id from another table.
Everything works here and I show a crucial snippet below
set rsRecordCount = Cn.execute(mysql_query)
dim cnt
cnt = rsRecordCount("TotalRecords")
response.write("cnt " & cnt & " id = " & id & "<br>")
rsRecordCount.Close
set rsRecordCount = nothing
So - via response.write I get info about the occurrence. The problem arises
when I want to test the cnt variable
if cnt = 0 then
response.write(".....<br>")
end if
just a simple test, the code stops execute here and I wonder why? I first thought cnt might work like a pointer pointing to a recordset and when setting it to null it would be error or undefined behaviour? But I've tried to comment out .Close and = nothing. The problem is still there.
Its really the cnt that is the prolem, If I use antoher variable - the code works again
if blablabla = 0 then
response.write("it works now<br>")
end if
How do I get around this/what did I miss
thanks!!!
I first thought cnt might work like a pointer pointing to a recordset
You were right in this assumption, but not 100% accurate. The default property of Recordset object returns a Field object, i.e. after this line:
cnt = rsRecordCount("TotalRecords")
The variable cnt is actually a Field object. Now you ask, why this line "works"?
response.write("cnt " & cnt & " id = " & id & "<br>")
That's because the Field object returns its Value property when being treated as a String.
However, when trying to compare to integer, VBScript fails to find any proper conversion and chokes.
There are two common ways to solve such a thing:
Convert the value to integer using CInt or CLng:
cnt = CLng(rsRecordCount("TotalRecords"))
Like the Response.Write above, this one first converts to String, then to Long. Profit.
Take the actual value, not the Field object:
cnt = rsRecordCount.Fields("TotalRecords").Value
Personally, I think the second way is more elegant and readable. Both ways work. Good luck!

How to express the Parameters correctly in SQLite Select statement

I am just not sure the correct way to express the parameters in the SQLite Select statement with parameters.
In the Select-statement , how to express?
1) use =? or ='?'
"Is_Offline_Trans =?" or "Is_Offline-Trans='?'"
2) for Number,
Example "0" or "'0'"
3) For String
Example: "SQlite's Rocking solid" or "'SQLite's Rocking Solid"
is this correct?
here my Parameterized Select statement:
string string Company ="Anderson's Studio";
var Orders = db.Query<SalesOrderHeader >("Select * From SalesOrderHeader Where Is_SyncToNAV =?" +
" AND Is_Offline_Trans =?" +
" AND DataEntryComplete =?" +
" AND Company =?","0" ,"1" ,"1", Company);
Can I do hard code for the company this way:
" AND Company =?", "Anderson's Studio"
Your help is greatly appreciated.
Thanks
Inside the SQL statement, a parameter is just a single ?.
With quotes, '?' would be a string containing a single question mark character.
The parameter values are not given in SQL. but come from your program.
Therefore, you must write them correctly for the programming language that you're using.
(C# does not quote numbers.)

Remove ' single quotes in Classic ASP

Field = "columnName"
value = '2,4'
query = ""&Field&" in("&value&")"
Here the query will be columnName in('2,4') but I want it to be columnName in(2,4).
How to remove the single quotes?
If you know that there always is apostrophes around the value, you can simply get the characters between them:
query = Field & " in (" & Mid(value, 2, Len(value) - 2) & ")"
If you know that the vales are numerical (i.e. doesn't need any apostrophes in the SQL because they aren't string literals), you can remove all apostrophes in the value:
query = Field & " in (" & Replace(value, "'", "") & ")"
You might also consider where the apostrophes comes from in the first place. If you add them at some point earlier in the code, the easiest would of course be to not add them (or preserve the original value in a different variable if you need the apostrophes for something else).
This will do.
Field = "columnName"
value = '2,4'
query = ""&Field&" in("& Replace(value, "'", "") &")"

Resources