Search function in VB.NET - vb.net

This is my line of code to search the information from the database. I am trying to display the result obtained from the execution of this line. Should I just use a message box or use any object to display it? I do not want to use dataset.
ACommanad.CommandText = "SELECT * FROM Students where StudentID =" & DeleteRecordTextBox.Text & ""

Yes. A message box is perfectly acceptable way to display something to the user.

Related

DLookup When Trying Use Form Field as Criteria

I am new to Access and also to VB. I have a report that displays information on transformers, the report displays data from a query that queries data from two different tables. I would like to use a button to open a PDF document saved in one of the tables.
The code I have so far is this:
Private Sub Command70_Click()
Dim hypa As String
hypa = DLookup("[TestReport]", "TransformerPics", "TxID = " & [Reports]![TransformerInfoSheet]!TXID)
Application.FollowHyperlink [hypa]
End Sub
The error i get is Run-time error '2471' The expression you have entered as a parameter produced this error: 'TP00686'
TP00686 is the transformer number that is displayed on the report.
You need to have some quotes in there:
hypa = DLookup("[TestReport]", "TransformerPics", "TxID = '" & [Reports]![TransformerInfoSheet]!TXID & "'")
Please also see:
DLookup Usage Samples
Dlookup using a string variable as criteria parameter

Passing a query a parameter [Access 2016]

