Dynamic Formula in Crystal Report Formula with winform - vb.net

I using Visual studio.
I have SQL table tbInvoice: amount1, amount2, invNumber.
In winform, i have checkbox1, checkbox2.
If checkbox1 is checked, then in CR should be #total = amount1.
If checkbox1 and checkedbox2 is checked, then in CR should be #total = amount1 + amount2
My formula to print :
frmInv.CrystalReportViewer1.SelectionFormula = "{tbInvoice.invNumber} = '" & FrmInvoice.txtInvNo.Text & "'"
How i accomplish this?
How formula #total in CR can read if checkbox is checked in winform?
I need something like this in CR #total:
if frmInvoice.checkbox1.checked = true and frmInvoice.checkbox2.checked = false then
{tbInvoice.amount1}
elseif frmInvoice.checkbox1.checked = true and frmInvoice.checkbox2.checked = true then
{tbInvoice.amount1} + {tbInvoice.amount2}
endif
Pls Help.
Thank You

Add a parameter to the report.
The formula logic can depend on the parameter.
Your code can set the parameter value.

I didn't understand your last question but here is an example for the {#YourValue} formula:
IF {?Your_Parameter} = "ABC" Then {tbInvoice.amount1} ELSE {tbInvoice.amount1} + {tbInvoice.amount2}
Then, simply SUM that formula at whatever level(s) you need.
Your code needs to set the parameter value.

Related

How do I use the DCount function so it does not count fields with "Null" values in Access?

I have this basic VBA script for conditional formatting. It checks for duplicate values. But this function also retruns True for the empty cells. How do I adjust this formula so it only counts the values that are Not Null?
Is it possible to use the Dlookup function instead to compare if my newly entered Number in me.txt_Number_1 already exists in field [Number_1]?
Private Sub Form_Current()
If Nz(DCount("*", "[qry_DataEntry]", "[Number_1] = '" & Me.txt_Number_1 & "'"), 0) = 1 Then
Me.txt_Duplicate.Visible = False
Else
Me.txt_Duplicate.Visible = True
End If
End Sub
to count values that are not null, use;
DCount("[Number_1]", "[qry_DataEntry]", "[Number_1] <> Null")
to check the latest entered number, use;
DLookup("[Number_1]", "[qry_DataEntry]", "[Number_1] = " & Me.txt_Number_1)
is Number_1 numeric? numeric fields dont need to be surrounded with quotes when setting search criteria per this example.
What if you try an IsNull() into the query? Substitute for a field or use multiple with parenthesis. Something like this:
With Me
If DCount("*", "qry_DataEntry", "Number_1 = '" & .txt_Number_1 & "' AND NOT IsNull(Number_1)") Then
.txt_Duplicate.Visible = False
Else
.txt_Duplicate.Visible = True
End If
End With

Validation Rule in VBA to determine the length of text

I have a form for recording the phone numbers which have two kinds:
Internal has 4 characters
External has 4 or 7 or 8 characters
I want to make the ValidationRule change according to the Combobox that determines the type of phone numbers.
I tried to use ValidationRule in the form properties but it doesn't work with IIf formula or dependening on the value of another textbox or combobox.
So I made this piece of code, but it doesn't work:
If me.combo.value = "internal" then
Me.field.validationrule = "Len([field]) = 4"
ElseIf Me.combo.value = "external" Then
Me.field.validationrule = "Len([field]) = 4 or Len([field]) = 7 or Len([field]) = 8"
End If
Thanks in advance.
You don't have to change the validation rule. Try something like this instead:
([combo]="internal" And Len([field])=4) Or ([combo]="external" And (Len([field])=4 Or Len([field])=7 Len([field])=8))
The code for which I ask
If me.combo.value = "enteral" then Me.field.validationrule ="Is Null OR Like """"" Elseif me.combo.value = "extetnal" then. Me.field.validationRule="Is Null OR Like """" OR Like """" OR Like """""
End if.
The source
https://www.officena.net/ib/topic/87200-التحكم-بخاصية-قاعدة-التحقق-من-الصحة-validationrule-لمربع-نص-برمجياً-vba/

Print Checboxcolumn from Datagridview to Crystal Report

I nee a help. I have a datagridview and have a checkboxcolumn in the last column. Which row is checked, it will be send to crystal report. In the crystal report, I only get the last checked even I check many records. Anyone can help me?
I use Vb.net
For i As Integer = 0 To dgv1.Rows.Count() - 1
Dim cek As Boolean = CBool(row(i).Cells(5).Value)
If cek = True Then
count += 1
Frm_HasilOrder.crv_hasilorder.SelectionFormula = "{tabelpasien.nama}='" + txt_namaOrder.Text + "' AND {all_kodeprmtr.kode} ='" + dgv1.Rows(i).Cells(0).Value + "'" '.ToString()
End If
Next
Frm_HasilOrder.crv_hasilorder.Refresh()
Frm_HasilOrder.Show()

Visual Basic - DropDown text not altered to reflect value

I am working in VB and have an event that is supposed to update a number of values in a DropDown, and update the text accordingly:
For i As Integer = 0 To (prices.Items.Count - 1)
If prices.Items(i).Text.Contains("£") Then
Dim dConvertedValue = getTextAsDouble(prices.Items(i).Value) / dConversionRate
prices.Items(i).Value = dConvertedValue.ToString()
'should update displayable text here, but no change
prices.Items(i).Text = (Math.Floor(dConvertedValue).ToString("N") & "$")
End If
Next
This works fine in theory, and I have stepped-through and can see that the values are changing as expected. However, the Dropdown does not update at any point.
I'm very new to VB, so it could be something as simple as a syntax error. Does anybody know why this might be?
Mark
Try using this I have used your exact code but changed the loop to 'For each'
For each Item as ListItem in prices.items
If Item.Text.Contains("£") Then
Dim dConvertedValue = (getTextAsDouble(Item.Value) / dConversionRate)
Items.Value = dConvertedValue.ToString()
Items.Text = (Math.Floor(dConvertedValue).ToString("N") & " sq m")
End If
Next

