Set values in formula fields in crystal report from VB - vb.net

I'm new in vb and crystal report so please help me from my problem. I'm assigning values using vb codes to a formula field from my crystal report. Here is my code:
Dim report As CrystalDecisions.CrystalReports.Engine.ReportDocument
report = New report_Student()
report.DataDefinition.FormulaFields("student").Text = "Slone" & Chr(13) & "Thompson"
frm_print.viewerReport.ReportSource = report
frm_print.viewerReport.RefreshReport()
frm_print.Show()
i place this in button click event. Now i have an error on running this, before it loads the crystal report from viewer this error shows:
The remaining text does not appear to be part of the formula.
Details: errorKind
Error in formula student:
'Slone
'

I guess as you have formula field (not text object) you should use formula there:
report.DataDefinition.FormulaFields("student").Text = _
ControlChars.Quote + "Slone" + ChrW(13) + "Thompson" + ControlChars.Quote
So, in result formula will contain one text literal ("Slone\nThompson").
Not tried this, but hope it should work.
If not, then probably you need to use CR inner function ChrW() for newline in next way:
report.DataDefinition.FormulaFields("student").Text = _
ControlChars.Quote + "Slone" + ControlChars.Quote + _
" + ChrW(13) + " + _
ControlChars.Quote + "Thompson" + ControlChars.Quote
So, in result two text literals ("Slone" and "Thompson") will be concatenated with result of CR inner function ChrW().
In CR formula editor it will be shown as
"Slone" + ChrW(13) + "Thompson"
But I expect 1st way should work.

You should use a report parameter instead of a formula. In case you need to modify the values inside the report use the parameter inside the formula

Related

How do I perform a total calculation using price in a combo box?

I am attempting to make a project on visual studio.
I have the following data in a combo box and I was wondering how I would be able to multiply the price (the CStr value) by the number of days the user selects, showing the total in another text box using a calculate button
{cmbPedigreeDog.Items.Add("African Hairless" & CStr(1.14))
cmbPedigreeDog.Items.Add("Boxer" & CStr(0.86))
cmbPedigreeDog.Items.Add("Chihuahua" & CStr(1.83))
cmbPedigreeDog.Items.Add("Dalmation" & CStr(0.65))
cmbPedigreeDog.Items.Add("Eskimo Dog" & CStr(1.14))
cmbPedigreeDog.Items.Add("Farm Collie" & CStr(0.95))
cmbPedigreeDog.Items.Add("GreyHound" & CStr(1.99))
cmbPedigreeDog.Items.Add("Husky" & CStr(1.85))
cmbPedigreeDog.Items.Add("Irish Setter" & CStr(0.65))
cmbPedigreeDog.Items.Add("Jack Russell Terrier" & CStr(1.77))
cmbPedigreeDog.Items.Add("King Charles Spaniel" & CStr(1.02))
cmbPedigreeDog.Items.Add("Labrador Retreiver" & CStr(1.74))
cmbPedigreeDog.Items.Add("Maltese" & CStr(1.47))
cmbPedigreeDog.Items.Add("Pug" & CStr(1.31))
cmbPedigreeDog.Items.Add("Rottweiler" & CStr(2.17))
cmbPedigreeDog.Items.Add("St Bernard" & CStr(1.63))
cmbPedigreeDog.Items.Add("Tibetan Mastiff" & CStr(1.15))
cmbPedigreeDog.Items.Add("Working Sheep Dog" & CStr(0.75))
cmbPedigreeDog.Items.Add("Yorkshire Terrier" & CStr(0.88))
cmbPedigreeDog.Items.Add("Other" & CStr(1.22))}
Not the best way, it requires that all your strings in combobox are of the same pattern with same quantity of delimiters.
Example:
If you use instead of "Boxer" & CStr(0.86) something like "Boxer|" & CStr(0.86) (added just a | symbol), then you can split this string back like this.
line = cmbPedigreeDog.Value
price = CDbl(Split(line, "|")(1))
And some explanations:
Each line of text is a container of symbols. Each can be splitted to array with some delimiter. So the line price = CDbl(Split(line, "|")(1)) can be also written like this:
Dim someArray()
line = "Boxer|" & CStr(0.86)
someArray = Split(line, "|")
' after splitting you have two items in array one is "Boxer" and another is "0.86" (which is text)
' if you follow the pattern in each of your combobox options the price always will be the second one
' so you may refer to second item in array, which is number 1
Price = CDbl(someArray(1))
UPDATE
Another way, as per request in comment. Use two columns combobox. To fill your combobox you have to do following
1 - Go to you ComboBox's properties and set the "ColumnCount" values to 2 (see here how to do it).
2 - This is up to you - you may use the binding with prices on a worksheet as like in topic I gave a link in p.1 or change each line on your code to this pattern:
With cmbPedigreeDog
.AddItem ("Boxer")
.column(1, cbx.ListCount - 1) = 0.86
.AddItem ("Chihuahua")
.column(1, cbx.ListCount - 1) = 1.83
' and so on
End With
3 - To retrieve the price of selected item use such code
If cmbPedigreeDog.ListIndex >= 0 Then
price = cmbPedigreeDog.List(cbx.ListIndex, 1)
Else
MsgBox "item is not chosen"
End If
UPDATE 1
This answer is for VBA, not for Visual Basic WinForm application. For this try to look for a solution like "Multi Column ComboBox"

