MS Access Combobox's value doesn't exist in table/query - ms-access

I have some ComboBoxthat are configured in table/query mode, that means that they get the list of items from a table in the database of Access.
Normally, this ComboBox -called Editar_Codigo and Editar_Nombre- only use the items from the list, but sometimes I want to wrote new items that still doesn't exist in the table (because then I'll press the "add record to table" button).
But there I get the problem, Access spawn a PopUp saying that the values in both ComboBox doesn't exist in the table, and I can't close that PopUp until I delete the wrote value in both ComboBox.
I found that exist an expresion that is activated when a ComboBox get a value that doesn't exist.
Private Sub ComboBox_NotInList(NewData As String, Response As Integer)
End Sub
But even using that, this happened:
First, I wrote the "wrong" value.
Second, the ComboBox_NotInList (in my case Editar_Codigo_NotInList and Editar_Nombre_NotInList) is executed. (I check it open an MsgBox).
Finally, the Access PopUp is open.
I want to delete the third item of the above "chain event". How can I do that?
Edit:
The idea is to write the code of the product in the Editar_Codigo ComboBox, the name in Editar_Name and the price and stock in the next two TextBoxs. And finally, press the button called "Añadir", which will add the record to the table called "Lista de Stock".
But the problem is that I can't write "inexistent values" in the ComboBoxes (values that doesn't exist in the table). So I want to be able to "ignore" the PopUp, How can I make Access to not raise a PopUp?

set limit to list property of combo box under data tab to no .

ok. I think What you can do is . First of all make that combo to see only one column Alpha, beta ,Gamma. Now whenever user selects the Beta than set property of combo box i.e. and write code to fetch corresponding value from table.
Example. Suppose name of combobox is combo1.
colomnInvisibleValue = dlookup("colomnName","TableName","VisibleColomnName= '" & me.combo1.value & "' ")
Variable colomnInvisibleValue will contain value (2) according to your last comment.!

Related

VBA Shows Listbox is Always Null Even When Option Is Selected

I created a listbox and added it to my user form from the GUI. The list box is single select and is one column. 3 options were added via the Row Source; the values are "", "jimmy", "jack" and I have the following vba to ensure an option was selected
If IsNull(Me!List10) Then
MsgBox "Please Select A Value!", vbOKOnly
Me.List10.SetFocus
Exit Sub
Else
selectedoption = Me!List10
End If
Now the issue I have is even when a value is selected the MsgBox still displays informing the user to select a value, so there is no way to pass! What would be the proper syntax to check if a listbox is null?
EDIT
This syntax will work if the user clicks the button on the user form while the list box is still selected (has focus), but it seems that the syntax can not retain the value if the list box does not have focus...
I didn't notice the first time that the first entry on your value list is "".
One "fix" (hack) would be to add a space " ".
The problem must be happening only when that first entry is selected. Does it need to be there?

Use a text box as a wildcard search on a field within a linked SQL Server Table within MS Access

I've managed to successfully build a combo-box that will allow a user to select a record from a list to get an ID from a table. With the table I'm currently working with running to around 60,000 records, it's not realistic to use this method to find the record.
What I want the user to be able to do is enter a name in a text box, and a combo box be populated with the relevant records from the table where one of the fields matches that. So if the user entered 'This' into the text box, the combo box would present records where the field had 'This', 'This and That' and 'this'. It would not present the record that only had 'That' in the field.
Lets say the Text box is called 'txtBox', the combo-box is called 'comBox' and the field in the linked SQL Server table 'LinkedTable' is called 'SearchField'
I would suggest you not use the combo box. Just have a text box in which the user can type in a few characters and hit enter key.
You THEN display a “list” of results that allows the user to select and click on any of the results to “edit” or “view” the given row of data.
In the following screen shot we working with a VERY small table of 500,000 rows. We looking for smith, so we just type in smi and hit enter. The results are displayed instant, and at that point the user can type in “first name” or just a few chars of first name and further drill down and filter.
The form looks like this:
The code in the after update event of the text box is simply:
Dim strSQL as String
strSQL = "select * from tblCustomers where LastName like '" & me.TextSearch & "*’"
me.RecordSource = strSQL
So very little code is required. You could I suppose fill in the results to a combo box, but then the user has to type into a box, then select something from a combo box and then somehow you bring up that record for editing. That’s like 3+ steps for the user.
Just use what Google or most accounting or darn near any computer software does:
A simple text box – you type in a few characters and when they hit enter the results are displayed for the user to pick.
Note in above the user can click on the “glasses” icon button to launch a form that displays the single record. The code behind that button is:
Docmd.Openform "frmEditDetails",,,"ID = " & ME!ID
I went down a different route to the suggested option in the other answer. This met my needs more accurately and might help someone with a similar issue.
First, I amended the query on the Combo box so that it used the value in the text box as a criteria for one of the fields.
In the Criteria for the 'SearchField' within 'LinkedTable' I entered:
Like "*" & [forms]![*FormName*]![txtBox] & "*"
This will restrict the results in comBox to those where the SearchField contains the value entered into txtBox. This doesn't update dynamically however and will only update after the first value entered into txtBox.
To get around this problem, I added the following to the 'On Get Focus' event for comBox
Private Sub comBox_GotFocus()
Dim ctlCombo As Control
' Return Control object pointing to a combo box.
Set ctlCombo = Forms!FormName!comBox
' Requery source of data for list box.
ctlCombo.Requery
End Sub
This will force the combo box to run the query again, using the current value in txtBox, each time it is 'clicked on'
As a result, I get the restricted list of values in the combo box that I need and allows the user to 'search' for a record that can then be used for creating a new record on a form.

