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.
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
In my Access Form I have a Text Box that should have this value:
SELECT [QueryOne].[Company] FROM [QueryOne] WHERE ([QueryOne].[ID] = "1000"
So, I wrote in the ControlSource of this textbox the following string:
= ( SELECT [QueryOne].[Company] FROM [QueryOne] WHERE ([QueryOne].[ID] = "1000" )
but the result that I get is that the textbox.value is #Name?. Maybe this string should not be in the ControlSource?
Use DLookup:
=DLookup("[Company]"), "[QueryOne]", "[ID] = '1000'")
or more probably ID isn't a text column, then
=DLookup("[Company]"), "[QueryOne]", "[ID] = 1000")
Access text boxes cannot be directly used to execute SQL. You can however call a function from the text box by setting it as the Control Source and display the result. Access offers a number of functions which can be used to retrieve data from a database. DlookUp should do what you require here:
=DLookUp("[Company]","[QueryOne]","ID='1000'")
You probably want to be able to enter the company ID to return the name. To do that, change the function to refer to another input textbox on your form like so (obviously just change the form and text box names to match your elements):
=DLookUp("[Company]","[QueryOne]","[ID]='" & [Forms]![Form1].[txtCompanyID].[Value] & "'")
Of course, if you are doing anything more than the most trivial of lookups, the correct way of doing this would be to set up a Record Source for the form and set the text box Control Source to a field from this dataset.
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.
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 have an unbound text box that I want to enter a number in and then it would go in a form. When I do this, I want to change the status of checkbox in another form as well. So it is like inventory. When I add this item to this box, I need the checkbox value to change because it's no longer in inventory. So the form I have is 'Info' and the checkbox is named 'ChkboxAvailableUse'. I have the checkbox correlated to a kit number and location and stuff too. So when I enter the number in an unbound text box called 'AssignKit', I want that number to look for the same kit number the 'Info' form and the 'InvKitNumber' text field, and then change that records checkbox (ChkboxAvailableUse) to change to false. I hope this makes sense. I have the line of code I thought would work below. Any help would be fantastic. Thank you
CurrentDb.Execute " UPDATE Info SET ChkboxAvailableUse = FALSE WHERE InvKitNumber = " & Me.AssignKit & ""
If you just want that form to uncheck or check in the table to match your form or subfrom then you could use a recordset.
Dim myR as Recordset
Dim strSQL as String
'This SELECT string will pull from the assignkit field in the subform
'and will be used find the matching record in the table
strSQL = "SELECT * FROM info WHERE InvKitNumber = '" & Me!subformnamehere.Form.AssignKit & "'"
'This will make your recordeset variable the one record you want to modify
Set myR = db.OpendRecordset(strSQL, dbOpenDynaset)
'Now that the recordset is pointing to that row, you can change the checkbox
'with something like this, and even use an IF statement
myR.Edit
myR![ChkboxAvailable] = True
myR.Update
'Then close the recordeset when done
Set myR = Nothing
I hope this helps, let me know if you need me to tweek it.
The sql UPDATE statement updates fields in a table. Tables don't have checkboxes, forms do, and I assume that the field (in the table) is not called ChkboxAvailableUse ?
If the field is named AvailableUse then you would update this field with:
CurrentDb.Execute "UPDATE Info SET AvailableUse = FALSE WHERE InvKitNumber = " & Me.AssignKit
Then you could ReQuery the (other) form (or a control on the form) so that it reflects these changes.