Is it possible to edit data that is grabbed from a recordset? In my case, I am trying to add quantities together so that I can get a total. So an example of what I am trying to do would be:
<%
set rs = server.CreateObject("ADODB.recordset")
totalqty = 0
do NOT while rs.EOF
totalqty = totalqty + rs("QTY")
loop
>%
Whenever I tried to do something like this, I would always get an 'Type MisMatch' Error and I'm not sure how to resolve this problem.
As always, any and all help would be appreciated.
Try to "cast" the value in the recordset like so:
CDbl( rs.fields("QTY").value )
This will cast the value to a double. If the value is null you will get en error so you have to check that first...
Or you can write a function to always get the correct type:
public function parse(value, alternative)
dim val
val = trim(value & "")
parse = alternative
if val = "" then exit function
on error resume next
select case varType(parse)
case 2, 3 'integer, long
parse = cLng(val)
case 4, 5 'single, double
parse = cdbl(val)
case 6 'currency
parse = ccur(val)
case 7 'date
parse = cDate(val)
case 11 'bool
parse = cBool(val)
case 8 'string
parse = value & ""
case else
on error goto 0
lib.throwError("type not supported. val:" & value & " alt:" & alternative)
end select
on error goto 0
end function
dim val : val = rs("QTY")
val = parse(val, 0)
' now val is always an integer (either the value from db or 0)
ulluoink's solution will work, but this is simpler...
function ToDbl(vIn, nDefault)
'Convert a variant to an integer using default where necessary
if isnull(vIn) then
ToDbl = nDefault
else
if IsNumeric(CStr(vIn)) Then
ToDbl = CDbl(vIn)
else
ToDbl = nDefault
end if
end if
end function
Then just call:
totalqty = totalqty + ToDbl(rs("QTY"), 0)
Related
This is the first time I've asked a question although I have found the solutiion to many of my broblems here over the years.
I have a frustrating problem to which I cannot find an answer. I want to do the initial read prior to a read sequentially through a linked table opened as a dynaset DAO recordset using a specific index name as defined on the table.
My code returns error 3251 'operation is not supported...' on the .index line. No doubt there is an obvious solution (Mind you, I'm trying to avoid opening a SQL query which would be the obvious answer).
Public Function IOrdCustomerStock(CustomerID As Long, ProductID As Long, KeepRsOpen As Boolean, Optional UseIndexName As String) As Boolean
Set zcls_CS.CS_rs = CurrentDb.OpenRecordset(Name:=CS_TableName, Type:=RecordsetTypeEnum.dbOpenDynaset)
With zcls_CS.CS_rs
If Not IsMissing(UseIndexName) Then
.Index = UseIndexName
End If
.FindFirst "CS_CustomerID = " & CustomerID & " and CS_ProductID = " & ProductID
If .NoMatch Then
zcls_CS.CS_EOF = True
Else
zcls_CS.CS_EOF = False
zcls_CS.CS_ID = .Fields("[ID]")
zcls_CS.CS_CustomerID = .Fields("[CS_CustomerID]")
zcls_CS.CS_PhysSalesStock = .Fields("[CS_PhysSalesStock]")
zcls_CS.CS_ProductID = .Fields("[CS_ProductID]")
zcls_CS.CS_PurQuantityRecvd = .Fields("[CS_PurQuantityRecvd]")
zcls_CS.CS_PurUnitDesc = .Fields("[CS_PurUnitDesc]")
zcls_CS.CS_PurUnitFactor = .Fields("[CS_PurUnitFactor]")
zcls_CS.CS_SaleQuantityAlloc = .Fields("[CS_SaleQuantityAlloc]")
zcls_CS.CS_SaleQuantityOrdered = .Fields("[CS_SaleQuantityOrdered]")
zcls_CS.CS_SaleUnitDesc = .Fields("[CS_SaleUnitDesc]")
zcls_CS.CS_SaleUnitFactor = .Fields("[CS_SaleUnitFactor]")
End If
End With
If Not KeepRsOpen Then
Call IOclCustomerStock
End If
IOrdCustomerStock = Not zcls_CS.CS_EOF
End Function
Once I'd restricted the problem to linked tables, I found the following post:
https://social.msdn.microsoft.com/Forums/office/en-US/d402a8d2-0771-458c-b57e-09e2d6f0c536/trying-to-open-a-linked-table-whats-going-on?forum=accessdev
I don't pretend to understand the OpenDatabase parameters but it works. I just need to add the usual error handling to my little proof of concept:
Public Function IOksInitRsIX1CustomerStock(UseIndexName As String, CustomerID As Long, ProductID As Long) As Boolean
Set zcls_CS.CS_rs = OpenDatabase(Mid(DBEngine(0)(0).TableDefs(CS_TableName).Connect, 11)).OpenRecordset(CS_TableName)
With zcls_CS.CS_rs
zcls_CS.CS_rs.Index = UseIndexName
If (CustomerID > 0 And ProductID > 0) Then
.Seek "=", CustomerID, ProductID
Else
If CustomerID > 0 Then
.Seek "=", CustomerID
End If
End If
If .NoMatch Then
zcls_CS.CS_EOF = True
Else
zcls_CS.CS_EOF = False
zcls_CS.CS_ID = .Fields("[ID]")
zcls_CS.CS_CustomerID = .Fields("[CS_CustomerID]")
zcls_CS.CS_PhysSalesStock = .Fields("[CS_PhysSalesStock]")
zcls_CS.CS_ProductID = .Fields("[CS_ProductID]")
zcls_CS.CS_PurQuantityRecvd = .Fields("[CS_PurQuantityRecvd]")
zcls_CS.CS_PurUnitDesc = .Fields("[CS_PurUnitDesc]")
zcls_CS.CS_PurUnitFactor = .Fields("[CS_PurUnitFactor]")
zcls_CS.CS_SaleQuantityAlloc = .Fields("[CS_SaleQuantityAlloc]")
zcls_CS.CS_SaleQuantityOrdered = .Fields("[CS_SaleQuantityOrdered]")
zcls_CS.CS_SaleUnitDesc = .Fields("[CS_SaleUnitDesc]")
zcls_CS.CS_SaleUnitFactor = .Fields("[CS_SaleUnitFactor]")
End If
End With
IOksInitRsIX1CustomerStock = Not zcls_CS.CS_EOF
End Function
i am having a problem in recovering a null date from my database into datepicker in vb.net.
i was able to save a datein mysql with a null value but can't retrieve it to datepicker.
i tried this code but it doesn't work.
If reader.IsDBNull(0) Then
Return
Else
refdate.Text = reader.GetString("refdate")
End If
my code for retrieving is
Try
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
forid.Text = row.Cells("id").Value.ToString
Try
connection.Open()
Dim sel As String
sel = "select * from recordtracker where id ='" & forid.Text & "'"
com = New MySqlCommand(sel, connection)
reader = com.ExecuteReader
While reader.Read
Cancel.Show()
Clear.Hide()
rdate.Enabled = False
rfromtbx.Enabled = False
doctype.DropDownStyle = ComboBoxStyle.DropDown
ID.Text = reader.GetInt32("id")
doctype.Text = reader.GetString("Type_of_Document")
itemtbx.Text = reader.GetString("Items")
rfromtbx.Text = reader.GetString("Received_From")
rdate.Text = reader.GetString("Received_Date")
remarks.Text = reader.GetString("Remarks")
margnote.Text = reader.GetString("Marginal_Note")
reftotbx.Text = reader.GetString("Referred_To")
acttaken.Text = reader.GetString("Action_Taken")
'refdate.Text = reader.GetString("refdate")
'If reader.Read() Then
If reader.IsDBNull(0) Then
Return
Else
refdate.Text = reader.GetString("refdate")
End If
Delete.Show()
' End If
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
any help is appreciated.
For future use, this was the answer needed by the asker.
reader.IsDBNull(0) gets the value of index zero which I presume that refdate is not in the first index.
Using reader.GetString("refdate") gets the String value; therefore, when retrieving null value, it returns error so better use isDbNull(reader("refdate")) to check if the field is null or not. To get the String value, you can simply use reader("refdate").toString
I'm using Join(Lookupset) to find unique group values which returns a sequence number. This is my function:
Join(LookupSet(Fields!itemId.Value & Fields!UseByDate.Value & Fields!rackId.Value
, Fields!itemId.Value & Fields!UseByDate.Value & Fields!rackId.Value
, Fields!CustomerSeqNo.Value
, "PickingList"), ",")
The problem is on some items there are multiple transactions. I want to remove the duplicates.
I found a blog http://blogs.msdn.com/b/bobmeyers/archive/2012/06/18/creating-short-lists-using-the-lookupset-function.aspx but could not get SSRS Report Builder to reference Linq assembly. My issue is
How can I just show the unique values?
You don't need Linq, but you do still need custom code (in BIDS go to Report -> Report Properties -> Code)
You can put a RemoveDuplicates function in here, something like this:
Public Shared Function RemoveDuplicates(m_Array As Object()) As String()
System.Array.Sort(m_Array)
Dim k As Integer = 0
For i As Integer = 0 To m_Array.Length - 1
If i > 0 AndAlso m_Array(i).Equals(m_Array(i - 1)) Then
Continue For
End If
m_Array(k) = m_Array(i)
k += 1
Next
Dim unique As [String]() = New [String](k - 1) {}
System.Array.Copy(m_Array, 0, unique, 0, k)
Return unique
End Function
To use it in your Join:
Join(Code.RemoveDuplicates(LookupSet(...)),",")
I agree with #user3697615 that Report Code is best. However, I prefer to build it straight into a string:
public shared function JoinDistinct(
dups as object(),
delimiter as string
) as string
dim result as string = ""
system.array.sort(dups)
for i as integer = 0 to dups.length - 1
if i <> 0 then result += delimiter
if i = 0 orElse dups(i) <> dups(i-1) then result += dups(i)
next i
return result
end function
This way, we eliminate one nested function on the call:
=Code.JoinDistinct(LookupSet(...), ",")
If you're like me, you also want the elements in order based on frequency (descending order).
I created the following VisualBasic code to do so
Public Shared Function RemoveDuplicates(dataset As Object()) As String()
Dim unique As New System.Collections.Generic.List(Of String)
Dim frequency As New System.Collections.Generic.List(Of Integer)
For i As Integer = 0 To dataset.Length - 1
Dim index As Integer = -1
For j As Integer = 0 To unique.Count - 1
If dataset(i).Equals(unique(j)) Then
index = j
Exit For
End If
Next
If index < 0 Then
unique.Add(dataset(i))
frequency.Add(1)
Else
frequency(index) += 1
End If
Next
Dim uniqueArray As [String]() = unique.ToArray()
Array.Sort(frequency.ToArray(), uniqueArray)
Array.Reverse(uniqueArray)
return uniqueArray
End Function
This is based off others' answers where the SSRS expression is the following
Join(Code.RemoveDuplicates(LookupSet(...)),",")
Note: I learned VisualBasic in about an hour to solve this problem, so my algorithm probably isn't the most efficient.
I liked pwilcox's idea, so I wrote this one which filters out null and blank values.
Public Function JoinDistinct(arr As Object(), delimiter As String) As String
System.Array.Sort(arr)
Dim result As String = String.Empty
Dim lastvalue As String = String.Empty
For i As Integer = 0 To arr.Length - 1
If Not arr(i) Is Nothing And arr(i) <> lastvalue And arr(i) <> String.Empty Then
If result = String.Empty Then
result = arr(i)
Else
result = result + delimiter + arr(i)
End If
End If
lastvalue = arr(i)
Next
Return result
End Function
Usage:
=Code.JoinDistinct(LookupSet(...), ",")
I have been trying to figure this error out for the past few days with no luck. I am hoping one of you would be able to help. I am getting "value used in formula of wrong data type.
Quick explanation:
convert functions like this one to its corresponding text (20054/18393)*100.0
the 5 digit numbers are Field IDs that refer to questions.
ID Question
20054 How many days of year do you work
18393 How many days of vacation do you get a year
The result I am trying to get to is (How many days of year do you work / How many days of vacation do you get a year) *100.0
It could be easily done manually if it was just a hand full. I have over 2600 formulas that need to be converted.
I created this function below which is resulting in the error mentioned in the title. Any assistance would be greatly appreciated
Here is my function
Function Test(sInput As String) As String
Dim i As Long
Dim num As String
Dim Text, a, str, shortname As String
For i = 1 To Len(sInput)
a = Mid(sInput, i, 1)
If IsNumeric(a) Then
num = num & a
Text = ""
Else
If a = "." Then
num = num & a
Else
'search for num value in second sheet short name
shortname = WorksheetFunction.VLookup(WorksheetFunction.Int(num), Worksheets("questionlist").Range("A3:F2537"), 5, False)
num = ""
End If
Text = shortname & a
shortname = ""
End If
str = str & Text
Next
Test = str
End Function
The error is raised because you are passing blank value to INT Function in the line
WorksheetFunction.VLookup(WorksheetFunction.Int(num), Worksheets("questionlist").Range("A3:F2537"), 5, False)
To reproduce the error Type =INT("") in any cell
To fix this handle blank values
Updated Answer:
Function Formula2Text(ByRef myCell As Range) As String
Dim QuestionId As Integer
Dim strInput As String
'Get Formula instead of values
strInput = myCell.FormulaR1C1
'Use Regex to Catch all ID's
Set Regex = CreateObject("VBScript.RegExp")
Set rnglookup = Worksheets("questionlist").Range("A3:F2537")
Regex.Global = True
Regex.Pattern = "\d+"
For Each Match In Regex.Execute(strInput)
'Skip if the ID is 100
If (Match.Value <> 100) Then QuestionId = Match.Value
'Lookup ID in the rnglookup,Make sure the Ids are sorted in asc in the questionlist sheet
Qntxt = Application.VLookup(QuestionId, rnglookup, 5, False)
If IsError(Qntxt) Then Qntxt = "Missing Lookup"
'Replace the ID with the lookup
strInput = Replace(strInput, QuestionId, Qntxt)
Next
Formula2Text = strInput
End Function
Usage:In the cell next to the formula use the function by referencing the formula
=Formula2Text(A1)
I have an If statement which I was assuming was comparing each value to each other. However it seems no matter what the values are (e.g. all values contain a count of 4) it goes to the else. Am I missing something in the If statement?
If rst![CountOfProvider] = rst![CountOfDelivery Type] = rst![CountOfBU Creator] = rst![CountOfOrigin] = rst![CountOfSub-Destination] = rst![CountOfDestination Zipcode] = rst![CountOfCity] = rst![CountOfState] = rst![CountOfCost Zone] = rst![CountOfRate] = rst![CountOfMarket Name] Then
chk = False
Else
chk = True
End If
VBA doesn't perform that sequence of comparisons as you seem to expect.
Consider this simpler example from the Immediate window ...
Debug.Print 2 = 2
True
Debug.Print 2 = 2 = 2
False
I'm uncertain how VBA handles those multiple equality comparisons, but suspect it may be testing the first and then comparing the result from that with the next ... sort of like this ...
Debug.Print (2 = 2) = 2
False
The first comparison returns True, which is the integer -1 ...
Debug.Print CInt(2 = 2)
-1
So that means the final comparison would be equivalent to this ...
Debug.Print -1 = 2
And naturally that returns False.
The computationally quickest way is to hard code the comparisons. The more
extensible way is to test via a loop.
HansUp makes a good comment - you should be wary of potential null values and add in a handler to deal with them as desired (e.g. using Nz() in Access or IsNull() in any host environment)
'Method 1
If rst![CountOfProvider] = rst![CountOfDelivery Type] And _
rst![CountOfProvider] = rst![CountOfBU Creator] And _
...etc...Then
chk = False
Else
chk = True
End If
'Method 2
chk = True
For Each ele In Array(rst![CountOfDelivery Type], rst![CountOfBU Creator],...your others...)
If ele <> rst![CountOfProvider] Then
chk = False
Exit For
End If
Next ele