Inline null check for string in vba - string

iam searching a way to check if a string is empty before converting it to date, problem i that i want to avoid if-else since i have to use it on multiple variable in single statement:
Call MyObj.Insert(CInt(Me.ddlFiltroPD), _
CInt(Me.Parent!ddlFiltroEsito), _
CInt(Nz(ddlFiltroPD.Column(1))), _
Nz(txtPuntoDebolezza, ""), _
CInt(Nz(Me.ddlRilevanza, 0)), _
Nz(txtInterventi, ""), _
Nz(txtOwner, ""), _
CDate(txtData1), _
CDate(txtData2), _
CDate(txtData3), _
CDate(txtData4), _
Nz(txtMotivazioneChiusura) _
)
txtData1, txtData2, txtData3 and txtData4 can be null and i have to convert it to a data when they are not null
i have thought to create a function to do the convertion or to use some temp variables but iam looking for something better

You can use this expression:
IIf(txtdata1 = vbNullString, "", CDate(txtdata1))

i solved in this way
Call MyObj.Insert(..., _
txtData1, _
txtData2, _
txtData3, _
txtData4, _
)
in MyObj.Insert i change parameter to variant and i transform parameter using Format(txtData1, "YYYYMMDD")

Related

No value given for one or more required parameter error for VBA in Excel

I have a SQL data table which contains below data.
In this I need to get the "Component" data which are structured under "Item" and after that each component will be considered as "Item" and recursive CTE comes here. I have executed the below code for this, but I am facing with error. Please help me to sort this out.
revQueryString = "with ReqCTE as (" & _
"SELECT Item, Component, qtyper, 0 as lvl FROM STRUCT " & _
"where Item = ? " & _
"Union all " & _
"select Folders.Item, Folders.Component, Folders.qtyper, Child.lvl +1 from ReqCTE as Child " & _
"join STRUCT as Folders on Folders.Item = Child.Item); " & _
"select * from ReqCTE;"
Also I have attached the input and required Output for this task.
Input:
Required Output:

Dynamically changing vlookup table

I recorded this:
Range("B1").FormulaR1C1 = _
"=IF(VLOOKUP(RC1,sorted!R3C1:R35C33,8,FALSE)="""","""",VLOOKUP(RC1,sorted!R3C1:R35C33,8,FALSE))"
I need to have it update dynamically.
I found this (modified for my needs), but I'm not sure what to put in the "?" fields. Hopefully I'm on the right track:
With Worksheets("Move to New")
.Range("?").Offset(x, 0) = Application.WorksheetFunction.VLookup( _
.Range("?").Offset(x, 0), _
Worksheets("sorted").Range("?", .Range("?").End(xlDown)), 8, False)
I got it resolved.
Range("B1").FormulaR1C1 = "=IF(VLOOKUP(RC1,sorted!C1:C33,8,FALSE)="""","""",VLOOKUP(RC1,sorted!C1:C33,8,FALSE))"

Export to excel from Object

I'm trying to export a query to excel, i got the query and then i save the result in:
Dim _Result As Object = Nothing
_Result is declare as a Object.
In the next code the query is saved into _Result
_Result = _Query.GetRecords(_pOP, _
_pName, _
_pLastName, _
_pAdress, _
_pEFSL, _
)
In this point everything runs fine, but i would like to know how to covert that result in a XLS file.
nQuerys is a Class that have the procedures to retrieve information from the DB., the function GetRecords (function into nQuerys) retuns the info in a List and then AJAX_Directory
receives the information after the info is stored in _Result, thanks for your answer.
Here is my code complete. Thanks you for your help.
<WebMethod>
Public Shared Function AJAX_Directory(_pOP, _
_pName, _
_pLastName, _
_pAdress, _
_pEFSL, _
) As Object
Dim _Query As New nQuerys <--------- This is a class where I have all my procedures that retrieve information from the database
Dim _Result As Object = Nothing
Try
_Result = _Query.GetRecords(_pOP, _
_pName, _
_pLastName, _
_pAdress, _
_pEFSL, _
)
Return _Result
Catch ex As Exception
Return ex.Message
Finally
_Query = Nothing
End Try
End Function

'DoCmd.TransferSpreadsheet' to export to Excel

