why i cannot use :
rep8_bc = IIf(IsNull(rs(8)), "null", Replace(rs(8), "'", "''"))
it say " Invalid use of Null"
but if i remove replace, it's work. And then get error because record have an apostrophe character, so i change the code into this :
rep8_bc = "null"
If IsNull(rs(8)) = False Then rep8_bc = Replace(rs(8), "'", "''")
or this :
If IsNull(rs(8)) = False Then
rep8_bc = Replace(rs(8), "'", "''")
else
rep8_bc = "null"
end if
Mostly likely compiler doesn't short circuit within IIF() statement. Compiler takes it as a whole statment (both values) before returning one. That's where you get the error. So breaking into pieces of proper conditional statemetns would be the key here. So you have any achieved that with your answer.
To further add, IIF() is much slower in execution than the IF-ELSE statments.
Related
I have an application that generates some reports. I use some filters in data that is retrieved.
wf_gen_filtro_conceptos()
//PROBLEM---------------
dw_rp1.setfilter(string(is_filtro) + " " + "or IsNull(causa)")
//----------------------
dw_rp1.filter()
dw_rp1.sort()
dw_rp1.groupcalc()
In wf_gen_filtro_conceptos() the variable is_filtro is set up.
The problem is when I use setfilter(). I can't concatenate those strings to create the new filter. If I use one of those strings, for example, IsNull(causa) on its own, there's NO problem.
Add a line to concatenate the string before you use the SetFilter method.
Something like
ls_filterstring = is_filtro + " " + "or IsNull(causa)"
dw_rp1.setfilter(ls_filterstring)
This way you can examine what is happening with your filterstring in the debugger. If is_filtro is NULL then the entire string will be NULL.
If is_filtro is being set make sure the quote marks, if any, in the string are correct.
I suspect that is_filtro has an invalid column, or an invalid value, or is_filtro is null, or is_filtro is empty.
Initialize is_filtro to something meaningful.
This works if you want all values.
is_filtro = "(1 = 1)"
If is_filtro is empty ("")
dw_rp1.setfilter(string(is_filtro) + " " + "or IsNull(causa)")
setfilter would be effectively
dw_rp1.setfilter(" or IsNull(causa)")
If is_filtro is null
setfilter would be effectively
dw_rp1.setfilter()
Your filter needs to match the column datatype.
Suppose that causa is a string column and you intend to filter values A, B, and C.
Wrong
is_filtro = "causa in (A, B, C)"
Right
is_filtro = "causa in ('A', 'B', 'C')"
If you are combining different filter clauses you need an and between them.
Thanks everyone for answering my question.
I've tried the stuff you suggested but it's still throwing the same error..
Here is the function that set the filter up.
choose case is_filtro_ca
case ''
messagebox("Filtro", "Is filter 1")
is_filtro = ""
case '*'
messagebox("Filtro", "Is filter 2")
is_filtro = ""
case else
messagebox("Filtro", "Is filter 3")
is_filtro = "causa in (" + is_filtro_ca + ")"
End choose
This function is called when a button "Show empty lines" is pressed. I put those messages an it always prints "Is filter 2".
I have a return value from the database but I can not filter the null value.
With rs_receita
Do Until .EOF
Set noaux2 = xml.createElement("Superavit")
noaux2.Text = IIf(IsEmpty(!Superavit), "", CStr(!Superavit))
Call noaux.appendChild(noaux2)
.MoveNext
Loop
End With
Set rs_receita = Nothing
Avoid IIf for this scenario. IIf always evaluates both expressions. So when !Superavit is null, this will cause an error.
A single-line If statement, on the other hand, will only evaluate the expression to be executed. Combine that with the IsNull() function to reliably assign a database value to a variable if it's not null:
If IsNull(!Superavit) Then noaux2.Text = "" Else noaux2.Text = CStr(!Superavit)
In VB6 (I'm sorry!) you can force coercion to a string by appending an empty string to the field value.
Edit: Phew, this article is a blast from the past. It gives a bunch of NULL handling ideas for classic VB6: https://technet.microsoft.com/en-us/library/Aa175775(v=SQL.80).aspx
I believe either of the following will work:
noaux2.Text = "" & rs("Superavit")
OR
noaux2.Text = vbNullString & rs("Superavit")
I'm connecting with LDAP to Active Directory for a corporate phonebook. I'm grabbing the data I want, but I need to do an If...Else statement on some of the data I'm grabbing.
I want to check if the value in AD attribute 'homePhone' begins with "01". If it does, I want to write out its value. If it begins with anything else, I want to either write "" or "Not Valid".
Here's what I've been writing, but isn't working:
Response.Write "<td>"
if objRS("homePhone") = "01*" then
Response.Write objRS("homePhone")
else
Response.Write ""
end if
Response.Write "</td>"
This seems to just go to the Else condition, and the homePhone attribute doesn't get written.
You can't use wildcards in string comparisons and, unfortunately, VBScript doesn't support the Like operator used in VBA/VB. You can use a regular expression but that's overkill for what you need here. Just strip the first two characters and perform your comparison.
If Left(objRS("homePhone"), 2) = "01" Then
If you need to perform case-insensitive string comparisons (not necessary in this situation, but may be helpful in the future), you can convert both strings to upper/lowercase before comparing or use the StrComp() function with the vbTextCompare parameter value.
If StrComp(Left(objRS("homePhone"), 2), "01", vbTextCompare) = 0 Then
I am trying to make a program which uses multiple OR operator in If statement..
If aryTextFile(i) = "and" Or "but" Or "or" Or "nor" Or "for" Or "yet " Or "so" Then
TextBox2.Text = aryTextFile(i) & " is a Coordinating Conjunctions"
But this gives out the error
Conversion from string "but" to type 'Boolean' is not valid.
I need help to make multiple or operator to work or even if i could replace if statement :P
Any help appreciated.
P.S - Beginner in programming !
use OrElse instead of Or (to not evaluate every instance, if the first is a match, it wont evaluate the rest of the expressions as it is not necessary)
And you have to do it like this:
If aryTextFile(i) = "and" OrElse aryTextFile(i) = "but" OrElse aryTextFile(i) = "or" Then
The string by itself is not a boolean expression
It expects "boolean expresson" OrElse "another boolean expression"
I have 6400+ records which I am looping through. For each of these: I check that the address is valid by testing it against something similar to what the Post Office uses (find address). I need to double check that the postcode I have pulled back matches.
The only problem is that the postcode may have been inputted in a number of different formats for example:
OP6 6YH
OP66YH
OP6 6YH.
If Replace(strPostcode," ","") = Replace(xmlAddress.selectSingleNode("//postcode").text," ","") Then
I want to remove all spaces from the string. If I do the Replace above, it removes the space for the first example but leave one for the third.
I know that I can remove these using a loop statement, but believe this will make the script run really slow as it will have to loop through 6400+ records to remove the spaces.
Is there another way?
I didn't realise you had to add -1 to remove all spaces
Replace(strPostcode," ","",1,-1)
Personally I've just done a loop like this:
Dim sLast
Do
sLast = strPostcode
strPostcode = Replace(strPostcode, " ", "")
If sLast = strPostcode Then Exit Do
Loop
However you may want to use a regular expression replace instead:
Dim re : Set re = New RegExp
re.Global = True
re.Pattern = " +" ' Match one or more spaces
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("O P 6 6 Y H.", "")
Set re = Nothing
The output of the latter is:
D:\Development>cscript replace.vbs
OP66YH.
OP66YH.
OP66YH.
D:\Development>
This is the syntax Replace(expression, find, replacewith[, start[, count[, compare]]])
it will default to -1 for count and 1 for start. May be some dll is corrupt changing the defaults of Replace function.
String.Join("", YourString.Split({" "}, StringSplitOptions.RemoveEmptyEntries))
Because you get all strings without spaces and you join them with separator "".