Dlookup reference where name of column being searched is the name of the textbox on my form

I am editing a database created by my predecessor at work. I am creating a "helper" textbox that will pull a value from a table in the same database.
Problem is, in my Dlookup, the name of the column that I am searching is also the name of the textbox on my form that contains the criteria. To change the name of my textbox, I would have to update a lot of code that I did not create. Is there a way around this?
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# = GreigeRoll#")
I expect the output to be the "GreigeWeightAvg" value from the table.
The output is:
"Syntax error (missing operator) in query expression 'GreigeRoll# = GreigeRoll#'."
Try to concatenate the value:
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "[GreigeRoll#] = " & Me![GreigeRoll#].Value & "")
or, it text:
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "[GreigeRoll#] = '" & Me![GreigeRoll#].Value & "'")
Include the fully-qualified control name in your selection criteria, e.g.:
txtgreigeweight = Application.DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# = [Forms]![YourFormName]![GreigeRoll#]")
Change YourFormName as appropriate.
At first stop using special characters in field Names...some might think that it improves readability..BUT it won't...its almost a recipe for issues.
So ... just clarify what kind of value is GreigeRoll#
If its numeric (like 1,21,21321) then you should have :
txtgreigeweight = DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# =" & [GreigeRoll#])
On the other hand is alphanumeric (like "A12", "BigGreige","1stG") then it should be :
txtgreigeweight = DLookup("[GreigeWeightAvg]", "dbo_TuftingGreigeData", "GreigeRoll# ='" & [GreigeRoll#] & "'")

Run time error in spreadsheet

thank you for your time. I've put stars on the error line if this helps.
I'm lost with an error and I can't find the reason. Can I get some help, please? The macros is not running for only one period and I didn't find any different data in the database compare to the previous period
That's the debug menu:
Get values and convert to string for text box:
a = Format(.Cells(rowNum, dateCol), "dd mmm")
a = a + ", " + Str(.Cells(rowNum, actualAssetCol))
Get values and convert to string for text box:
a = Format(.Cells(rowNum, dateCol), "dd mmm")
a = a + ", " + Str(.Cells(rowNum, actualAssetCol))`
The string concatenation operator in vba is & and not +. Thus, use it like this:
a = a & ", " & Str(.Cells(rowNum, actualAssetCol))`

How to write raw string in Excel VBA?

I need to write a formula in an Excel sheet via VBA, so I go through select range and apply formula, but my formula is too long and it contain lot of double quotes (") so to ignore double quotes (") I am adding two double quotes (")
Some time string write as per my desire or some time by mismatching double quotes (") string get changed and formula applied is not correct.
As in python we write r before string and it work as follows:
print(r'hello\'s Sam.')
hello\'s Sam
but is there any way in Excel VBA to write such a raw string?
Formula is as below
=IF(NOT($E24=""),IF($Q24="0-10V(AI)","Direct (0-10V) = (0-100%)",IF($Q24="2-10V(AI)","Direct (2-10V) = (0-100%)",IF(OR($Q24="PT 1000",$Q24="NTC 20K"),"-50 to 150 Deg C","N/A"))),"")
And I apply it through VBA as follow
Sheet4.Range("R2:R50000").Formula = "=IF(NOT($E2=""" + """),IF($Q2=""" + "0-10V(AI)""" + ",""" + "Direct (0-10V) = (0-100%)""" + ",IF($Q2=""" + "2-10V(AI)""" + ",""" + "Direct (2-10V) = (0-100%)""" + ",IF(OR($Q2=""" + "PT 1000""" + ",$Q2=""" + "NTC 20K""" + "),""" + "-50 to 150 Deg C""" + ",""" + "N/A""" + "))),""" + """)"
There is no notion of raw string in VBA, but you could write the formula using e.g. single quote marks rather than double quote marks and then replace them. You could even make a simple utility function to do so:
Function r(s As String, Optional QuoteSymbol As String = "'") As String
r = Replace(s, QuoteSymbol, """")
End Function
Then your formula could be inserted simply as:
Sheet4.Range("R2:R50000").Formula = r("=IF(NOT($E2=''),IF($Q2='0-10V(AI)','Direct (0-10V) = (0-100%)',IF($Q2='2-10V(AI)','Direct (2-10V) = (0-100%)',IF(OR($Q2='PT 1000',$Q2='NTC 20K'),'-50 to 150 Deg C','N/A'))),'')")
In the off-hand chance that you need to have single quote marks in the final formula then you could pass something like the back-tick ( ` ) to the optional parameter QuoteSymbol
Having said all that, you seem to be doing more work than needed in the sense that inside a string any two consecutive double quotes are replaced by just one double quote. You don't need all of that concatenation to build up the final string.
Just add one quote(") as following example:
Formula:
=text(now(),"mmm dd yyyy")
VBA:
Sub InsertTodaysDate()
' This macro will put today's date in cell A1 on Sheet1
Sheets("Sheet1").Select
Range("A1").Select
Selection.Formula = "=text(now(),""mmm dd yyyy"")"
Selection.Columns.AutoFit
End Sub
As There is no notion of raw string in VBA, so this problem can solve with putting saw string in excel Cell and then use it in formula like
Sheet4.Range("R2:R50000").Formula = "=" & Cstr(Sheet10.Cells(1,2).Value)
next time only changing value in cell and formula get update

SSRS - Line break in Matrix cell

In a SSRS Matrix cell I want to be able to have a line break between each output given.
I have the following code in my MS SQL Server Stored Procedure which I then point my SSRS report to
SELECT Customer, Hostname, (QName + QHostname + Qtag + QSerial + QCategory + QType + QItem + QManu + QModel + QVersion) AS AdditionalInfo1
FROM TableQ
At the moment in the AdditionalInfo1 cell when one of the options is returned they are separated by a comma
e.g.
QName, QHostname, Qtag.
Instead I would like them to be separated by a line break all within the same cell
e.g.
QName
QHostname
Qtag
I have tried putting + char(13) + between each Q... in AdditionalInfo1 but this didn't work.
For SSRS, you want to use Chr(10), not Chr(13). I've used this in expressions and as a Join delimiter argument and it produced the desired effect: line breaks within the textbox.
Edit:
Below is an expression that will include the fields with line breaks if a value is present, or omit both if the field is null.
=Fields!QName.Value
+ IIF(ISNOTHING(Fields!QHostname.Value),"", vbCrLf + Fields!QHostname.Value)
+ IIF(ISNOTHING(Fields!Qtag.Value),"", vbCrLf + Fields!Qtag.Value)
+ IIF(ISNOTHING(Fields!QSerial.Value),"", vbCrLf + Fields!QSerial.Value)
+ IIF(ISNOTHING(Fields!QCategory.Value),"", vbCrLf + Fields!QCategory.Value)
+ IIF(ISNOTHING(Fields!QType.Value),"", vbCrLf + Fields!QType.Value)
+ IIF(ISNOTHING(Fields!QItem.Value),"", vbCrLf + Fields!QItem.Value)
+ IIF(ISNOTHING(Fields!QManu.Value),"", vbCrLf + Fields!QManu.Value)
+ IIF(ISNOTHING(Fields!QModel.Value),"", vbCrLf + Fields!QModel.Value)
+ IIF(ISNOTHING(Fields!QVersion.Value),"", vbCrLf + Fields!QVersion.Value)
Instead of concatenating all the 'Q' columns into a single string keep them as separate fields. Then create individual placeholders in a single cell, one for each field. You can do this quite quickly by clicking into the cell (which is rich TextBox) and typing the field name (as it appears in the DataSet) enclosed by square brackets
eg:
[QName]
[QHostname]
[Qtag]