To make a longer story shorter:
I'm an Access noob, doing a quick-and-dirty conversion of a massive Excel spreadsheet into an Access database. Part of the requirements are to mimic some of the functionality of Excel, specifically, pulling data from a certain table and doing some basic calculations on it (sums, averages, etc.).
I've written a chain of queries to pull the data, count/sum it, etc., and have been testing them by using a manually-entered Parameter (i.e., the kind where the input box pops up and asks you to type a response). Now that I'm ready to drop these queries into a (sub)form, though, I have no idea how to automatically pass that parameter from a box in the form into the subform into the query.
Every query I've written uses a manually-entered Parameter named "MATCHNAME," which holds the name of an individual. In manual testing, if I enter this parameter on one query, all the queries it calls also get that value. So, I think I just need to figure out how to tell the top query what MATCHNAME actually is, and that'll take care of it.
Problem is, I don't know how to do that in Access. If it was any other programming language, I'd do something like "queryXYZ(MATCHNAME);", but I don't think I can do that in Access. Plus, since the values queryXYZ returns are all calculated, I'm not sure how to add an extra MATCHNAME field, nor how to actually make sure that gets read by the queries, nor how to make sure it gets passed down the chain. I've even tried creating a Parameter in design view, then trying to set up Link Master Fields, but the Parameter doesn't appear in that window.
I'd also like to re-run these queries whenever a new record is pulled up, but I'm not sure how to do that either--i.e., the numbers should be current for whatever record I'm looking at.
And, before we go there--I feel like a Relationship is out of the question, as the data itself is auto-generated, and is in rough enough shape to where I can't guarantee that any given key is wholly unique, and large enough (20k+) that, outside of writing a magical script, I can't assign a numerical key. However, I don't know much about Relationships in Access, so please prove me wrong.
(Is this all making sense?)
Do you have any suggestions for me--for how to make a subform read a field on the main form to run its queries on? Alternately, is there an easier way to do this, i.e., to bed SQL calls inside a form?
Thanks very much for your help...
You can use SQL as the recordsource of the subform in the property tab and use the afterupdate event of your matchname field to change yourform.recordsource = "Select * from table where filteredfieldname = & me.matchname & ";" . You can also use sql as the control source of form fields. To pass criteria to filter the subform using the whole table as the recordsource, add an event procedure to your field's after update event like this
`In the declarataions at the top
Global mtchnmfltr as string
Private Sub MATCHNAME_AfterUpdate()
'use the same procedure for Private Sub yourmainform_Current()
mtchnmfltr = "[yourfilterfield] = " & Chr(34) & me.matchname & Chr(34)
'if matchname is not text then just = "[yourfilterfield] = " & me.matchname
with me.subformname.form
.filter = mtchnmfltr
.filteron = true
end with
'Build your sql as a string for your sum avg fields etc. using mtchnmfltr in the where clause
me.yoursumfield.controlsource = "Select...where " & mtchnmfltr & ";"
'etc.
end sub
Or you could throw Matchname into a sql recordsource of the subform and add the function fields to the subform on the same on current and after update events
if me.newrecord = true then
me.dirty = false
end if
me.subform.form.recordsource = "Select Table.Matchname, sum(yourfield) as sumalias, _
(etc.) from yourtable where table.matchname = " & chr(34) & me.matchname & _
chr(34) & Group By table.matchname"
If you are storing your sums etc in a table you need to do it a bit different, since your controls controlsource are bound to fields.
dim strsqlsumfld as string
dim rs as dao.recordset
strsqlsumfld= "Select SUM.....AS sumfldalias where " & mtchnmfltr & ";"
set rs = currentdb.openrecordset(strsqlsumfld)
me.yoursumfield = rs("sumfldalias")
rs.close

How to add a value to Sql CommandText

Nowadays I am programming a cricket scoring software and all the details are saved in a database. I want to know how to add +1 to the field "W" in the database when click "Wicket".
cmd.CommandText = "UPDATE database.table SET W=W+1 WHERE bowler='" & frmmain.lblbowler.Text & "' "
In this code frmmain.lblbowler.text contains the bowler name.
Is this code correct? What changes must do? Please be kind enough to answer.
Don’t ever build a query this way! The input frmmain.lblbowler.Text is typically retrieved from a TextBox control on either a Windows form or a Web Page. Anything placed into that TextBox control will be put into frmmain.lblbowler.Text and added to your SQL string. This situation invites a hacker to replace that string with something malicious. In the worst case, you could give full control of your computer away.
Instead of dynamically building a string, as shown in your code, use parameters.
Anything placed into a parameter will be treated as field data, not part of the SQL statement, which makes your application much more secure.
Try the following
cmd.CommandText = "UPDATE database.table SET W=W+1 WHERE bowler = #bowler"
command.Parameters.Add("#bowler", SqlDbType.NVarChar)
command.Parameters("#bowler").Value = frmmain.lblbowler.Text

MS Access: How to get value of textbox on button click?

This is a complete newbie question, but I couldn't find any solution online (at least nothing to my understanding). I want to make a form with a textbox and a button and on click, the button should take the value written in the textbox, find the row with that ID in a table and change an attribute to unavailable. Normally I would do this with a simple SQL statement:
UPDATE table SET available='NO' WHERE id = *textbox.value*;
What should I write instead of textbox.value so that it works?
You can refer to an open form:
UPDATE table SET available='NO' WHERE id = Forms!MyForm!MyTextbox
Assuming that the form is the main form and not a subform. You can also run SQL in VBA. Make sure you do not update a record via a form and via a query or code at the same time.
Re Comments
In this case, the problem is corruption, something I often forget to watch out for. You will find a good article here : http://www.granite.ab.ca/access/corruptmdbs.htm, including Decompile.
Getting the value from the text box is even easier if:
the query is executed from VBA
the VBA is in the same form as the text box
If I understood the question right, this is the case here.
Then you can just do this:
Dim SQL As String
'case 1: when the id is a string value
SQL = "UPDATE table SET available='NO' WHERE id = '" & Me.NameOfTheTextBox & "';"
'case 2: when the id is a numeric value
SQL = "UPDATE table SET available='NO' WHERE id = " & Me.NameOfTheTextBox & ";"
CurrentDB.Execute SQL

VB - simply link a Data Grid View to display data that was selected from a ComboBox

I just want to display data in a DataGridView (from SQL - already made the connection) based on what is selected in a ComboBox (data that is also coming from SQL). The 2 are separete on the form. I am using VB 2010.
This doesn't work for me:
objCommand2.CommandText = "SELECT ProductID, Name, Color, Size, ListPrice FROM SalesLT.Product WHERE ProductCategoryID = " & cbCategory.SelectedValue
It gives me an error "invalid syntax around '=' "
Thank you!!!
Catalin
Try this
cbCategory.SelectedText
and look into passing command parameters to a stored procedure in the future.
Have you tried to assign the long string to a variable first to see if it really looks like a SQL statement. As Saif suggested, it may be something related to the value of the combobox. What I usually do is hardcode a SQL statement in a string to make the function work and then replace it with the dynamic string.
Dim s As String = "SELECT ProductID FROM SalesLT.Product WHERE ProductCategoryID=1"
One simple step at a time
Check that cbCategory.SelectedValue is indeed a numeric value. If it is, say "XXY", then you would need to code
WHERE ProductCategoryID = '" & cbCategory.SelectedValue & "'"
(watch out for the difference between ' and " !