Vba code to show selected data from combobox - vba

I have an Access 2010 form which has a combobox listing three columns:
ID
first name
last name
The combo box is bound to a table containing this data. The ID column in the combo box is hidden, it only shows the first and last name.
When the user selects a row only the the first name is shown. in the property section, I chose:
Column Count: 3
Column widths:0;3,3
Bound Column: 1
I made another text field and in the combobox I wrote the following vbcode:
text=combo.value
that shows in the text field the chosen ID.
I want to show in another field (text\combo?) the last name.
How can I do that?

well I did it with recordset.
if someone know a simpler solution, I will be glad to lrn.
this is my code:
Dim dbs As ADODB.Connection
Dim id As String
Set dbs = CurrentProject.AccessConnection
Set rsRep = New ADODB.Recordset
Set rsRep.ActiveConnection = dbs
rsRep.Open "tblRep"
id = Me.cbPrvFirstName
rsRep.MoveFirst
rsRep.Find "RepId=" & id
Me.txtPrvLastName = rsRep.Fields(2)
rsRep.Close

You can pull in the value from another column within the combo box using the below code. By default combo.value will always give the value from the first column even if hidden.
try
text=combo.column(x)
in your case to retrieve last name it would be
text=combo.column(2)
where x is a NUMBER of the column you want to retrieve 0 being the first column
You could also try an alternative - This works like a VLookup in excel if you are familiar with that.
(Not as simple as the first option ;) )
text=Dlookup("[last name]","tblRep","RepId=" & combo.value)

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.

How can a combobox store different type field values in MS Access database or have a dynamic column type

My goal is that the user chooses the type of field search and according to that another combo box show that available field:
CustomerID, CustomerFirstName, CustomerLastName, Phone are the columns of the Customer table.
If I choose customer ID in first the adjacent box shows:
When I choose first name as an option:
But after pressing Enter on "Dog", I get an error
The value you entered isn't valid for this field
The code for the combo box 2 is:
Private Sub SelctionBY_AfterUpdate()
Dim iVal
iVal = Me.SelctionBY.Value
Dim S As String
S = "SELECT " & iVal & ", CustomerID, CustomerFirstName, CustomerLastName, Phone From Customer"
Me.Combo33.RowSource = S
End Sub
So how do i fix this?
A combobox doesn't store anything, it's just for selection of something!
Data is stored in table fields and a combobox can be bound to a field to store data.
Your issue is that the bound field type changes, but it shouldn't. You can avoid that by binding the data field to the second column of the combobox, asCustomerIDis customer tables PK(Primary Key) that should be stored as FK(Foreign Key)
Me.Combo33.BoundColumn = 2

fill textbox based on combobox selection vb.net *data not bound [duplicate]

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.

Query to return field names in a table within Microsoft Access

I thought there was a way you can do this in ms access whereby I can run a query on a table called 'Employees' for example and it will return the field names i.e. EmpID, FirstName, Surname titles,not the values of the field e.g. 13, john, doe.
Is this possible in ms access?
Regards,
forestgump
You will be needing a Recordset object to play with this. Not sure how you intent to use. But this code will print it to the immediate window.
For more info on how to use Recordset - http://www.utteraccess.com/wiki/index.php/Recordsets_for_Beginners
Public Sub printFieldName(inputTable As String)
'Takes a TableName as input and prints the field names
' of that Table.
Dim tmpRS As DAO.Recordset
Dim fCtr As Long
Set tmpRS = CurrentDB.OpenRecordset("SELECT * FROM " & inputTable)
For fCtr = 0 To tmpRS.Fields.Count - 1
Debug.Print tmpRS.Fields(fCtr).Name
Next
Set tmpRS = Nothing
End Sub
Usage would be,
printFieldName "TransactionTable"
FirstName
LastName
manTeam
probEnd
department
If you just want a list, open the table, select the first line, copy it and paste it into Excel. The field names and the first record will be pasted. Delete the first record. Copy the row with the field names and paste, transpose to get a list. Obviously not usable if you need the info dynamically but helpful if you need a list to work with.

Linking result of a query that related to a Listbox value to textboxes?

I have a problem in MS Access and I don't know how can I solve this problem.
I have a listbox on my Form. If the use changes the value of the Listbox,a SQL query should be run and the result should be displayed is some Textboxes. For example:
The user select 'A' value From the list box then this query should be run:
SELECT XValue, YValue,Wert FROM Mytable WHERE Name='A' and ID=fzgID
then I want to display XValue, YValue and Wert in three textboxes. The problem is may be I have more than one pair for this combination (XValue, YValue,Wert).
How can I do that? How can I Link the list box and the query to the textboxes?
thank you so much
Every ListBox has a ListBox_Click() procedure, which is run when an element of the listbox is clicked.
You can get, what you want, if you do something like this:
Sub ListBox1_Click()
Dim valueOfListbox As String
valueOfListBox = ListBox1.Value
' **** here comes your code ****
' get the actual value from the listbox and do the query
' change the values of the textboxes to the result of the query
End Sub
If your listbox is named different than "ListBox1" you have to change the name of the procedure. For example if the listbox is named "blaBox" it has to be blaBox_Click().
Depending on the structure of your data, I would do something like this. Set the table/query of your listbox to have all the data you want to display.
SELECT ID, Name, XValue, YValue, Wert FROM Mytable
In the Format tab of the property sheet of the listbox, set the column count to 2, and the column widths to 0";2" or your preference. This will hide the other values in the listbox rows.
In the three textboxes, set the control source like so.
Textbox1.ControlSource: =[Listbox1].[Columns](3)
Textbox2.ControlSource: =[Listbox1].[Columns](4)
Textbox3.ControlSource: =[Listbox1].[Columns](5)
You may need to adjust the numbers. A listbox has a property called Columns that enable you to access the values at different columns of the listbox. If you don't want to or cannot have all the data in the listbox, then you can use the DLookUp function in each textbox.
Textbox1.ControlSource: =DLookUp("XValue", "Mytable", "ID=""fzGID"" And Name=""" & [Listbox1] & """")
The reference to [Listbox1] will pull the value of the bound column. If the bound column is not the data you are looking up by then you will need to reference a column (i.e. [Listbox1].[Columns](2)).