Select Data from DataTable that are ISNUMERIC - vb.net

I am trying to select data from DataTable using ISNUMERIC function, unfortunately I get error below:
System.Data.EvaluateException: "The expression contains the undefined
function call ISNUMERIC()."
this is my current code (sorry some parts are in German but to translate I don't think it makes sense)
Public dtFakturas As New DataTable
Artikelgruppe_Einfuegen(rows:=dtFakturas.Select("FAK_KundenNr = " & Adr_Nr &
" AND Liegenschaft = '" & Liegenschaft & "' " &
" AND Mandant = '" & Mandant & "' " &
" AND von = '" & VerrechnungsPeriode.Von & "'" &
" AND bis = '" & VerrechnungsPeriode.Bis & "'" &
" AND FAK_BoMatNr <> ''", "FAK_BoMatNr").Where(Function(r) IsNumeric(r("FAK_BoMatNr"))),
Postable:=PosTable,
Adr_Nr:=Adr_Nr,
Mandant:=Mandant,
Ueberschrift:="", MasterMandantConnectionString:=MastermandantConnectionString,
Verrechnen_bis:=VerrechnungsPeriode.Bis)
Private Function IsNumeric(ByVal o As Object) As Boolean
Dim result_ignored As Decimal
Return o IsNot Nothing AndAlso Not (TypeOf o Is DBNull) AndAlso Decimal.TryParse(Convert.ToString(o), result_ignored)
End Function
my question is: how do i correct a select query to function properly and only give me values that are ISNUMERIC?

Related

Why does "Enter parameter value" appears using INSERT INTO VALUES?

I'm always getting the "enter parameter value error" when I try to run this code. The error refers to idPat, idSP, idPar and measure_value.
I have checked with the debug idPar, idPat, idSP and measure_value and they assume the correct value; the errors comes out when I run sql.
Dim idPar As Integer
Dim idPat As Integer
Dim idSP As Integer
Dim current_fc As String
Dim namePar As String
Dim sql As String
Dim measureValue As Integer
current_fc = TempVars!CurrentUsername
namePar = Me.cmbManualDailyPar.Value
measureValue = Me.txtMeasureValue
idPar = Nz(DLookup("ID_par", "Parameter", "name_par = '" & namePar & "'"), 0)
idPat = Nz(DLookup("ID_patient", "Patient", "fiscal_code = '" & current_fc & "'"), 0)
idSP = Nz(DLookup("ID_SP", "Diagnosis", "ID_patient = " & idPat & ""), 0)
sql = "INSERT INTO Measure(ID_par, measure_value,ID_SP,ID_patient)" & _
" VALUES(idPar,measureValue,idSP,idPat);"
DoCmd.RunSQL sql
Your VALUES part is just text. What you need is:
" VALUES(" & idPar & "," & measureValue & "," & idSP & "," & idPat & ")"
You may also need to enclose text values in database quotes, for example:
" VALUES(" & idPar & ", '" & measureValue & "'," & idSP & "," & idPat & ")"
to enclose measureValue if it is text.

Syntax using Dlookup in a loop with strings and dates

I am trying to get a command working to lookup if objects have been already selected for certain dates. I would like to do this with the Dlookup method, problem is, I am still getting used to SQL syntax. This is the code I am trying to get working:
Dim i, coun as Integer
For i = 1 to 10
If IsNull(DLookup("[Column1]", "Table1", "[Column1] LIKE '*AT 1/*' & i "
And (("[Date_Beg] BETWEEN #" & Me.txtDate1 & "#" AND "#" & Me.txtDate2 & "#")
Or ("[Date_End] BETWEEN #" & Me.txtDate1 & "#" AND "#" & Me.txtDAte2 & "#")))) = True Then
coun = coun
else
coun = coun + 1
End If
When it gets executed, I get an error
Run-time error '13': Type mismatch
EDIT: Please go see my answer to see the code that ended up working for me.
The problem is with the SQL string you are trying to pass into the DLookup function. When you need SQL strings like this I find it best to store them as variable and then pass the variable into the function. Let me know if this works for you.
Sub QueryExample()
Dim i, coun As Integer
Dim strSQL As String
For i = 1 To 10
strSQL = "[Column1] LIKE *AT 1/*" & i
strSQL = strSQL & " And (([Date_Beg] BETWEEN #" & Me.txtDate1 & "# AND #" & Me.txtDAte2 & "#)"
strSQL = strSQL & " Or ([Date_End] BETWEEN #" & Me.txtDate1 & "# AND #" & Me.txtDAte2 & "#))"
'Debug.Print strSQL
If IsNull(DLookup("[Column1]", "Table1", strSQL)) = True Then
coun = coun
Else
coun = coun + 1
End If
Next i
End Sub
Taking the suggestion from #pheeper and modifying his code a little bit I got the program working. This is what worked:
Dim i, coun as Integer
For i = 1 To 10
strSQL = "[Column1] LIKE ""*AT 1/" & i & "*"""
strSQL = strSQL & " And (([Date_Beg] BETWEEN #" & Me.txtDate1 & "# AND #" & Me.txtDate2 & "#)"
strSQL = strSQL & " Or ([Date_End] BETWEEN #" & Me.txtDate1 & "# AND #" & Me.txtDate2 & "#))"
'Debug.Print strSQL
If IsNull(DLookup("[Column1]", "Table1", strSQL)) = True Then
coun = coun
Else
coun = coun + 1
End If
Next i

Combobox filtering for listbox output is not working as expected

I have a few controls (Combobox's I call DDL's) that I use as filters for a dynamic query, shown below.
I have a region, division filter - and BCI/BCV/ABC/etc dropdowns.
When I select the region and division, the output list box correctly filters out everything except THOSE region/divisions. Good.
The problem comes in when I use the other DDL's, ABD/BCI/etc... those do not filter out correctly and I think it is with my and/or clauses below.
Can anyone see anything glaring or point me in the right direction to get this so that every control and ddl element filters out the data it is intended for - while keeping it in a format where the SQL itself is part of a string, like in my example?
Private Sub goBtn_Click()
strSQL = "SELECT [account_number], [BCI_Amt], [BCV_Amt],[ABC_Amt], [other_Amt], " & _
"[BCI_Amt]+[BCV_Amt]+[ABC_Amt]+[other_MRC_Amt], Division_Name, Region_Name, " & _
"Tier, Unit_ID, Name, Description_2 " & _
"FROM dbo_ndw_bc_subs " & _
"WHERE DivisionDDL = [Division_Name] and RegionDDL = [Region_Name] " & _
" and ( [BCI_Ind] = CheckBCI.value or [BCV_Ind] = CheckBCV.value or [ABC_Ind] = CheckABC.value " & _
" or BCIServiceDDL = [Tier]" & _
" or BCVServiceDDL = [Description_2]" & _
" or ABCServiceDDL = [Unit_ID] )" & _
"ORDER BY 6 asc"
Me.output1.RowSource = strSQL
End Sub
One of the combo box DDL control codes. There are check boxes that make the combo box visible or not visible.
Private Sub CheckBCV_Click()
If Me.CheckBCV = vbTrue Then
Me.BCVServiceDDL.Visible = True
Me.BCVServiceDDL = "Select:"
strSQL = "SELECT Distinct subs.[Description_2] FROM dbo_ndw_bc_subs "
Me.BCVServiceDDL.RowSource = strSQL
Me.BCVServiceDDL.Requery
Else
Me.BCVServiceDDL.Visible = False
Me.BCVServiceDDL = ""
End If
End Sub
Edit: Added additional code to the first code block for context, and updated some comments.
To reiterate the point of my question - Since some of the DDL's work as expected while the others do not. Is it in the AND/OR section where I have a problem - or am I forced to do an IF/IIF statement in the select. (And if I do this IF solution - how would that be incorporated into a string the way I have it now, I have not seen an example of this in my research on a resolution).
I think your top code sample should read more like this:
Private Sub goBtn_Click()
Dim strSQL As String
Dim strWhere As String
Dim strOp As String
strSQL = "SELECT [account_number], [BCI_Amt], [BCV_Amt],[ABC_Amt], [other_Amt], " & _
"[BCI_Amt]+[BCV_Amt]+[ABC_Amt]+[other_MRC_Amt], Division_Name, Region_Name, " & _
"Tier, Unit_ID, Name, Description_2 " & _
"FROM dbo_ndw_bc_subs "
strWhere = ""
strOp = ""
If Not IsNull(Me.DivisionDDL.Value) Then
strWhere = strWhere & strOp & "(Division_Name = """ & Me.DivisionDDL.Value & """)"
strOp = " And "
End If
If Not IsNull(Me.RegionDDL.Value) Then
strWhere = strWhere & strOp & "(Region_Name = """ & Me.RegionDDL.Value & """)"
strOp = " And "
End If
If Me.CheckBCI.Value Then
strWhere = strWhere & strOp & "(Tier = """ & Me.BCIServiceDDL.Value & """)"
strOp = " And "
End If
If Me.CheckBCV.Value Then
strWhere = strWhere & strOp & "(Description_2 = """ & Me.BCVServiceDDL.Value & """)"
strOp = " And "
End If
If Me.CheckABC.Value Then
strWhere = strWhere & strOp & "(Unit_ID = """ & Me.ABCServiceDDL.Value & """)"
strOp = " And "
End If
If Len(strWhere) > 0 then
strSQL = strSQL & " WHERE " & strWhere
End If
strSQL = strSQL & " ORDER BY 6 asc"
Me.output1.RowSource = strSQL
End Sub
This is wordier, but much closer to correct. P.S. I guessed that all values are strings. If not remove the quoting around non-string values.

Case when inside VB.net query

sql = "INSERT INTO Machine_Master(Machine_Code,Machine_Name,Status)" _
& " VALUES ('" & textmachinecode.Text & "','" & textdescription.Text & "', '" & combostatus.Text & "')"
How to change text from combobox? When it's saved in database it will be integer. In this case, text combostatus.Text active will be 1 when saved and not active will be 0.
As Patrick said, you don't need case when since you can do that on the vb.net side, but you should use parameters to avoid problems:
Dim result As String = String.Empty
sql = "INSERT INTO Machine_Master (Machine_Code,Machine_Name,Status) " & _
" VALUES (#CODE, #DESC, #STATUS)"
Using cn As New SqlConnection("connectionString"), cmd As New SqlCommand(sql, cn)
Dim status As Integer = -1
Select Case combostatus.Text.ToLower()
Case "active" : status = 1
Case "not active" : status = 0
End Select
cmd.Parameters.AddWithValue("#CODE", textmachinecode.Text)
cmd.Parameters.AddWithValue("#DESC", textdescription.Text)
cmd.Parameters.AddWithValue("#STATUS", status)
result = cmd.ExecuteScalar().ToString()
End Using
You don't need a case when, you could simple solve this client side (mind that using parameters is better, as already suggested!)
Use this condition:
IIf(combostatus.Text = "active", 1, 0)
So, totally it becomes:
sql = "INSERT INTO Machine_Master(Machine_Code,Machine_Name,Status)" _
& " VALUES ('" & textmachinecode.Text & "','" & textdescription.Text & "', " _
& IIf(combostatus.Text = "active", 1, 0) & ")"

how to make condition to update table?

i want to make condition to update my table if there's already same data (in the same column) inserted in the table.
im using
If String.ReferenceEquals(hotel, hotel) = False Then
insertDatabase()
Else
updateDatabase()
End If
this is the updateDatabase() code...
Dim sql2 As String = "update infoHotel set nameHotel = N" & FormatSqlParam(hotel) & _
", streetAddress = N" & FormatSqlParam(StreetAddress) & _
", locality = N" & FormatSqlParam(Locality) & _
", postalCode = N" & FormatSqlParam(PostalCode) & _
", country = N" & FormatSqlParam(Country) & _
", addressFull = N" & FormatSqlParam(address) & _
", tel = N" & FormatSqlParam(contact) & _
"where hotel = '" & hotel & "')"
this is the formatSqlParam() code:
Function FormatSqlParam(ByVal strParam As String) As String
Dim newParamFormat As String
If strParam = String.Empty Then
newParamFormat = "'" & "NA" & "'"
Else
newParamFormat = strParam.Trim()
newParamFormat = "'" & newParamFormat.Replace("'", "''") & "'"
End If
Return newParamFormat
End Function
the function manage to go into updateDatabase() but has some error saying
"Incorrect syntax near 'Oriental'."
Oriental is the data inserted in the table. is it suitable to use ReferenceEquals?
im using vb.net and sql database..tq
You should be using parameterized queries instead of concatenating strings like so:
Dim sql2 As String = "Update InfoHotel" _
& "Set nameHotel = #nameHotel" _
& ", knownAs1 = #knownAs1" _
& ", knownAs2 = #knownAs2" _
& ", knownAs3 = #knownAs3" _
& ", knownAs4 = #knownAs4" _
& " Where hotel = #hotel"
If you used parameterized queries, you wouldn't have to worry about trying do quote replacement and potentially introducing a SQL Injection vulnerability.
You have a single quote in your data, as in:
Oriental's
Show us the code for FormatSqlParam() -- the bug is probably in there.
Or else you left out the single quotes around the hotel name:
where hotel = '" & hotel & "')"
I hope that's not it, because it would mean you're using the name as a key, a very bad idea.