Access SubForm recordsource revert to original

I have an Access form with two subforms, both in continuous mode. Since I cannot have a subform inside a continunous form, I had to do it that way (Datasheet is not an option either)
Either way, when I click on my first subform, I change the other subform record source using some rather simple code :
Public Sub MAJFiltre(intIdMembership As Integer)
IdMembershipFiltre = intIdMembership
Me.RecordSource = "SELECT * FROM T_PeriodeMembershipPartipant WHERE IdPeriodeMembreship=" & IdMembershipFiltre
Me.Requery
End Sub
This function is called from the first subform. I did this for another form and it was working fine. For this one, if I use a breakpoint, I can see the recordsource is changed, but nothing happen in the UI. However, if I put a breakpoint in a BeforeInsert event, I can clearly see the recordssource reverting to the original one (with no WHERE clause)
I also notice something unusual : If I save the form code while debugging, all of a sudden, it works. However, as soon as I close the form, it revert back to its "buggy" version.
Does anyway have an idea of what is going on and how I can correct /prevent it ?
Thanks
It sounds similar to a problem we suffer periodically, which relates to subforms linked to a parent form with link master/child ids: If you've (ie Access has done it for you) unintentionally saved a filter value as a property in one of the subforms, (filter by or in a record source), when your code reassigns the record source, this saved value prevents the new record source from being assigned correctly, and ms access then does what it thinks is best - in this case, reverting to the valid (prior) record source.
Check your subforms for unwanted saved values of data-tab properties.
Could the problem be that your field name is misspelled in your query criterion?
IdPeriodeMembreship
If the field in your T_PeriodeMembershipPartipant table is IdPeriodeMembERship instead of IdPeriodeMembREship your query would likely prompt you to enter the parameter value manually when it is run.
If that field does exist in your table but the value you're specifying in your criterion isn't found in that field, it will return no results and the second subform's recordsource will be set to an empty recordset.

Access VBA restore listbox selection after requery based on a non-bound column

So, I inherited MS Access 2010 (and 2013 now) code that has a listbox bound to a column that is not unique. However, that bound value is used in code, so, I cannot change which column is bound. But, I have a subform that needs to requery the listbox on the parent form. What I am struggling with is how to restore the selection in the parent's listbox to what item was selected before the listbox was requeried.
I have tried storing the Parent.List0 value before the requery and then using Parent.List0.Selected and Parent.List0.ItemData, but these do not seem to work because, presumably, the listbox is bound to a column that is not unique.
There is a column in the listbox that is unique. The 4th column (column(3)) is unique, but I'm not finding a way to select a listbox item based on a column other than the bound column.
I am confident someone has some ideas that will sort this out, but I've not been able to locate any solutions in my search of the web.
I am in version 2010 and while testing, I was unable to replicate a situation where the list box loses its value. Neither on a bound form or an unbound form.
In any case however, it seems that column 3 has a unique key - so you can use that.
' Assuming this code runs from within the sub-form
KeyID = Me.Parent.Controls("MyListBox").Column(3)
With Me.Parent.Controls("MyListBox")
.Requery ' Or do whatever you do that causes it to requery
' Find the previously selected item and select
For i = 0 To (.ListCount - 1)
If .Column(3, i) = KeyID Then
.Selected(i) = True
Exit For
End If
Next
End With
That should do the trick.

ms access auto fill main form based on focus of subform

I have a data entry/editing form with two combo boxes (Name and Group).
Each Group correlates to multiple names but each name is in only one group.
At the bottom of this form is a subform which is a continuous form
displaying a query of the associated table filtered based on an
unbound combo from which you select the group.
Basically, you choose which group you want to see and it displays a list of all the Names in that Group.
I want to make this form able to add and delete Names from the table (which it does with buttons already), but I also want to be able to select a Name from the subform and have the main form focus on that entry and auto fill the two bound combos.
And then from there I would like to be able to edit and save that entry or just delete the entry.
Similarly, I would like to be able to add a new entry without worrying about writing over a current entry. Access might do this automatically; if this is the case:
Is it even possible to edit an entry without deleting and then replacing it?
I also want to be able to select a
Name from the subform and have the
main form focus on that entry and auto
fill the two bound combos.
To do that, you can do a FindRecord on the main form's recordset, using the ID from the subform. The form will move to the correct record. From the subform's OnCurrent Event:
Forms!MyMainForm.Recordset.FindFirst "MyID = " & desiredRecordID
or
Forms!MyMainForm.Recordset.FindFirst "MyID = '" & desiredRecordID & "'"
Similarly, I would like to be able to
add a new entry without worrying about
writing over a current entry.
To do that, execute the following code:
DoCmd.GoToRecord acDataForm, "MyMainForm", acNewRec