I need to EXPORT an access table from ACCESS to EXCEL. There are 618499 rows. I tried simple method: right-click - export - excel. It worked only when I choose export with format (without format created blank excel document), but it is not convenient because I'll have to create 10 documents. I tried to use this code but it became red and did not work:
DoCmd.TransferSpreadsheet(
[acExport],
[acSpreadsheetTypeExcel12Xml],
[tab_1],
["C:\Users\admin\Desktop\import_2014\final.xlsx"],
[True])
What should I change in it?
How should I type it in a module.
Or maybe you can offer another simple solution.
All VBA code must be in a sub or function, that is in a module.
Get rid of all those [], and if you want to put each parameter into a new line, use the line concatenation symbol _
DoCmd.TransferSpreadsheet _
acExport, _
acSpreadsheetTypeExcel12Xml, _
"tab_1", _
"C:\Users\admin\Desktop\import_2014\final.xlsx", _
True
EDIT
Either use Call:
Call DoCmd.TransferSpreadsheet(acExport, acSpreadsheetTypeExcel12Xml, _
"tab_1", "C:\Users\admin\Desktop\import_2014\final.xlsx", True)
or omit the parentheses:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
"tab_1", "C:\Users\admin\Desktop\import_2014\final.xlsx", True

Multiline String In Classic Asp

is there any possiblity to get multiline string in classic asp (I think vbscript is the language)?
I want a multiline string like in python or groovy:
def str = """hello
I am a
multiline
string"""
I searched a lot but didn't find a solution.
Workarounds are welcome too.
BTW: I had in javascript the same problem and solved it back in time with a function saved in a variable. This function had a multiline comment in it so I could through everything away except the comment using regex.
Something like this:
var multilinestr = function() {
/*
hello
I am a multiline
string
*/
}
And after Regex I got a String which contains:
hello
I am a multiline
string
Thank you.
Edit:
I think I missed a very important point.
My client is you using something like a "pre processor" for his scripts.
It looks like this:
Dim str
str = "<%std_text%>"
The "pre processor" exchanges "<%std_text%>" with a text which comes from a Database.
But this text have breaks in it so I can't just put a '" & vbNewline ' to the end of line.
This means after "pre processing" it looks like this:
Dim str
str = "hello
I am a multiline
string"
Is there anyway to get this "text" in a string?
If I could write something like this (groovy):
def multistr = """<%std_text%>"""
after "pre processing":
def multistr = """hello
I am a multiline
string"""
It would be great!
python:
text = """"
hello world
this is some text
"""
vbscript:
text = "" & vbcrlf &_
"hello world" & vbcrlf &_
"this is some text" & vbcrlf
You can also write a custom stringbuffer class etc.
with new StringBuffer
.writeline "hello world"
.writeline "this is some text"
result = .as_string
end with
Just KISS... I mean a 'preprocessor' for a scripting language? That doesn't sound good....
If you really need to use the preprocessor (i18n?) than you will need to modify it such to replace all line breaks by " & vbcrlf & ".
Could you not add carriage returns to the string?
Dim myString : myString = "Line 1" & vbCrLf & "Line 2"... etc
Unfortunately you will not be able to use this as-is as far as I can tell. You would need to modify the pre-processor to replace the vbNewLine's with an actual vbNewLine variable rather then true line breaks. There is no way in VB Script to concatenate a string on multiple lines without using & _ which requires you to close off the string before doing so on each line, which doesn't seem possible with this setup.
Do you want multi-line as in the plain text sent to the browser is multi-line? Or do you want the rendered text to be multi-line?
If its the former, a solution like Mick's works. You can use either vbCrLf or vbNewLine to create a new line in the text sent to the browser. IIRC the latter is preferred as it provides either the carriage return or the carriage return/line feed as appropriate for the client.
Dim myString : myString = "hello" & vbNewLine & "I am a multi-line" & vbNewLine & "string"
If its the latter, you just need to put a <br /> where you want the browser to create a new line. In this case, you may want to think about why you want it to display the way you want it to display as there may (or may not) be a better way of doing it.
There's only two ways that I know of and I've been doing this for 10+ years.
Dim x: x = "Hello" & vbNewline & "World"
or
Dim x: x = "Hello" & vbNewline & _
"World"
I guess there's the hack way, too:
Dim x: x = Replace("Hello\nWorld", "\n", vbNewline)
The closest I could get was:
Dim multilineStr : multilineStr = Join(Array(_
"Begin" , _
" my" , _
" multiline", _
" string!" , _
"End" _
), vbNewLine)
Dim multilineStr : multilineStr = Join(Array(_
"Begin" , _
" my" , _
" multiline", _
" string!" , _
"End" _
), "<br/>")

Resources