I have a combobox on a form and I'm trying to programmatically select one of the items in the combobox after I've run my SQL query on the access database.
I use the following code to iterate over the items and set the selected item:
'Make the appropriate location appear in the combobox
For i = 0 To cboLocations.ListCount - 1
If Me.cboLocations.Column(0, i) = locindex Then
Debug.Print "locindex: " & locindex & vbCrLf & " Me.cboLocations.Column(0, i):" & Me.cboLocations.Column(0, i)
Me.cboLocations.SetFocus
Me.cboLocations.ListIndex = i '<<< error 2115
Exit For
End If
Next i
As indicated, I keep getting error 2115: The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Access from saving the data in the field.
Neither of the properties for this combobox indicated in the error message is set to anything. So I'm stuck. Please advise.
programmatically select one of the items in the combobox
The way I've always done that is to assign something to the combo's Value like this ...
Me.MyCombo.Value = "target text"
Value comes from the Bound Column of the combo's selected row. (You can find Bound Column on the Data tab of the combo's property sheet.) Conversely, assigning "target text" to Value selects the matching row.
In your situation, I think you're trying to select the combo row which contains the same text as your locindex variable. And if that is true, then I think all you need is this ...
Me.cboLocations.Value = locindex
As far as when you do that, neither Before Update nor Validation Rule seems like the right choice to me. I suggest you do it from whatever code you're using to run your "SQL query on the database", immediately after the query.
You are probably colliding with the BeforeUpdate event.
Try using AfterUpdate.
Related
I have an Access database in which I store some data, and that database has 13 tables plus a reference one.
I want to make a form where there are several checkboxes and a button. Each chekbox represents a table, and every table selected will be joined inside a query writen in VBA, associated with the button click.
I've already made the same thing in Excel, and it works perfectly, so the only problem here is that I don't know how to access the checkbox value and use an IF condition to get the correct SQL string.
To make it clear, here I have a IF statement for one of the checkboxes in Excel:
If Range("B8").Value = True Then
CTODStrc = ", CTODTYPE, CTOD.TEMPERATURE, VALIDITY, DELTAR, DELTAL"
CTODStr = " JOIN CTOD ON REF.ID = CTOD.REF_ID"
JoinStr = JoinStr & CTODStr
Columns = Columns & CTODStrc
End If
SQLStr = RefStr & JoinStr 'Query sentence
The SQLStr is the query text, and it has a prior "select" string which is added.
In Excel, the cell B8 was associated with the checkbox, but in Access I have to make this condition using a checkbox thats in the form - how can I do it?
I've tried Me.CbName.Value, but it says the command is not supported.
Thank you.
The checked state of a checkbox is given by the Value property of the checkbox control. This property may be 0 (unchecked), -1 (checked), or Null for a block-filled triple state checkbox.
Since the Value property is the default property for a checkbox, and assuming you are not using a triple state checkbox, you should be able to use simply:
If CBName Then
' Do stuff
End If
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.!
I have a form bound to a query, with controls bound to fields of the query.
I also have an unbound combobox, whose Row Source is the same query, used to select the current form record via its After Update event handler:
Private Sub Loc_cbo_AfterUpdate()
DoCmd.SearchForRecord , "", acFirst, "ID = " & Str(Nz(Loc_cbo, 0))
End Sub
The combobox has three columns:
the Bound Column, an invisible ID,
a location code,
a location name.
The combo's Row Source is SELECT * FROM Sites ORDER BY Loc DESC. (Originally, it was just Sites but since I have the form's Order By as Site.Loc DESC, I wanted the order to match.)
It works fine.
Of course, I can also navigate through the form’s records via its navigation control (First, Previous, Next, Last).
That, too, works.
However, I’d like the combobox to be synchronized with the current record as it changes via the navigation control.
I’ve tried to do it via the form’s Current event handler, three different ways.
First, to set the combobox’s Text value to match that of the corresponding textbox control
Private Sub Form_Current()
Loc_cbo.SetFocus
Loc_cbo.Text = Loc_txt ‘ ERROR
End Sub
but I get
Run-time error ‘2101’: The setting you entered isn’t valid for this
property.
Second, to set the combobox’s ListIndex value relative to the current record
Private Sub Form_Current()
Loc_cbo.SetFocus
Loc_cbo.ListIndex = CurrentRecord - 1 ‘ ERROR
End Sub
but I get either the same Run-time error ‘2101’ or
Run-time error ‘7777’: You’ve used the ListIndex property incorrectly.
in the same place.
Third, (thanks to HansUp's early answer) to set the combobox’s Value property:
Private Sub Form_Current()
Loc_cbo.Value = Loc_txt.Value
End Sub
This "works" in so far as there is no error message and, according to debug.prints, the value of Loc_cbo does change to match that of Loc_txt. However, the visible textbox-like portion of the combobox now appears to be empty/blank (nothing in it) always.
How can I get the record selector combobox to agree with the current record when the latter has been changed via the record navigator?
I cannot believe users will be happy having the two out of synch.
I also cannot believe I haven't found this problem described anywhere else!
Assign to the combo's .Value property to change its selection. For .Value, you don't need to SetFocus.
Private Sub Form_Current()
Me.Loc_cbo.Value = Me.Loc_txt.Value
End Sub
Note this suggestion assumes the combo's .Value is what you want to match to the text box value. Check to confirm you want the match between the combo and text box to be based on the combo's .Value instead of another of the combo's columns.
Post mortem:
The content of the text box was intended to match one of the combo's columns. However, since the combo's Bound Column contains numbers associated with that other column, Martin used a DLookup expression to fetch the number which corresponds to the text box content and assigned that number to the combo's .Value property:
Me.Loc_cbo.Value = DLookup("ID", "Sites", "Loc= '" & Me.Loc_txt.Value & "'")
Note a combo's .Value is the value in the Bound Column of its selected row.
I'm working on a little project in Access using VBA. I created a report with some data from three different tables. In the Report_Open method I want to assign a query to the ControlSource of a TextBox.
Here is my code
Me.textImpactP.ControlSource = DLookup("[ImpactText]", "Root_cause_basic_reports", "[Report_id] =" & rid & " And [Root_cause_basic_id] =" & rootCauseId)
When I run this I keep getting the dialog "Enter Parameter Value".
The parameter "people" is the result of my DLookup and is supposed to be the value of my TextBox on my report.
Does anyone have an idea how to get the parameter "people" as a value in my TextBox?
Enter Parameter value does not represent, what you think it does. It normally pops up when you try to open a Report whose record Source is based on a Query which requests a parameter. Check the Query again.
I have a combo box and several text boxes on a form. When I select a value from the combo box I want it to run a query based on that value and populate the text boxes with the data returned by the query. The query should only return one record and the textboxes correnspond to different columns in that record.
I have this code:
Private Sub cbo_equip_loc_Change()
Dim location As String
Me.cbo_equip_loc.SetFocus
location = DLookup("NAME", "Query1", "position = '" & Me.cbo_equip_loc.SelText & "'")
Me.Text51.SetFocus
Me.Text51.Text = location
End Sub
But I get this error: "This property is read-only and can't be set"
Any ideas?
Solved: Im an idiot.
I had some value in the Control Source from something I was trying to do before. Removed that and it worked!
There is no need to do this:
Me.Text51.SetFocus
Me.Text51.Text = location
it is true that the text property is only available when the control has the focus, but the value property is available without any focus, or Access VBA is quite happy with just the name of the control:
Me.Text51.Value = location
Or
Me.Text51 = location
Textbox Text51 is locked, set property Locked to False.