click button VBA to update a drop down field to a specific value - vba

I'm wanting a button to perform multiple actions. when clicked it should:
add 1 to dial attempts field
change the "leagGenOutcome" to "no answer"
the problem is that the "leadGenOutcome" is a dropdown field taking it's options from another table.
I used this VBA code which works for a field with no drop down, but doesn't work on the drop down field.
Private Sub AddDialAttBTN_Click()
DialAttempts = DialAttempts + 1
LeadGenOutcome = "no answer"
End Sub
I know I must be missing something simple to get this to work. any help is much appreciated.
EDIT
by "doesn't work" the DialAttempts counter ticks up as it should but a "run time error" displays stating "the value you entered isnt valid for this field"
LimitToList property is Yes
"no answer" is a listed item
the field that LeadGenOutcome is bound to is called LeadGenOutcome01 in a form called LeadGenOutcomes.
currently it's a test database which I am using to learn how best to arrange and format.

changed "no answer" to "2" which is the ID for that response in the linked form. I had thought that it would display as "2" and not the associated text. this is not the case. thank you June7 for the assist.

Related

Problem with active status when an continue form have zero record for a filter

this is my first question here and english isnt my language but I'll try to explain at my best.
Im using MS Access 2007.
The form is a "continue form" with a header section where you find an "apply filter" button and a field named "cmpCercaCognome" (italian language for "find surname" plus "cmp" in italian language as field).
This is a cut out of the feature where I am having problems.
The function is fine if the search filter that is set in the field produces at least one result.
The function fails if there are no results.
Private Sub cmpCercaCognome_Change()
If (ActiveControl.Name = "cmpCercaCognome") Then
LunghStr = Len(Me.cmpCercaCognome.Text)
else
LunghStr = Len(Me.cmpCercaCognome)
End if
'Something else but I dont reach it
end sub
The error appear when I'm clicking in the field "cmpCercaCognome" and then Im typing a key there to change the value to use for a filter that I'll apply with a button.
The check "activecontrol" is true still the Len function give error "2185 - Cant read control... without an active status..). How? I just check that have active status! And Im typing just there...
I tryed to modify the IF check so:
"If (ActiveControl.Name = "cmpCercaCognome") And (Me.RecordsetClone.RecordCount > 0) Then"
So I can check just when the count of the record is more than 0.
The problem that so I check the .value of the field that dont contain the last pressed key.
So isnt usefull for me
Is there any way to avoid the problem by keeping control of the temporary value of the field? Or is there a way to make the "temporary value" effective right away so as to avoid checking the .text property?
Thanks for any help you can get on this.

Button to check for missing values in a MainForm and Subforms in MS Access

New to Access (still), have only basic VBA skills.
I've got 3 subforms (subfrm_PackingSteps1 , subfrm_MetalDetection and subfrm_Weights - the first 2 are continuous and the other one is single form) within a main form (frm_daily_packing_record) that users go through and input data. The user should be able to input data in no particular order, and only at the end there would be a button to confirm that the user is ready to save this form.
I'd like to have this button on the main form that checks each control (in main form and subforms) for empty values. I found and adjusted a code to check the recordset of one of the continuous forms (see below), but I can't figure out:
how to include a code that checks each control instead of manually adding all of them (I've used a function before that utilises the Tag property, but can't add it to this)
how to keep the button in the main form while checking the controls/recordsets in the other subforms.
Thanks in advance.
Private Sub ConfirmBtn_Click()
Dim blnSuccess As Boolean
blnSuccess = True
Me.Recordset.MoveFirst
Do While Not Me.Recordset.EOF
If IsNull(Me.pc) Or IsNull(Me.InnerP) Then
blnSuccess = False
Exit Do
End If
Me.Recordset.MoveNext
Loop
If blnSuccess = True Then
MsgBox "You may proceed to save this record"
Else
MsgBox "You still have some empty fields to fill in!", vbCritical + vbOKOnly, "Empty Fields!"
End If
End Sub
I personally don't like this because it is too code-heavy and can easily break when the form is modified.
Instead may I suggest doing it slightly different, for each field have vba code .ondirty or .onupdate and do the validation checking right as the user is actually on that field.
This has 2 benefits, it is creating the validation when you are creating each form field and it STOPS the user right when their first mistake or bad data is entered. The last thing I want is to enter 50 fields, scroll to the bottom, submit fails then scroll back and try to find where the mistake was. If validation is done while the user it doing the actual data entry, when you get to the bottom you should have valid data and the submit should succeed without further testing.
Less code to debug and timely messages to the user if an error is caught!

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

Microsoft Access 07 VBA: Using Text Box or Combo Box as a Control to hide Columns

I currently have a form. The form contains a subform that displays a Query. The Form also has 2 Text Boxes and buttons for both Boxes. The first box acts as a filter for the query to filter specific record. The second box is intended to be used to hide columns in the query. My issue is that my code will not recognize the text box, or any outside source from my form as a Field Name to be found. Here is my current code:
Private Sub Command137_Click()
Forms![Vermont]![Query1 subform].Form.[Query1 Field Name].ColumnHidden = True
End Sub
Currently if I replace "Query1 Field Name" with any field name that exists in the query the column will hide. However if replaced with anything else I recieve the following error:
"Runtime Error '2465'
Microsoft Access Can't Find the Field '|' reffered to in your expression"
I am pretty sure that I am not referencing the Form Control correctly. I have tried replacing [Query1 Field Name] with the following:
[Text142.Text]
[=Text142]
[Text142]
[Forms![Vermont]![Text142]]
I am very new to VBA but I definitely feel as if this is an easy fix; if possible.
Thank you in advance for any help!
Subform references are always a bit weird. This should work, though:
Forms![Vermont]![Query1 subform]![Text142].ColumnHidden = True

How to make a textbox not defualt to the first vaule in a database?

This may be something simple stupid, but its not working for me.
I made a table of rates in Access 2007.
I made a form for the table using create, split form, and followed the wizard.
I added a "Add Record", "Delete Record" and "Exit" button to the form.
For the auto created textbox, in properties, data, defualt value, I set it to "Rate"
When I first open the form the textbox always shows the first value in the database and will edit the first value in the database if I type in it. After I click the "Add Record" button it shows "Rate" and adds records to the end of the database.
So the question is, how do I get the textbox to read "Rate" and have it defualt to the end of the database when I first open it up I can add records?
Thank you,
This is relatively simple when using the switchboard. New switchboard item; Open Form in ADD Mode > Put in forms name. This would open the form at a new record.
Prgammically you can use docmd.openform eg: docmd.OpenForm(form_Name,acNormal,,,acFormAdd).
See Link in Remou's comment
HTH
Roger