textbox matching pattern from database table - vb.net

I have a name textbox and I want to find any names included in the typed text in the database table patient details name column. I know how to use LIKE operator if you know the letters you want the search to start/end with etc. but this time I want textbox. I think my issue is with the quotations; I tried to play around with it but it didn't work!
From x in PatientDetails where ( x.Patient_Name Like '%" Textbox1.Text "%' )
For example: If a Patient name in the database is: John Matt
and a user typed Matt, the above record for John Matt should be returned.
P.S I tried looking it up in Google but it mostly discuss characters not entered text box
Thank you all.

Something like this would do
C#
var query = (from x in PatientDetails
where x.Patient_Name.Contains(Textbox1.Text)
select x).ToList();
VB.NET - Converted using CodeConverter
Dim query = (From x In PatientDetails Where
x.Patient_Name.Contains(Textbox1.Text)x).ToList()

Related

SQL Injection from a textbox

I'm new to MS Access.
So, I wrote a SQL query(query name = qryEmployeeInfo) which shows employee information. The query outputs two columns. The employee ID(header name = employee_ID) and the corresponding employee address(header name = employee_address).
My Access form has a text box(text box name = txtEmployeeID) that I want the user to be able to enter the employee_ID into and have it output the corresponding employee_address into another text box (text box name = txtEmployeeAddress). I also want the employee_address to be in the format of a string variable so I can perform other VBA checks on it later(for example - if LIKE "California" THEN...something).
I want to write what (I think) is called an injection SQL query so that I can pull the address data from the query for that specific employee_ID. I believe the format should look like this:
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("select employee_address from qryEmployeeInfo where employee_ID = "' & txtEmployeeID & "'", dbOpenDynaset)
Do I have this written correctly?
If so, then how do I get that output into a string variable format(variable name = strEmployeeAddress)?
After I get the employee address into a string variable format I want to simply use txtEmployeeAddress.value = strEmployeeAddress to populate the employee address text box. Again, I also want the employee_address to be in the format of a string variable so I can perform other VBA checks on it later(for example - if LIKE "California" THEN...something).
Any help you could provide would be greatly appreciated.
If employee_ID is a number field, remove apostrophe delimiters.
If employee_ID is a text field, move first apostrophe so it is within quote marks.
"select employee_address from qryEmployeeInfo where employee_ID = '" & txtEmployeeID & "'"
Then txtEmployeeAddress = rs!employee_address
However, instead of opening a recordset object, could just use DLookup.
txtEmployeeAddress = DLookup("employee_address", "qryEmployeeInfo", "employee_ID =" & txtEmployeeID)
Or even better, eliminate the VBA. The DLookup() expression can be in textbox ControlSource property, just precede with = sign.
However, domain aggregate functions can perform slowly. So instead of textbox, use a combobox for selecting employee. Include employee information in combobox RowSource. Expression in textbox references column of combobox: =combobox.Column(1). Issue is combobox has limit on how many characters can be pulled into column so if field is memo (long text) type, this approach is not feasible and should use DLookup.
The address will be available in textbox for use as long as this form is open. If you want the address to be available to other procedures even after form is closed, then need to set a global or public variable. Such variable must be declared in a module header to make it available to multiple procedures. TempVars are another way to hold values for future use. I have never used them.

Sorting Data on Form in Access 2010 by VBA

I have the following setup:
Table "Mitarbeiter" (Users) with fields: "UNummer" / "Sortierung" /....
Table "Mo01" (a sheet for every month) with fields: "UNummer" / "01" / "02" / ....
The Field UNummer in Table Mo01 is a combination field that gets Mitarbeiter.UNummer and saves it as text
I call a Form "Monatsblatt" that is based on the table Mo01.
In that Form I have a Field "fldSort" that is calling "Sortierung" from table "Mitarbeiter". The Data in that field is based on "=DomWert("Sortierung";"Mitarbeiter";"UNummer = '" & [ID] & "'")"
This works and looks like this:
I am trying to sort the form by that "fldSort" in Form "Monatsblatt" by using this code:
Form_Monatsblatt.OrderBy = "fldSort"
Form_Monatsblatt.OrderByOn = True
When I start the form with that code running, Access asks for parameters:
I tried a lot of different ways of writing the code, referencing to the field in different ways. I do NOT want to base the form on anything other then the table.
Why not ask the wide world watch "Why Access asking me for Parameter"? That would have brought you to the clue I think. Debug.Print or MsgBox your .OrderBy and you see it's "fldSort", not a valid sort. Access is assuming you want to use a parameter called fldSort, but you want the string in the variable fldSort, but it's not recognized, because of the double quotes surrounding it. Everything between 2 double quotes is interpreted as a string, even it's a var name.
Delete the quotes and everything will work fine (if your sort string is sufficent)!
Form_Monatsblatt.OrderBy = fldSort
[Update]
Late, but now I see the clue. You added a calculated field to the form, but you can't sort or filter them.
Instead of appending this field to the table, create a query and add it there, then you bind the form to the query and add the field to the form. Now you can filter and sort as you like!
The query looks like this:
SELECT *,
Dlookup("Sortierung","Mitarbeiter","UNummer = '" & [ID] & "'") AS ldSort
FROM Mo01;
Or with a join:
SELECT
Mo01.*,
Mitarbeiter.Sortierung AS fldSort
FROM
Mo01
LEFT JOIN
Mitarbeiter
ON
Mo01.ID = Mitarbeiter.UNummer;
Now you can use
Form_Monatsblatt.OrderBy = "fldSort"
Form_Monatsblatt.OrderByOn = True
because you have a bound control called fldSort.
[/Update]

Use an Access Forms Unbound text box as a Field filter in a table

Access 2013 - Reference an Unbound text box on a Form
I am currently trying to use an unbound text box [Text161] on a Form name [DCM_Gap_Servers] to sort information through a table. I want the query that I created to be able to take the users input from [DCM_Gap_Servers]![Text161] as the field that is being sorted from the table names 'Server'.
This is the SQL I am using right now in the query:
SELECT * FROM Servers WHERE "Forms![DCM_Gap_Servers]![Text161]" IS NULL
** I have already Tried:
"Forms![DCM_Gap_Servers]![Text161]" ; (Forms![DCM_Gap_Servers]![Text161]); Forms.[DCM_Gap_Servers]![Text161]
This will work at any time if I replace the Text Box reference with the actual Field name I am using, but since there are hundreds of combinations of fields, I need the reference to work.
I have looked all over, and I can't seem to find the correct answer. I am willing to do it in VBA if needed, whatever it takes to get the filtering done correctly.
Thank You.
It is:
SELECT * FROM Servers WHERE Forms.[DCM_Gap_Servers].[Text161] IS NULL
but that will just select all records whenever your textbox is Null.
So it rather is:
SELECT * FROM Servers WHERE SomeField = Forms.[DCM_Gap_Servers].[Text161]
To use the form value as a field name, you must use concatenated SQL:
strSQL = "SELECT * FROM Servers WHERE " & Forms![DCM_Gap_Servers]![Text161].Value & " IS NULL"
This you might pass to the SQL property of an existing query object:
MyQueryDef.SQL = strSQL
Or:
Constant SQL As String = "SELECT * FROM Servers WHERE {0} IS NULL"
FieldName = Forms![DCM_Gap_Servers]![Text161].Value
MyQueryDef.SQL = Replace(strSQL, "{0}", FieldName)
Of course, take care the the field name isn't a zero length string.

Speed up the search process in a big amount of data?

I have a SQLite (v3) database with following example table:
5 columns ("FirstName", "LastName", "Street", ZIPCode, "City")
more than 1'100'000 rows
I'm looking for the fastest method in VB.NET to find an entered search string in the entire data. This entered search string should match the complete content of a field OR only a part of it.
So far, I try to load the whole data into a datatable (takes about 40 seconds). Then I try to search with this SQL command:
dt.Select("FirstName LIKE '%" + SearchString + "%'")
In my other method, I create a sorted DataView and search with the RowFilter command:
dvSORTED.RowFilter = "FirstName LIKE '%" & SearchString & "%'"
The first method returns smaller amounts of results (~100) within 1,1 seconds, the second method needs 1,5 seconds.
This is true for a search in a single column. For every additional column, the search time will be multiplied by the above time. A search in all columns needs about 8 seconds (5 x 1,5).
Little comparison: If I execute this SQL command directly in SQLiteSpy
SELECT FirstName, LastName, Street, ZIPCode, City FROM Addresses WHERE FirstName LIKE "%Peter%" or LastName LIKE "%Peter%" or Street LIKE "%Peter%" or City LIKE "%Peter%"
then I have to wait about 10 seconds for the result.
How can I speed up the search process in all the available data?
Try putting all of the strings you need to search in a single string and ask if it contains the value you are looking for. Here are my test results with 1.7M records (fairly unique):
If DT.Select("street like '%" & SearchString& "%'").Count > 0 Then
This took about 11 seconds to run
If I put all my values into a string with a pike "|" separator and then ran
If StringToSearch.Contains(SearchString) then...
This took about 20ms. Even the time to put all the strings into a single string only took 2 seconds so I could reload the StringToSearch every time and still be faster than the SELECT. I wasn't even using a StringBuilder. But, if you can keep the variable 'StringToSearch' around, and not rebuild it everytime, it will be way faster.
BTW, iterating through the rows and asking each field if it contains the value took 1.5 seconds:
If dr("street").ToString.Contains(SearchString) Then
Lastly, I did try Regex but is was the same time as Contains.

Search access db using 3 fields

How can I search using 3 fields on vb.net
Usually we use something like:
Dim query As String = "Insert into () values () Where id=1"
I am not really good in access or sql, so I asked.
week, sow order and piglet# are different fields, because I need to segregate in the future if the user searches only on weeks.
What I want is, get the data which is the same as what the user inputs and display it in the DGV. Is something like this possible:
Dim query As String = "Insert into (FarrowDate) values (dtpFarrow.Text) Where week=1,soworder=1,pigletnumber=0"
' display the data in the DGV '
Help please, they only gave me a 2 week deadline >.<
In your query you cannot use where in insert statement.Use update statement
Update tablename set FarrowDate='' where PigletNumber='0' And Week='1' And SowOrder='1'