How can I assign a value to an Access Combobox? - vba

I have a form (FrmAddBookReviews) that is opened when certain events in another form (FrmAddArticles) occur.
I want to initialize the FrmAddBookReviews with some values from FrmAddArticles. It works fine except for the Author of the book review who I have in 2 comboboxes, where I want to write the first name in one and the last name in the other combobox. I do not want to use an unbound textbox, because I want the value of the primary key of the reviewer to appear in the table belonging to FrmAddBookReviews.
If I do (rstauth.Fields(0) is the primary key to TabAuth)
Me!cmbFnam.SetFocus
Me!cmbFnam.Selected(1) = True
Me.cmbFnam.Column(1) = rstauth.Fields(1).Value
I get run time error 424: Object required
I have tried a DLookup to get the name followed by
Me.cmbFnam.Column(1) = strFnam
gives "the value does not match this field", in spite of the fact that column 1 of the combobox is text (but the control-source is the id of the reviewer). the same happens when I do
Me.cmbFnam.Column(1) = rstauth.Fields(1).Value
I have also tried to give the row-number in the Columns-clause. This gives me the "object required" error.
Can I get any help?
Biorn Veiro

Related

Getting a type mismatch but combo box still searches and displays data. ID column is being used to pull through Name but doesn't work

Error popup after click ok on type mismatch
I am using a combo box to search for records and it is telling me I have a type mismatch. I have been unable to find where this mismatch would be. The combo box still performs the desired function of selecting all records based on their "region"
I am using an ID field which is selected using the combo box but I want to pull through a name as well so I have input an unbound textbox and used VBA on change in the ID to update the name to the corresponding ID in the unbound textbox
Form
For privacy reasons I snipped around some of the other fields showing data.
The VBA in the on_change event in ID goes
Private Sub txtSupplierID_Change()
Me.txtSupplerName.Value = Table("tblSuppliers").SupplierName
Where Table("tblSuppliers").ID = txtSupplierID
End Sub
I'm aware this code could be very wrong but I could not find anywhere else that showed me how to do what I was trying to do. Any suggestions on how to actually do it would be appreciated. If I have not been as detailed as needed or yo have any questions please ask.
Edit for Clarification:
The form is not being used to save data. It is only being used to display data and issue an output to a report.
Code is not just wrong, it is nonsense. Cannot reference a table object like that. And that Where usage is not valid. One way to pull value from a table is with DLookup domain aggregate function. Advise use AfterUpdate instead of Change event.
Private Sub txtSupplierID_AfterUpdate()
Me.txtSupplerName.Value = DLookup("SupplierName", "tblSuppliers", "ID = " & txtSupplierID)
End Sub
Might want to correct textbox name from txtSupplerName to txtSupplierName.
However, should not save both supplier ID and supplier name into this table. There really should be no need for this VBA. Just put DLookup() expression in a textbox ControlSource. For other methods of pulling the supplier name to form, review answer in MS Access Identical Comboboxes for Autofill

How do I validate a Form input field with a Table row value in microsoft access?

