I got a bounded combobox to a group name in vb.net. i can view in the drop down items of the combobox the set of GROUP NAMES. but when i do an insert or update query, i need to make the selected group name refers to the GROUP NUMBER. I don't want to store letters in the database, instead i prefer numbers. how can i do that?!
Here is my code so far :
cmd1.Parameters.AddWithValue("#group", DirectCast(Additemcombobox.SelectedItem,
DataRowView).Item("GroupName"))
Storing the group name in database is currently working well.
My question might not be well explained. Please ask me in case...
any help would be appreciated
You can show one element to the user such as the name, but use a different one for the code to identify the item using DisplayMember and ValueMember
Dim SQL = "SELECT Id, Name FROM GroupCode ORDER BY Name"
...
GrpDT = New DataTable
GrpDT.Load(cmd.ExecuteReader)
cboGroup.DataSource = GrpDT
cboGroup.DisplayMember = "Name"
cboGroup.ValueMember = "Id"
The user will only see the names, while the code can use ValueMember:
Private Sub cboGroup_SelectedValueChanged(...etc
Console.WriteLine(cboGroup.SelectedValue)
End Sub
It prints the Group ID not the name. Depending on the ORDER BY clause in the SQL and the ID, the SelectedIndex may or may not match, so respond to SelectedValueChanged event. If you use SelectedValue instead of SelectedItem you wont have to thrash about with a DataRowView item.
Note that SelectedValue is Object so you will have to cast to integer or whatever for use elsewhere.
Related
I have DBGrid, but not all fields I can see inside Grid (TdxDBGrid)
Was adding three new fields(red V) to qPassengers and I don't see those values.
Here is part of query for DBGrid
SELECT * from Workers left outer join Map on Workers.id = Map.WId
Workers.DecodedStreet AS Street,
Workers.DecodedHouseNumber AS StreetNum,
Workers.DecodedCity AS City,
ISNULL(Map.DecodedStreet,'') AS DStreet, // new field
ISNULL(Map.DecodedStreetNum,'') AS DStreetNum, // new field
ISNULL(Map.DecodedCity,'') AS DCity // new field
and after that with property SummaryGroups I placed new fields to my Grid,
BUT I didn't find any New fields in property FieldName, that connecting fields from query with column in DBGrid
For example So I can't find corresponding field in FieldName for column dbGridTableDStreet,
because DStreet absent in FieldName list, BUT present in the query qPassengers
*Compile and Build was done *
Your grid is using data taken from 'qPassengers' dataset. If this dataset is using persistent fields, then adding the fields to the query of the dataset is not enough to make them visible. The new fields must first be added to the persistent fields collection of the 'qPassengers' dataset. Click with your right mouse button on the 'qPassengers' dataset and open the fields editor to do so.
Table 1: My general information table
Organization: A lookup/relationship field that defaults to "N/A" but pulls values from...
Table 2: Organization List
No ID field, just the names of the organizations in the order which they were added
My data entry form has a combo box for organization and I would like it to update when I add an organization that hasn't been added before. I know I am supposed to use the "Not in List" event, but I don't know how to update the Organization list using this event. How do I do this?
Make sure the Limit To List property of the combo-box is set to Yes.
Add a On Not In List event to insert the value to your source table when a new value appears:
Private Sub MyComboBox_NotInList(NewData As String, Response As Integer)
With DoCmd
.SetWarnings False
.RunSQL "INSERT INTO [Organization List](Organizations) VALUES ('" & NewData & "')"
Response = acDataErrAdded
.SetWarnings True
End With
End Sub
Edit... nearly forgot... before I answer, what have you tried? :)
Edit 2... the example given is for a string value. Remove the ' from either side of New Data if it's a numeric value (but probably not if it's an organisation name).
Edit 3... The INSERT SQL is just one way of putting data into a table. You may prefer the RecordSet.Add and .Update methods.
For example i have 3 fields in one table. Auto numbering ID, name, surname. And I want to unlock surname field only then when name is Jack. Is it possible in MS ACCESS 2013?
You create the form.
You create two textboxes. Create three textboxes if you also want to show the auto field.
You lock/disable all other textboxes other than surname by default.
On the Change_event of the surname textbox you use the following code:
.
Sub txtSurname_Change()
if (Me.txtsurname = "Jack") then
Me.txtSurname.enable = True
Me.txtSurname.Locked = False
Else
Me.txtSurname.enable = False
Me.txtSurname.Locked = True
End if
End Sub
Here it will continously check if what the user typed is Jack. It would be wise to also add code to disable the textboxes again if it is not Jack anymore.
I am currently attempting to design a database that requires a number of users inputting data via forms.
one of these tables is a 'user' table. Amongst the information in the table is
userid (int),
username (text),
first name (text),
last name (text)
In the even that I'm filling out a form and supply the the username in the username field is it possible if the username already exists to pull the first name and last name from the user table and auto-populate those form fields? If so can you point me in the right direction please?
Directly via access functionality or via vba? If not possible in 2003 is this possible in 2007?
Ok now for the auto fill (yes i did find one example), This will fill the username after you filled the userid (Both should be comboboxes in this case called Usernamecombo and useridcombo)
First make the query with a SQL similar to this:
SELECT [User].username FROM User WHERE ((([User].userid) Like '*' & [Forms]![Yourform]![useridcombo] & '*'));
Lets call this query "qry_username".
Then go to designview of the form and to the properties of the useridcombo, in the event/afterupdate property you make a event procedure (VBA) :
Private Sub useridcombo_AfterUpdate()
[Forms]![yourform]![Usernamecombo].Value = DFirst("username", "qry_username")
Forms("yourform").[Usernamecombo].Requery 'this last line is optional
End sub
Other fields can be added to the VBA pretty simply(dont forget the query)
Private Sub useridcombo_AfterUpdate()
[Forms]![yourform]![Usernamecombo].Value = DFirst("username", "qry_username")
[Forms]![yourform]![Firstnamecombo].Value = DFirst("Firstname", "qry_username")
[Forms]![yourform]![Lastnamecombo].Value = DFirst("Lastname", "qry_username")
Forms("yourform").[Usernamecombo].Requery 'this last line is optional
End sub
I have a similar form and i use to make these field as Comboboxes.
Then set the property row source as a query.
Set the criteria Where like this
WHERE ((([Users].Username) Like '*' & [Forms]![YourForm]![Username] & '*'));
This will allow the user to choose the name as fast as possible
But it will not fill it automatically because my users can have the same username as others.
before asking i wanna to show my tables and their relationships(created with ms access 2007)
here is the schema :
https://plus.google.com/photos/113802558354450440804/albums/5988059068393888337/5988059068667446962?banner=pwa&pid=5988059068667446962&oid=113802558354450440804
in this case, i create 3 combo boxes in VB2010 :
cbx_major(binded to MAJOR table)|major_id as the VALUE MEMBER, major_name as DISPLAY MEMBER
cbx_student(binded to STUDENT table)|student_id as the VALUE MEMBER, student_name as DISPLAY MEMBER
cbx_course( this is the question )
And here is the scenario :
first, i must choose what major is at cbx_major
second, the cbx_student will instruct the STUDENT table to select the student_name where major is equal to the selected value of cbx_major and set that query result as the DISPLAY MEMBER of cbx_student(this is done succesfuly without writing any code )
(this is the question)then the last, i want to set the cbx_course to display the course_name where student_id is equal to cbx_student.
i have done a lot of effort to do this :
i opened the combobox tasks menu and choose the student_course table and trying to create the query but it results "the schema returned by the new query differs from the base query"
i created the query in access by Joinning the table STUDENT_COURSE and COURSE using INNER JOIN then i bind the cbx_course to that query but it results wrong display.
i opened the xsd file then i create the query there but results wrong result
all those effort does not work.
i want to solve this case without writing code but using a technique such setting the taskbar menu, is it possible ? any idea? thanks so much for the attention