How can I calculate the sum of Legacy Form Fields without werid scrolling?

I have a Word document with a table that contains the price of an item, the order amount and the item sum, where amount and item sum are Legacy Form Fields:
There are other items available as options that you can chose via checkbox (also Legacy Form Fields):
Code
Now I've created a function that calculates the item sum
Sub order_amount_Click()
Dim amount As Integer
Dim valueFromCell As String
Dim list
Dim value As Double
amount = ActiveDocument.FormFields("order_amount").Result
If amount > 0 Then
valueFromCell = ThisDocument.Tables(1).Cell(5, 3).Range.Text
list = Split(valueFromCell, " ")
value = list(0)
subsum = value * amount
ActiveDocument.FormFields("order_amount_sum").Result = subsum
Else
ActiveDocument.FormFields("order_amount_sum").Result = 0
End If
CalculateSum
End Sub
and another function that calculates the item sum for additional items (if the checkbox is activated) multiplied by the order amount:
Sub optional_item_Click()
Dim checkbox As Integer
Dim valueFromCell As String
Dim list
Dim value As Double
Dim amount As Integer
Dim subsum As Double
checkbox = ActiveDocument.FormFields("optional_item1").Result
amount = ActiveDocument.FormFields("order_amount").Result
If checkbox = 1 And amount > 0 Then
valueFromCell = ThisDocument.Tables(1).Cell(7, 3).Range.Text
list = Split(valueFromCell, " ")
value = list(0)
subsum = value * amount
ActiveDocument.FormFields("optional_item1_sum").Result = subsum
Else
ActiveDocument.FormFields("optional_item1_sum").Result = 0
End If
CalculateSum
End Sub
(There are as many Click() functions as there are optional_item fields, 17 in sum - I haven't generalized the function yet.)
The last line within both Subs is a function call, that calculates the net sum, the VAT and the final total sum
Function CalculateSum()
Dim net As Double
Dim vat As Double
Dim total_sum As Double
net = CDbl(ActiveDocument.FormFields("order_amount_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item1_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item2_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item3_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item4_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item5_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item6_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item7_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item8_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item9_sum").Result)
' Cannot compile when line too long, so splitting into two statements
net = net + CDbl(ActiveDocument.FormFields("optional_item10_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item11_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item12_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item13_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item14_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item15_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item16_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item17_sum").Result)
'MsgBox "net " & net
ActiveDocument.FormFields("net_sum").Result = net
vat = net * 0.19
'MsgBox "vat " & vat
ActiveDocument.FormFields("vat_sum").Result = vat
total_sum = net + vat
'MsgBox "total " & total_sum
ActiveDocument.FormFields("total_sum").Result = total_sum
End Function
Problem
The code itself works fine, all elements are calculated correctly. But there are two major problems that make the whole document almost unusable (all actions for users are restricted to enter their name on top of the document, chose the amount and de-/activate the checkboxes; sum fields aren't accessible):
When I perform an action (e.g. enter an amount) the CalculateSum() function visibly loops over all sum fields (spanned over three pages) and weirdly scrolls on the document along the sum fields.
When I click on a checkbox on the 2nd or 3rd page, the document scrolls up to the first page to the place where I can enter the amount of pieces I want to order.
Now how can I supress all that looping over the sum fields and weirdly scrolling around? And how can I prevent the document from scrolling up to the 1st page?
Any help appreciated! (I'm also thankful for side comments on my code, as I'm new to VBA.)
Update #1
I've added a screencast showing the error.
Update #2
The basic problem seems to be that the macro is scrolling to the position I am referring to in my script, e.g. if I'm using
ActiveDocument.FormFields("total_sum").Result = total_sum
it's scrolling to the total_sum field.
Too difficult for a comment...
For (b) you have to use VBA - you can't use the trick I suggested elsewhere with the FormFields checkboxes, only with the Content Control ones.
For (c), something like this (not tested)
Make sure the On Exit macro is set for optional_item1 and the Calculate on Exit is also set. I don't think you need an On Entry macro as well.
Sub optional_item1_Click()
Call cb_Click("optional_item1")
End Sub
Sub cb_Click(ffname As String)
ActiveDocument.Variables(ffname) = abs(int(ActiveDocument.FormFields(ffname).Checkbox.Value))
End Sub
In your Summa column, next to the checkbox, something like
{ ={ DOCVARAIABLE optional_item1 }*X2 }
where X2 is the cell that contains the 3,00 Euro value and all the { } are the special field code brace pairs that you can insert using ctrl-F9 on Windows Word.
BUT
Whether Word will correctly interpret your Euro value correctly depends on the regional settings of the user using your form, (decimal separator, group (thousands) separator, currency symbol and location of the currency symbol may all change Word's behaviour. Even within the Eurozone I think some of those things can vary. So if the user does not need to modify the multiplier 3,00 Euros in the cell, it may be better to plug the value directly into the { = } field:
{ ={ DOCVARAIABLE optional_item1 }*3 }
You can also use \# numeric formats to give you the format you want when positive or 0. But again, Word's numeric formatting facility is not locale-independent).
Those locale dependencies are also sometimes a reason for doing more in VBA rather than field codes.
As AdamsTips found out elsewhere, replacing this
ActiveDocument.FormFields("myField").Result
by this
ActiveDocument.Bookmarks("myField").Range.Fields(1).Result
allows accessing the field without focusing it.