I am having issues with setting a Validation rule in Access.
I have a database with the tables Clients, TypeClient, Sales, SalesList, Items.
I have a form for Sales, with a sub-form SalesList inside which has a relationship with Items in order for me to put several stock items in Sales instead of only one item.
Inside Sales table is ID, Date Sold, ID Client.
Inside SalesList is ID_List(referenced to ID in Sales), ID_Item(referenced to ID in Items), Quantity.
Inside Items is ID, Name, Stock(how much we have in stock), Price
The issue is that I am trying to validate the data I enter in the sub-form Quantity field to be higher than 0, but no higher than the available stock.
The issue is that I have tried using the expression builder to get the value from a calculated query that has 2 fields - the ID and the value for that id, with criteria that is for the ID to get it from the main form, the sub-form, the combo box input and the query works.
But when trying to get it like this:
It shows an error "The expression [Query bla] you entered in the form control's ValidationRule property contains the error The object doesn't contain the Automation object 'Query bla'".
I tried using directly the table Value(but it won't work anyway as it doesn't know for which field to get the stock value from), still the same error. I guess it can't reference to anything else? Normal validation rules work, but I want to validate it not to exceed the value of the available stock for the item I am selling now.
Nowhere on the internet there is a tutorial how to do it with expression builder. How do people validate their fields then?
It doesn't stop me in this case to sell 200 items when I currently have stock of only 2 of them, for example.
Note: I have tried DlookUp in expression builder, straight up tells me No.
Sample wrong validation rule expression builder code:
<=[Query bla]![Stock]
<=[Items]![Stock]
I am currently using VBA and fetch the record I need(the current stock) with one of the following and my unbound text is changing on every subform to the same, it shouldn't happen like that. What can I use to populate a field for each record uniquely:
Private Sub ID_Product_IZBOR_Click()
'Me.Stock_ValueField = DLookup("[Nalichnost]", "Stoka", "[ID]=" & Me.ID_Product_IZBOR)
Me.Stock_ValueField = Me.ID_Product_IZBOR.Column(2)
End Sub
Partial solution: I created a new Dim as Integer and fetched the records based on the ID provided by the field in the form and used BeforeUpdate to validate the current stock. Unfortunately the only way to see the current stock level is to click on the combo box where you choose your product and check the column for that one, it doesn't show anywhere else :(
Don't use the expression builder (I NEVER do) - just type the needed expression in property.
>0 AND <=DLookup("Stock", "Items", "ID=" & [ID_Item])
Another approach is to return Stock in textbox ControlSource then ValidationRule references that textbox. Also, user can then see the quantity limit. Methods of pulling the Stock value from Items table:
include Stock field in Items combobox RowSource - textbox then references column by its index (index begins with 0): =[cbxItems].Column(2); VBA may be needed to Requery combobox after record entry/edit is committed to table.
include Items table in form RecordSource - bind textbox to Stock field (Locked yes and TabStop no)
DLookup() expression in textbox ControlSource
Use ValidationText property to give users a custom message.
However, ValidationRule will not prevent user not entering any value if they skip the control. If you want to make sure a value is entered, either set field as required in table or use form BeforeUpdate event to validate record data.

MS Access Object Required Error on Combo Box Column

I have a combo box in Access named StateID. It's rowsource contains two columns one is ID which is in Column 0 and another is description which is Column 1.
I want to populate the value of the ID column by doing something like:
StateID.Column(0) = rs("ID")
I made sure that the combobox is name StateID. The rs("ID") is also returning a value but my code breaks on the line above and I get
Object Required
error.
I alos tried Me.StateID.Column(0) and StateID.Column(0).value but I still get the same error
You cannot do "something like" that because the "Column" property is Read-Only, per Microsoft Access documentation; seems like this is your problem.
I dont' know why you don't assign the record source in design mode. Any way, in the Form Load event you can do this
StateID.RowSource = "SELECT codage,nomage FROM TheTable"
Well I just tried various things and doing it this way works:
With StateID
.ColumnCount = 2
.Value = rs("ID")
End With

How to write correct field criteria to get rid of the 'enter parameter value' erro?

PICTURE OF QUERY :
I'm plotting graphs for 10 machines (machine 1, machine 2,...) performance -
average vs date graph.
User shall select the machine from a combobox (i made the values obtained from query, or should I make it from the table?) and set it to contain unique values only.
After selecting the machine, user shall click OK button. and a graph of the selected machine should be displayed in a new form.
Problem is, every time I click OK, it prompts me with the ENTER PARAMETER VALUE Forms!ViewReport!Combo9.
After I enter "machine 1" in the ENTER PARAMETER VALUE, it displays the graph correctly. I am expecting the graph to be displayed directly without the error.
The dialog appears because the query has no access to the field Combo9. Probably your form is not open or ViewReport is a subform, in this case, the reference to Combo9 should be different. Check, for instance, this giude for controls references.
But the most reliable method is to create a public function in the standard module, which should return the value from the desired form's field. It can check if the form is open and return the default value if the form is not available or the value is not selected. This function can be used in queries as criteria or in calculated fields.

Access assigning Null / Default value

I have a form in Access with several comboboxes.
If the user selects X in combobox A a value is assigned to another field (Field A). This is working fine now (through VBA code in the on change event).
However, if the user selects Y in combobox A I would like the value in Field A to return its default value (empty/null).
Currently I have it returning to 0, but that is not ideal. What can I do to make it return to default/null/empty?
I tried:
Field A = Null
But then I get the following message:
You tried to assign the Null value to a variable that is not a Variant data type. (Error 3162)
When I make it Field A = 0 then it is working, but as I said before that is not ideal for my situation.
Thanks in advance.