Handling ⅝ (five eights) Vulgar fraction as part of a string in VBA - vba

As part of an access application, i retrieve data from sql server. I have a string field that contains ⅝ in one of the rows, and when i attempt to insert that value into an MsAccess recordset, i get an error
Multiple-step operation generated errors. Check each status value. Here is my code
sFieldValue = getValue() ' when i add a watch, the '⅝' is replaced by a ? e.g. "The result is ⅝" will be shown as "The result is ?"
Rs(sFieldName) = sFieldValue ' error is thrown
I then attempted to hard code the value in VBA
sFieldValue ="⅝"
And the moment i type '⅝' it automatically changes to a question mark
sFieldValue ="?"
I would like to know how i can support characters such as "⅝". The other fractions work just fine, e.g. '½'. I do not want to do any calculations, the fractions are part of a string that comes from a SQL Server, and the problem is, i get a runtime error when i try to add a string value that contains a fraction to a recordset in access.
from this page, it shows that some fractions are supported in utf-8

The problem was that the field type of Rs(sFieldName) was adVarChar. So to fix the problem, i used a adVariant field type in my recordset, and it now accepts the ⅝ vulgar character.
I am creating the recordset on the fly.
Edit: adVarWChar is more appropriate, see comment below

Related

VB.NET IIF Statement

In VB.NET I am trying to use SQL Parameter to send data to SQL Server Database, I am trying to convert string to decimal with following code if the value of the datagridrow is nothing then send 0 or use conversion to decimal argument.
SQL.Addparam("#amt", IIf(IsNothing(DataGridView2.Rows(index).Cells(7).Value), 0, CDec(DataGridView2.Rows(index).Cells(7).Value)))
(SQL and Addparam is called from class I created)
But with this code I get error "System.InvalidCastException: 'Conversion from string "" to type 'Decimal' is not valid.'"
I need help on this please.
Thank you
The specific issue is clearly that your "empty" cell does not actually contain a null reference, i.e. Nothing, but rather an empty String. You would need to test for an empty String as well as or instead of Nothing. If your cell might contain either Nothing, an empty String or a Decimal value, this should work:
Dim cellValue = DataGridView2.Rows(index).Cells(7).Value
SQL.Addparam("#amt",
If(String.IsNullOrEmpty(CStr(cellValue)),
Decimnal.Zero,
CDec(cellValue)))
Note the use of If rather than IIf and an actual Decimal value for the zero. String.IsNullOrEmpty will detect Nothing or an empty String in a single call.
That's really not the best solution though.
It appears that you are using an unbound grid and then looping through the rows and saving each row individually. That is really the wrong way to go. What you should be doing is creating a DataTable with the appropriate schema, populating it from the database if required, binding it to the grid, performing the required edits and then just saving the whole DataTable in one go with a single call to Update on a data adapter.
If you do that then an "empty" cell will contain DBNull.Value and ADO.NET will automatically save NULL to your database. If you don't want NULL values in a column then just set the AllowDBNull property to False and the DefaultValue property to whatever you want instead.
To create the DataTable schema, you can call Fill on a data adapter to do that and populate with data, or you can call FillSchema to not retrieve any data. Alternatively, you can just build the schema yourself.

Issue summing doubles in DataTable with VB?

