Inputting Data into Database and reading it - sql

I have an SQL database and 2 VB applications, teacher and student application.
In the teacher application, the SQL database is connected with datagridview. In the form there are 10-15 checkboxes. those checkboxes are a set of weak points the teacher can select for a student in their class. for eg- "weak in calculations" etc. I want, when the teacher select the checkboxes, they should be shown in the teacher application in a string form. I think assigning each check box an ID would be a lot better than inputting long strings into the database. when the teacher selects a checkbox, the ID for that checkbox goes into the datagridview selected cell. I am not sure how to seperate each ID in the same cell because the teacher can select multiple checkboxes.
I need help with seperating the IDs and then reading them.
http://www.filedropper.com/sample_18 here is a sample program i made, it shows entering the checkbox id into the datagridview.

Let's keep it simple. Let's say we have 3 check boxes.
So for saving to db:
dim totalValue as integer = 0
if (chkBox1.checked) then totalValue +=1
if (chkBox1.checked) then totalValue +=2
if (chkBox1.checked) then totalValue +=4
save to db the totalValue
For reading from db:
totalValue = get this value from db
chkBox1.checked = ((totalValue and 1)=1)
chkBox1.checked = ((totalValue and 2)=2)
chkBox1.checked = ((totalValue and 4)=4)

Related

How can i restrict the textbox value to stay same in the access database

My goal is to create an access database form with ordering multiple item at a time. But for this phase my end goal is to find the total cost by multiplying price per unit*quantity. Through my working out i got stucked by the problem of on change event. Basically, whenever i change a the id in the combo box it changes in all the textbox that i programmed.
Here is the scenario:
In this case the price per unit stays 1000 for all of the stock ID, whereas, when i have stock id of 2 i want the price to be 0 and so on.
This is how i programmed the get price per unit value
Private Sub StockIDCombo_Change()
Me.PricePerUnit.Value = Me.StockIDCombo.Column(3)
End Sub
Note that the form is continuous.
Have an additional field in the table to hold the calculation, and bind your textbox to this field.
Is your PricePerUnit control unbound? It should be bounded to some table field as other fields (StockID, Quantity).

Compare array to database and change matching elements

I am working on a hospital database and I am loading hospital beds to combobox. What I want to do is, if bed is already taken, I want to add (Occupied) after id of bed.
There is a room and bed list table. Room 101 has Three beds. 1001, 1002 and 1003. I have loaded Bed_No to an array. bed_ids_array()
There is a Patient-Bed Occupied table. As you can see 1002 and 1003 are occupied by patients. I read this data through while loop dr(1)
I am adding data to combobox in vb.net and it should look like this:
But it looks like this:
(when rechecking with different values same data comes when data is being read from access database)
The code I have used is this:
'Check if beds in array have patients
Call connect()
con.Open()
cmd = New OleDbCommand("Select * from Bed_to_Patient_Relation", con)
dr = cmd.ExecuteReader
While dr.Read
'Add all occupied stuff to combobox first
For i = 0 To room_bed_count - 1
'take first bed id and check all database
If bed_ids_array(i) = dr(1) Then
Cmbbx_Beds.Items.Add(bed_ids_array(i) & "Occupied")
Else
Cmbbx_Beds.Items.Add(bed_ids_array(i))
End If
Next
End While
What I want to do is to add "Occupied" to text if match is found, else, just enter normally
I suggest making a few changes. Ditch the combobox for a datagridview. You can still select the row you want, but have better visibility of the data. Join your two tables in the query to identify which beds are available or occupied. Include the bed and room id in the patient to bed relationship table, or better yet, assign a unique id for each bed. An occupied table may allow for a more effective join.
The issue I think you have with existing code is that you're adding the list twice, or not clearing it after each reload of the box. I find it much more efficient to populate a grid based on data from a query, such as the join mentioned above.
probably not the answer you were looking for.

vb if condition on items and price from database

