Hi all,
I have one problem with queries.
I am doing a program in vb.net and backend is ms access.
My problem is...
I have many controls on my form(windows application)
In textboxes If I enter data(not mandatory) and click the search button.
So depending upon the search criteria data will be displayed.
I wrote the foolwing code.
When I run this code, it is displaying the exception as "Data type mismatch exception".
Because some fields in the database are numeric fields, So for numeric fields it displays
the exception.
So I tried in another way as
When I execute this, it is also displalying the exception "datatype mismatch exception"
for text type fields.
How can I solve this problem,
Thanks
I have one problem with queries.
I am doing a program in vb.net and backend is ms access.
My problem is...
I have many controls on my form(windows application)
In textboxes If I enter data(not mandatory) and click the search button.
So depending upon the search criteria data will be displayed.
I wrote the foolwing code.
Code:
Dim ctl As Control
Dim sSQL As String
Dim sWhereClause As String
sWhereClause = " Where "
sSQL = "select * from Table1 "
For Each ctl In Me.Controls
If ctl.GetType.FullName = "System.Windows.Forms.TextBox" And & _ ctl.Text.Trim.CompareTo(String.Empty) <> 0 Then
If ctl.Text.GetType.FullName = "System.String" Then
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & Mid(ctl.Name, 4, ctl.Name.Length) & "=" & ctl.Text & ""
Else
sWhereClause = sWhereClause & " and " & Mid(ctl.Name, 4, ctl.Name.Length) & "=" & ctl.Text & "" & ctl.Text
End If
Else
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & Mid(ctl.Name, 4, ctl.Name.Length) & "=" & ctl.Text
Else
sWhereClause = sWhereClause & " and " & Mid(ctl.Name, 4, ctl.Name.Length) & "=" & ctl.Text & ctl.Text
End If
End If
End If
Next ctl
Dim strSQL As String
strSQL = sSQL & sWhereClause
Because some fields in the database are numeric fields, So for numeric fields it displays
the exception.
So I tried in another way as
Code:
For Each ctl In Me.Controls
If ctl.GetType.FullName = "System.Windows.Forms.TextBox" And & _ ctl.Text.Trim.CompareTo(String.Empty) <> 0 Then
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & Mid(ctl.Name, 4, ctl.Name.Length) & "=" & ctl.Text
Else
sWhereClause = sWhereClause & " and " & Mid(ctl.Name, 4, ctl.Name.Length) & "=" & ctl.Text & ctl.Text
End If
End If
Next ctl
for text type fields.
How can I solve this problem,
Thanks
Last edited by a moderator: