Compare array to database and change matching elements - vb.net

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.

Related

Referencing SQL fields from a DataSet where 2 field names are the same

I have the following SQL query which I am loading in to a DataSet:
SELECT i1.* , i2.* From tblMMLettersImportTable i1 Join tblMMLettersImportTable i2 on i1.SectionID + 1 = i2.SectionID Where i2.startpage - i1.endpage <> 1
Idea is to check that the index for various sections of a document lead one page on to the other with no gaps. I.e section 2 ends on page 5 and section 3 starts on page 6.
I'm happy that the SQL works, however by joining on itself the field "SectionID" is duplicated. In SQL easy enough, just use i1. or i2. to reference the correct one.
The issue comes when I load this in to a VB.net Dataset. I need to raise an error message with something like:
MessageBox.Show("There is a page gap between sections " & row.item("i1.sectionID") & " and " & row.item("i2.sectionID")
I get the error message Column 'i1.intline' does not belong to table Table. Makes sense as that is not its name in the dataset. I've considered using the column number to reference the item to pull out, however the SQL Table tblMMLettersImportTable is deleted, created and populated dynamically depending on the type of Letter/document being produced so I cannot always guarantee that the columns will always numbered the same. This is also why i1.* and i2.* is used instead of listing each column.
Is there a way that I can reference 2 items in a DataSet that have the same item name with VB.Net?

Why the Recordset from VBA just return one record?

I have 3 tables, "persons", "per_resi" and "residence"
This three tables form a many to many relation.
Table "person" fields: id, name etc....
Table "residence" fields: id, Street etc.....
Table "per_resi" fields: person_id and residence_id (together principal index)
Well, the problem is when I design a query in the graphic Access tool it Works as it should be.
But if I do in VBA it only return 1 record.
Dim svivienda As String
Dim rvivienda As Recordset
svivienda = "SELECT tbl_persona.Id, tbl_vivienda.Calle, tbl_vivienda.Numero " _
& "FROM tbl_vivienda INNER JOIN (tbl_persona INNER JOIN tbl_perso_viv ON tbl_persona.Id = tbl_perso_viv.Id_persona) " _
& "ON tbl_vivienda.Id = tbl_perso_viv.Id_vivienda WHERE tbl_persona.Id = " & 168 & ";"
Set rvivienda = CurrentDb.OpenRecordset(svivienda, dbOpenDynaset)
I have tried LEFT JOIN and RIGHT JOIN but always the same just one record on the recordset.
Any ideas?
MS access 2013
Thanks in advance.
Thank guys,
This was a very novel question.
Here is the answer.
The RecordCount property does not report the amount of records you have.
The value of the RecordCount property equals the number of records
that have actually been accessed. For example, when you first create a
dynaset or snapshot, you have accessed (or visited) only one record.
If you check the RecordCount property immediately after creating the
dynaset or snapshot (assuming it has at least one record), the value
is 1. To visit all the records, use the MoveLast method immediately
after opening the Recordset, and then use MoveFirst to return to the
first record. This is not done automatically because it may be slow,
especially for large result sets.
Count the number of records in a DAO Recordset
Thanks!!!
Add following statement :
rvivienda.MoveNext
will return the next record of the recordset
or :
rvivienda.MoveLast
will return the last record of the recordset
You will see the result.
The goosie2018's answer is CORRECT. I just show you the simple way to understand.
SUMMARY
So, I think the recordset you get from the database will not show the result look like an Array or a List, but a cursor. And the default cursor points to the first row, so if you use :
rvivienda.RecordCount
you should receive the number of records you actually got.
Sorry for my English ! And thanks for reading.

Inputting Data into Database and reading it

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)

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.

How to make a drop down list (list box)in an MS Acess Query with values from two different tables

I need the drop down box above to display the date as shown and the ClassTypeDesc as you can see above the dropdown list shows 1/12/2010 twice. They have different ClassTypes Assigned to them.
The drop down list should show:
1/12/2010 ACLS-I Day One AM
1/12/2010 ACLS-I Day One PM
I need to know the statement to put in the Row Source Box on the lookup tab in the Field Properties to make this work.
Related Question on Making a drop down list
There's no need to concatenate the two columns. Based on the diagram, the SQL for your lookup combo box should look like this:
SELECT tblClassSession.SessionID, tblClassSession.Date, tblSessionType.ClassTypeDesc
FROM tblClassSession INNER JOIN tblSessionType
ON tblClassSession.SessionTypeID = tblSessionType.SessionTypeID;
Then in the properties for your lookup combo box, change ColumnCount to 3, and Column Widths to 0 (if you want to size the other columns, change Column Width sto something like 0";.75";1.5", and the List Width property to 2.25").
I may have gotten some of the field names wrong, but that's the basic idea.
(also, you probably really ought to rename tblClassSession.Date to tblClassSession.SessionDate so you don't run into problems with the fact that Date is a reserved word)
As you already have the right number of rows, you just need to concatenate enough fields to make it more useful, so your query would be something like:
SELECT c.Date + ' ' + s.ClassTypeDesc AS YourFieldName
FROM...