I actually have a Access database where i have a table called "tblfreight" and two column is there (items and Price)
And I want to auto show the Price when I choose Items.
There is two Textbox: "txtitems" and "txtprice"
I want to be like this....
If txtitems.text = select from tblfreight and column items Then
txtprice.text = select from tblfreight and column Price
Else txtprice.text=0 (Zero)
You need to query your DB first..
Select * from tblfreight - will get you your data you need.
Then you need to put this data somewhere. You can use a combobox for this. Once you have your combobox filled with your data you then can set the price into a label or textbox easily.
You can use the combobox selected index changed event...
Inside the changed event something like this would work...
textprice.text = Combobox.selecteditem.value
This will give you a great start, that's why this answer just gives you the steps and not the full code.

Display text corresponding to Combobox selection in Microsoft Access 2010

I have a contact form and one of the fields in a the form is a Contact_Type_ID. This field is a number field which also corresponds to a text field in another table (e.g. 1 = expatriate).
When I cycle through the contacts, their Contact_Type_ID is 1, 2, 3... instead of Non-profit, CEO, Vice-president, etc. This is a problem because one has no idea what number 3 means.
I would like to a combobox that only displays the corresponding text.
I can't get the two columns and 0;1 format to work. My hunch is that it's because I'm drawing information from two different tables. I can generate the correct list, but then the main entry doesn't change as I cycle through the contacts to reflect the current contact's [Contact_Type_ID].
I can't edit any of the current tables because I am supposed to apply this application to a much larger scale database.
I also tried setting the SQL for the row source:
'Populate the connection combo box '
Dim typeSQL As String
typeSQL = "SELECT DISTINCT Contacts.[ContactTypeID], Contact_Types.[ContactType] " & _
"FROM Contacts, Contact_Types " & _
"ORDER BY Contact_Types.[ContactType];"
Me.cbo_ContactType.RowSource = typeSQL
However, I then have the same problem: the combobox won't update as I cycle through the contacts. I don't understand the difference between the rowsource and the controlsource. I feel that this distinction might be key here.
Assuming I understand correctly:
In the combo box properties, go to the Data tab.
Set the Control Source to Contact_Type_ID.
This means that when you go through the records, the combo box will correspond to the Contact_Type_ID field from your data.
Set the Row Source to "SELECT Contacts.[ContactTypeID], Contact_Types.[ContactType] FROM Contacts, Contact_Types ORDER BY Contact_Types.[ContactType];"
The Row Source indicates the data that the combo box has access to, which will help it determine how to display the control source value.
Set the Bound Column to 1
Set Limit to List to Yes
Now in the Format tab. Change Column Count to 2.
Next set the column widths to the 0;1 you mentioned in your question.
Now try looking at form view, and it should behave as you expected.
If it does not work, create a new control with these instructions.
As I understand your question, you have a form with contacts based on a table Contacts and that table contains a field called either Contact_Type_ID or ContactTypeID. You wish to display the description of the contact type from a table Contact_Types.
You do not need to join tables for your combo, it should be very simple. The key, as you suspected, is the Control source, which is the property that relates one table to another.
Control source : ContactTypeID '' or Contact_Type_ID, choose from the list
Row source : SELECT ContactTypeID, ContactType FROM Contact_Types ORDER BY ContactType
Bound column : 1
Column count : 2
Column widths : 0,2
As an aside, you can join tables in a combo and still set the first column to 0 and it will still work, it is all in the correct set up.

one problem in fetching a data from backend in vb.net

i have two item in form it is---1 is textbox and 2nd is combo box.
now i have single value in textbox .
but there is a multiple value for a combobox with is totally depend on a value of textbox....
*i am already add data to the backend files ... in which 1 columns that is called column A it's value is for textbox and 2 column that is called column B it's value for combo box *
When i write in textbox it's dependent ALL value can be get in the combo box.
Can any one solve my problem... It is a row vice search.....
I used a Ms Access for my backend......
How i can create a table that can be i used for a my problem
Give me any reference or codes.
This is probably Category->Product kind of scenario, where ONE Category can have ONE or MORE Products,
Category "Softdrink" can have Products("Coke","Pepsi" etc.)
Category "Harddrink" can have Products("8 PM","BP",etc.)
So when u will put "Softdrink" in your textbox then you want to get the relevant products for it. (Assumed you are using TextChange event)
SQL - select Product.Name from Category Inner Join Product On Category.Id= Product.Category_Id where CategoryName ="Softdrink"
and table structure would be like,