I am having an issue summing a column of dollar amounts in a DataTable with VB. I have tried two different ways resulting in two different errors which I am not sure how to resolve. Before I go into the two ways I have tried to solve the problem here is the setup:
I am importing a tab delimited file into a DataTable. The headers are automatically populated with data from the first row in the file.
The DataGridView that displays the Datatable is called DGV_detail.
The column in DGV_detail I am trying to sum occurs at column 3 and is called 'Value-to-date'. This column is full of dollar amount values similar to: $10.00 With the dollar sign and everything.
I am also declaring a view variables and doing some calculations when a button is clicked.
Here is my first approach:
For i As Integer = 0 To DGV_detail.Rows.Count() - 1 Step +1
interestPaidToAccounts = interestPaidToAccounts + DGV_detail.Rows(i).Cells(3).Value
Next
When I try this approach I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Conversion from string "" to type 'Double' is not valid.
The other methods I was trying to use to solve the problem were (For reference: In these examples I created a variable local to the button_click function that assigned the BGV_detail.DataSource to equal dataTable1):
Dim sumObject = dataTable1.Compute("Sum(Convert(Value-to-date, 'System.Decimal'))", "[Value-to-date] IS NOT NULL")
and
Dim sumObject As Decimal = dataTable1.Compute("Sum(Value-to-date)", "")
Which both produced the following error:
Expecting a single column argument with possible 'Child' qualifier.
I am pretty lost at this point so whatever the community thinks the best method to resolves these problems is, I am open to.
I can provide additional information if needed! :)
Thanks in advance for your help!
The dollar sign is the issue, use mid to remove it and val to convert to number:
val(mid(DGV_detail.Rows(i).Cells(3).Value, 2,DGV_detail.Rows(i).Cells(3).Value.length -1))
Untested, but should get you started ... there are any number of ways to remove the $.

MS Access 2016 vba: Empty value passed from form field (text box)

I'm having a strange situation:
I have a simple form with 2 text boxes.
The second one has the vba code triggered after being updated.
For example: typing value to the first field, ENTER, typing value into the second field, ENTER and the code starts.
The code, initially, takes the values from the text boxes and assign them to the string variables (pre declared as strings), like:
test1 = Me.frmSSN.Value
The problem is, the test1 variable seems to be empty after the line above.
It seems to happen only when I type, for example this string:
073QB8KJ2D00A4X
It works fine, when entering CNB0K2W5JK
The tool is a simple serial number comparison.
Just for test, I've entered this line into the code:
aaa="073QB8KJ2D00A4X"
When running in the step-by-step mode and hovering mouse over the "aaa" I'm getting: aaa= and then nothing.
Not even single "" like aaa="" or so. Just aaa=
After retrying multiple things - I believe it's about the value I enter:
073QB8KJ2D00A4X
Could be, that for access/vba it's some control string or so?
I'm just dumb now...
Thanks in advance for any help
Marek
p.s. Source fields are plain text boxes. And here's the code:
Dim user As String
Dim 1stSN As String
Dim 2ndSN As String
1stSN = Me.frmSSN.Value
2ndSN = Me.frmHPSN.Value
Then the values are being used as a part of SQL query. The problem is - in this situation - query doesn't work, as the sql string looks like:
"Select * From sbo_SerialSource where SN like " and nothing after "like".
Debug shows the correct value (with serial number), but query fails with "syntax error" message. Seems like there are some strange "control characters" are being created/added.
That's it.
And I have to use the serial numbers as these "strange ones", because that's how they come from the vendor.

VBA Type mismatch Error - inconsistency

Okay, I'm so confused right now, I have written some VBA which is a plugin in Excel. There is a line which reads:
obsValue = CDbl(dataSplit(1))
The value of dataSplit(1) is the String 21440.5395043259
This line of code works okay...for me.
For another user of my code they get
Run time error '13:' Type mismatch.
They click on debug, it takes them to this line of code, and they hover their cursor over the dataSplit varaible, and it does indeed read the same value as above.
We are both using Excel 2010.
How can this be?
The language settings of Excel will determine what the decimal separator is. It seems likely that for your user, their language settings are different, and so the "." is probably treated as a thousands separator. The CDbl function is locale aware, so it "sees" numeric strings according to the regional settings of the current user.
You could try using the Val function, which will always treat the period as a decimal separator.
obsValue = Val(dataSplit(1))

Crystal Reports Display String Formula Error

I have an application which is running over hundred of system , I am facing an error in formula statement and only the best and optimal solution to change in Stored Procedure rather change in Report DLL .
Below i m attaching the screen shots , If any body suggest the best solution.
When Gross < 0 then this error occures.
any body suggest the solution
You are getting the error because the string you are converting to number doesn't have any numeric value as string but it contains string characters...
You are again converting Number to String after converting to number... why this multiple conversions.. instead use string value directly..
If you still want to do the same way then suggested approach is to check first with IsNumeric and then convert to number.
Main thing to consider is what does the variable String contains if Gross<0